Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is it possible to print different lines to different output files using awk

Tags:

I want to print different lines to different output files using awk, depending on different conditions, like

awk '{if($2>10) print > outfile1; else print > outfile2}' infile 

but this script doesn't work how to modify it? thanks!>

like image 248
user1769686 Avatar asked Oct 25 '12 21:10

user1769686


People also ask

How do I print a specific line in awk?

To print a blank line, use print "" , where "" is the empty string. To print a fixed piece of text, use a string constant, such as "Don't Panic" , as one item. If you forget to use the double-quote characters, your text is taken as an awk expression, and you will probably get an error.

Can awk read from multiple files?

Yes, you can read from multiple files at the same time using awk. In order to do that, you should use the getline command to explicitly control input. In particular, you want to use getline with a file so that you're not reading from the file(s) passed in as main arguments to awk.

What is awk '{ print $2 }'?

awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output. xargs kill -${2:-'TERM'} takes the process IDs from the selected sidekiq processes and feeds them as arguments to a kill command.

How do I print a third line in awk?

# Tell awk to print the third input record of the current file. awk 'FNR==3 {print}' my. txt.


1 Answers

You need to close the file names in double quotes:

awk '{if($2>10) {print > "outfile1"} else {print > "outfile2"}}' infile 
like image 109
sampson-chen Avatar answered Sep 28 '22 06:09

sampson-chen