 onlyforbopi
3/10/2017 - 10:39 AM

## AWK - SELECTIVE PRINTING OF LINES

AWK - SELECTIVE PRINTING OF LINES

``````Print the total number of lines containing pattern					      awk '/Beth/ { n++ }; END { print n+0 }'
Find the line containing the largest numeric field					      awk '\$1 > max { max=\$1; maxline=\$0 }; END { print max, maxline }'
Find the line containing the larg numeric field (negative too)    awk 'NR == 1 { max = \$1; maxline = \$0; next; } \$1 > max { max=\$1; maxline=\$0 }; END { print max, maxline }'
Print number of fields in each line, followed by line					    awk '{ print NF ":" \$0 } '
Print the last field of each line					                        awk '{ print \$NF }'
Print the last field of the last line					                    awk '{ field = \$NF }; END { print field }'   OR    awk 'END { print \$NF }'
Print every line with more than 4 fields					                awk 'NF > 4'
Print every line where value of last field is greater than..		  awk '\$NF > 4'
Print every line where a specific field meets a condition					awk ' { if(\$5>1000) print \$0}'
Print every line that fulfills combination of conditions
Print all lines except those divided by 6					                awk 'NR%6 {print \$0}'
Print lines from line X and onwards					                      awk 'NR > 5 { print \$0 }'
Print lines where X field equals to..					                    awk '\$2 == "foo" {print \$0}'
Print lines with six or more fields 					                    awk 'NF >= 6 { print \$0 }'
Print lines that match two patterns in any order 					        awk '/foo/ && /bar/ { print \$0}'
Print lines where a specific field match two patterns in any ord  awk ' \$4  ~ /foo/ && \$4 ~ /bar/ { print \$0}'
Print lines that match one pattern but not another					      awk '/foo/ && !/bar/ { print \$1, \$2, \$NF}'
Print lines that match combination of conditions (1)					    awk ' \$5>1000 && \$9<1000 { print \$0}'
Print lines that match combination of conditions (2)					    awk ' { if (\$5>1000 && \$9<1000) print \$0}'
Print lines that match pattern (1) 					                      awk ' \$9 ~ /test/ { print } '
Print lines that match pattern (2) 					                      awk '{ if(\$9 ~ /test/) print \$0}'
Print lines that match combination of if and pattern/regexp				awk '{ if(\$5>1000 && \$9 ~ /test/) print \$0}'
Print lines that matches pattern/regexp anywhere (grep)					  awk ' /pattern/ { print \$0}'
Print line/field that do NOT match pattern (2 patterns)					  awk ' \$9 !~ /(awk|xt)/ { print \$0 }'
Multiple pattern / Multiple fields check					                awk '{if(\$9 !~/(awk|xt)/ && \$6 ~/Dec/)}'
Print and sort the names of all users					                    awk -F ":" '{ print \$1 | "sort" }' /etc/passwd
Print the first two fields in reverse order on each line.					awk '{ print \$2, \$1 }' file
Swap first field with second on every line.					            awk '{ temp = \$1; \$1 = \$2; \$2 = temp; print }'
Delete the second field on each line.					awk '{ \$2 = ""; print }'
Print the fields in reverse order on every line.					awk '{ for (i=NF; i>0; i--) printf("%s ", \$i); printf ("\n") }'
Concatenate every 5 lines of input with a comma.					awk 'ORS=NR%5?",":"\n"'
Print with Line numbers attacked					sed -n "/\$line/{=;p}" \$FILE_INP | sed '{N;s/\n/ /}' >> \$FILE_OUT.2
``````