tags:

views:

174

answers:

3

I have

1 LINUX param1 value1

2 LINUXparam2 value2

3 SOLARIS param3 value3

4 SOLARIS param4 value4

need by awk to pring all lines that $2 is LINUX THX

A: 

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

B Johnson
The question explicitly mentions `awk` (and `sed` in a comment).
Hank Gay
what about LINUX can be linux or Linux ?
yael
+2  A: 

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.

Hank Gay
what about LINUX can be linux or Linux ?
yael
The pages I linked to explain about how to use regexes in the respective tools. The regex to match LINUX or Linux is L(INUX|inux) (roughly speaking; different tools sometimes have slightly different syntax). BTW, if this is homework, it should be tagged as such.
Hank Gay
If you want the match to be completely case insensitive, check out the `toupper` (or `tolower`) functions in `awk`.
Hank Gay
Note that the default action already is `print $0`, so this is all that's required: `awk 'toupper($2)=="LINUX"' test.txt`
glenn jackman
+1  A: 

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).

Dennis Williamson