Honored Contributor Honored Contributor
Re: Grep in AWK
Praveen,

You can use regular expressions in awk statements (instead of calling grep thru a 'system' call), like in this example:

$ echo "hello world" | awk '$1 ~ /[Hh]ello/ '{print $1}'
hello

knowing that $0 represents the entire string "hello world", which can be separated into multiple substrings (hello=$1, world=$2). Substrings are determined by delimiter specified by the -F flag of awk (space by default).

--

To retrieve each line of an input file, and do some processing afterwards (by splitting lines into multiple fields according to some patterns), you could issue:

# do some processing

cat myfile | awk 'BEGIN { while (getline) {
print $0
# do some further processing, knowing that each line is stored in $0
} # end while
}'

If you are used to Perl, then awk should'nt be a pb for you.

I would recommend the "sed & awk" book, from Dale Dougherty & Arnold Robbins, published by O'Reilly (ISBN 1-56592-225-5), which will show you all the powerful features of awk.

I hope this helps.

Bests,

Fred.