Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter file with awk and keep header in output

Tags:

header

filter

awk

I have following CSV file:

a,b,c,d
x,1,1,1
y,1,1,0
z,1,0,0

I want to keep lines that add up more than 1, so I execute this awk command:

awk -F "," 'NR > 1{s=0; for (i=2;i<=NF;i++) s+=$i; if (s>1)print}' file

And obtain this:

x,1,1,1
y,1,1,0

How can I do the same but retain the first line (header)?

like image 298
fibar Avatar asked Oct 26 '16 11:10

fibar


People also ask

What is awk '{ print $1 }'?

The awk is a very powerful command or interpreted scripting language to process different text or string data. The awk is generally used to process command output or text or configuration files. The awk provides '{print $1}' command in order to print the first column for the specified file or output.

How do I print output from 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.

How to use AWK to filter rows in Excel?

Using AWK to Filter Rows 1 Let’s look at the data we want to filter. What we want to do is get the rows from Chr (column 7) when it equals 6 and also the Pos ... 2 Printing Fields and Searching. We can also use AWK to select and print parts of the file. ... 3 Filtering Rows Based on Field Values. ...

How does AWK work with a file?

It works by reading a given line in the file, makes a copy of the line and then executes the script on the line. This is repeated on all the lines in the file. The 'script' is in the form '/pattern/ action' where pattern is a regular expression and the action is what awk will do when it finds the given pattern in a line.

How to use $0 field in AWK to print the value?

Using $0 field. Awk uses the variable 0 to store the whole input line. This is handy for solving the problem above and it is simple and fast as follows: $ awk '/ *$ [2-9]\. [0-9] [0-9] */ { print $0 "*" ; } / *$ [0-1]\. [0-9] [0-9] */ { print ; }' food_prices.list

How do you use an escape character in an AWK statement?

Use Awk with () Escape Character It allows you to take the character following it as a literal that is to say consider it just as it is. In the example below, the first command prints out all line in the file, the second command prints out nothing because I want to match a line that has $25.00, but no escape character is used.


2 Answers

$ awk -F "," 'NR==1; NR > 1{s=0; for (i=2;i<=NF;i++) s+=$i; if (s>1)print}' file
a,b,c,d
x,1,1,1
y,1,1,0
like image 66
jas Avatar answered Sep 28 '22 00:09

jas


Since it's only 0s and 1s:

$ awk 'NR==1 || gsub(/1/, "1") > 1' file
a,b,c,d
x,1,1,1
y,1,1,0
like image 40
James Brown Avatar answered Sep 27 '22 23:09

James Brown