Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use awk to print lines where a field matches a specific string?

Tags:

sed

awk

I have:

1 LINUX param1 value1 
2 LINUXparam2 value2
3 SOLARIS param3 value3
4 SOLARIS param4 value4

I need awk to print all lines in which $2 is LINUX.

like image 846
yael Avatar asked Jun 02 '10 11:06

yael


People also ask

How do I use awk to find a string?

Using Awk with set [ character(s) ] Take for example the set [al1] , here awk will match all strings containing character a or l or 1 in a line in the file /etc/hosts.

What is pattern matching in awk?

Any awk expression is valid as an awk pattern. The pattern matches if the expression's value is nonzero (if a number) or non-null (if a string). The expression is reevaluated each time the rule is tested against a new input record.

What is awk '{ print $3 }'?

txt. If you notice awk 'print $1' prints first word of each line. If you use $3, it will print 3rd word of each line.


6 Answers

In awk:

awk '$2 == "LINUX" { print $0 }' test.txt

See awk by Example for a good intro to awk.

In sed:

sed -n -e '/^[0-9][0-9]* LINUX/p' test.txt

See sed by Example for a good intro to sed.

like image 162
Hank Gay Avatar answered Oct 05 '22 18:10

Hank Gay


This is a case in which you can use the beautiful idiomatic awk:

awk '$2=="LINUX"' file

That is:

  • The default action of awk when in a True condition is to print the current line.
  • Since $2 == "LINUX" is true whenever the 2nd field is LINUX, this will print those lines in which this happens.

In case you want to print all those lines matching LINUX no matter if it is upper or lowercase, use toupper() to capitalize them all:

awk 'toupper($2)=="LINUX"' file

Or IGNORECASE with either of these syntaxs:

awk 'BEGIN {IGNORECASE=1} $2=="LINUX"' file
awk -v IGNORECASE=1 '$2=="LINUX"' file
like image 37
fedorqui 'SO stop harming' Avatar answered Oct 05 '22 18:10

fedorqui 'SO stop harming'


My answer is very late, but no one has mentioned:

awk '$2~/LINUX/' file
like image 21
enegue Avatar answered Oct 05 '22 18:10

enegue


Try these out:

egrep -i '^\w+ LINUX ' myfile

awk '{IGNORECASE=1}{if ($2 == "LINUX") print}' myfile

sed -ne '/^[0-9]* [Ll][Ii][Nn][Uu][Xx] /p' myfile

edit: modified for case insensitivity

like image 26
B Johnson Avatar answered Oct 05 '22 20:10

B Johnson


I think it might be a good idea to include "exact" and "partial matching" cases using awk ))

So, for exact matching:

OTHER_SHELL_COMMAND | awk '$2 == "LINUX" { print $0 }'

And for partial matching:

OTHER_SHELL_COMMAND | awk '$2 ~ /LINUX/ { print $0 }'
like image 43
Arsen Khachaturyan Avatar answered Oct 05 '22 20:10

Arsen Khachaturyan


In GNU sed case-insensitive matches can be made using the I modifier:

sed -n '/^[^[:space:]][[:space:]]\+linux[[:space:]]\+/Ip'

Will robustly match "linux", "Linux", "LINUX", "LiNuX" and others as the second field (after the first field which may be any non-whitespace character) and surrounded by any amount (at least one) of any whitespace (primarily space and tab, although you can use [:blank:] to limit it to strictly those).

like image 34
Dennis Williamson Avatar answered Oct 05 '22 18:10

Dennis Williamson