1748110 Members
4000 Online
108758 Solutions
New Discussion юеВ

Re: Grep in AWK

 
SOLVED
Go to solution
Praveen Bezawada
Respected Contributor

Grep in AWK

Hi
Can we use grep inside an awk statement ?
Praveen
12 REPLIES 12
Sandor Horvath_2
Valued Contributor

Re: Grep in AWK

Hi !

Insite awk You can call system() function. In system You can run grep.
I'm not sure it good for You .

Why do You want to do ?

regards, Saa
If no problem, don't fixed it.
Dan Hetzel
Honored Contributor

Re: Grep in AWK

Hi Praveen,

AWK has a very useful string search feature which could possibly make the use of external programs like grep useless.

If you want to use grep anyway, you can do it through the use of the system(cmd) call.

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Praveen Bezawada
Respected Contributor

Re: Grep in AWK

I need to read lines in one file and for some particular fields in that line check if they are present in a songle line of another file.

So I want to awk the first file , get the fields and grep in the second file.
Frederic Soriano
Honored Contributor
Solution

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.
Praveen Bezawada
Respected Contributor

Re: Grep in AWK

I can get the individual fields but after that I need to search for them in another file.
So I need to use something similar to grep. isn't it ???
Frederic Soriano
Honored Contributor

Re: Grep in AWK

Hi again,

What you could do is something like that:

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

This is quick and dirty, but this should do the trick.

Bests !

Fred.
Praveen Bezawada
Respected Contributor

Re: Grep in AWK

Can I have the syntax of using system in awk
Frederic Soriano
Honored Contributor

Re: Grep in AWK

Praveen,

It's as Dan and Saandor told you:
system(cmd)
eg:
system ("mkdir test")

But be warned that the system function, as implemented in awk, does not make the output of the command available within the program for processing. " It returns the exit status of the command that was executed. The script waits for the command to finish before continuing execution". (excerpt of "sed & awk" book)

Hope this helps.

Regards,

Fred.
Gregor Weertman
New Member

Re: Grep in AWK

Ok, we will tell you.

#files a:
#-rwxr-xr-x 1 root sys 252 Dec 18 09:23 mboxhwint.sh
#drwxr-xr-x 2 root sys 96 Mar 31 1998 meminfo
#-rwxr-xr-x 1 root sys 238 Aug 10 2000 mkhwtext.sh
#-rw------- 1 root sys 1331 Dec 28 13:11 nohup.out
#-rwxr-xr-x 1 root sys 80 Aug 14 10:47 pp.bat
#-rw-r--r-- 1 root sys 4311 Dec 6 1999 swlist.txt
#drwxr-xr-x 3 root sys 96 Aug 25 1998 tscript
#-rw-rw-rw- 1 root sys 569 Nov 11 1999 whichchar.c
#
#file b:
#drwxr-xr-x 2 root sys 96 Mar 31 1998 meminfo
#drwxr-xr-x 3 root sys 2048 Jan 25 10:22 script
#dr-xr-xr-x 2 root sys 1024 Oct 10 1997 script.old
#drwxr-xr-x 3 root sys 96 Aug 25 1998 tscript
#
#
#Result:
#drwxr-xr-x 2 root sys 96 Mar 31 1998 meminfo
#drwxr-xr-x 3 root sys 96 Aug 25 1998 tscript

#!/usr/bin/sh
awk 'BEGIN{ while (getline < "b"){
ii++
patt=tolower( $NF)
f1[ii] = $NF
}
}
{
line=tolower( $0)
for( jj = i; jj <= ii; jj++) if( index( $0, f1[ jj])) {print $0; next}
}' a

succes
Keep it simple, it really works.