1

I am trying to code this command to run in screen along with 2 others that will be using cat to read a file then pipe it into awk for filtering. Here is the command;

screen -d -m /bin/cat /var/www/html/filter/unfiltered.txt | awk '{print $1}' > /var/www/html/filter/first.txt

If I check the screen that is detached I see it filtering the list, when it completes, the file first.txt is created but empty. I saw something about using -L in screen for an output log, but I want the output going to the same folder the non filtered list is in and called first.txt, I cannot figure out why it will successfully run, but the output file is empty every time. If I run it without screen -d -m the file created is not empty. I think I am missing something with the screen command. I just started using screen. I am not sure when else to post.

roaima
  • 1,591
  • 14
  • 28
LinHost
  • 39
  • 5

1 Answers1

3

The reason this won't work is that the shell splits the command into two parts like this:

screen -d -m /bin/cat /var/www/html/filter/unfiltered.txt

awk '{print $1}' >/var/www/html/filter/first.txt

The screen runs cat on a separate pty. There is then no output on stdout to pipe to awk.

If you want the pipe to be run under screen, you need to group it with another instance of a shell:

screen -d -m bash -c 'cat /var/www/html/filter/unfiltered.txt | awk "{print $1}" >/var/www/html/filter/first.txt'

Having done that, it should become clear that this is a needless use of cat and actually the command can be simplified like this:

screen -d -m bash -c 'awk "{print $1}" </var/www/html/filter/unfiltered.txt >/var/www/html/filter/first.txt'

Notice I have changed the inside quotes from singles to doubles. This is because I have used single quotes to protect the entire command (and particularly $1 from early evaluation).

roaima
  • 1,591
  • 14
  • 28
  • awesome! I will give that a try, I saw the bash -c command listed while reading, but being new to screen I could not get the syntax correct, will try this ASAP, thanks so much! Thanks also for the cleanup, learning to bash script and loving it so far, so much to learn still. – LinHost May 14 '15 at 22:15
  • Would the above issue also happen if I put the command into a .sh file separating the two filter commands by && such as; cat /var/www/html/filter/unfiltered.txt | awk '{print $1}' > /var/www/html/filter/first.txt && cat /var/www/html/filter/first.txt | awk '{print $1}' > /var/www/html/filter/last.txt I have 2 commands for filtering to run in total – LinHost May 14 '15 at 22:18
  • @LinHost you really don't need the `cat`. But if you were to put your two `awk` commands in a script file you could call that directly from `screen`, yes. – roaima May 15 '15 at 00:47
  • I will give it a try without cat, i see your point about cat, I just copied the command from above as an example. If it works I will mark your answer. – LinHost May 15 '15 at 16:58
  • I would rather not use a sh file if I can do both commands, first one then second when the first is done in one line. This is in a PHP file so could I still use && between the 2 commands or do I use ; ? – LinHost May 15 '15 at 17:00
  • how about this command: 'awk "{print $1}" | sort -n | uniq -c | awk '$1 > 10' | awk "{print $2 " " $1}", i removed the cat and formated the paths like you did above, I also replaced the single quotes on the sides of print to double quotes, this is the second command, I can run each seperately, but if I can run one after another after the first completes that would be best for me – LinHost May 15 '15 at 22:16
  • I get the error "No such file or directory" when running the command without screen, with screen it just died without an error, the odd thing is there is a filter dir in HTML and there is a unfiltered.txt in the html/filter dir – LinHost May 15 '15 at 22:44