Re: Grep in AWK (814 Views)
Reply
Respected Contributor
Praveen Bezawada
Posts: 310
Registered: ‎09-13-2000
Message 1 of 13 (814 Views)
Accepted Solution

Grep in AWK

Hi
Can we use grep inside an awk statement ?
Praveen
Please use plain text.
Valued Contributor
Sandor Horvath_2
Posts: 132
Registered: ‎06-04-1997
Message 2 of 13 (814 Views)

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.
Please use plain text.
Honored Contributor
Dan Hetzel
Posts: 926
Registered: ‎07-25-2000
Message 3 of 13 (814 Views)

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
Please use plain text.
Respected Contributor
Praveen Bezawada
Posts: 310
Registered: ‎09-13-2000
Message 4 of 13 (814 Views)

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.
Please use plain text.
Honored Contributor
Frederic Soriano
Posts: 107
Registered: ‎07-06-1998
Message 5 of 13 (814 Views)

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.
Please use plain text.
Respected Contributor
Praveen Bezawada
Posts: 310
Registered: ‎09-13-2000
Message 6 of 13 (814 Views)

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 ???
Please use plain text.
Honored Contributor
Frederic Soriano
Posts: 107
Registered: ‎07-06-1998
Message 7 of 13 (814 Views)

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.
Please use plain text.
Respected Contributor
Praveen Bezawada
Posts: 310
Registered: ‎09-13-2000
Message 8 of 13 (814 Views)

Re: Grep in AWK

Can I have the syntax of using system in awk
Please use plain text.
Honored Contributor
Frederic Soriano
Posts: 107
Registered: ‎07-06-1998
Message 9 of 13 (814 Views)

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.
Please use plain text.
Occasional Visitor
Gregor Weertman
Posts: 4
Registered: ‎03-14-1999
Message 10 of 13 (814 Views)

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.
Please use plain text.
Occasional Visitor
Anantha Ganiga
Posts: 1
Registered: ‎08-10-2006
Message 11 of 13 (814 Views)

Re: Grep in AWK

Hi Praveen,
The same problem I am also encountered.
And I am also searching for the same solution.
the last solution you got seem to be a good one but it is not working for me and I could not able to understand
f1[ii] = $NF
If something worked for you then kindly post it to me.
Thanks advanced,
Regards,
Anantha Ganiga
Please use plain text.
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 12 of 13 (814 Views)

Re: Grep in AWK

Hi,

the assignment of the arry elements should read as
f1[ii] = $NF

The whole script I would change to
- a pure awk script
- not ignoring case sensivity
- include some flexibility

file get_bs_of_a.awk:
#!/usr/bin/awk
BEGIN{ while (getline < "b") f1[++ii] = $NF}
found_in_a { mypatt=$0
for( j=1; jj <=ii; jj++) if(index(mypatt, f1[jj]) {print "in a: "$0,"in b: "f1[j]"}

execute as
get_bs_of_a.awk a


What is done:
In the BEGIN statement you once collect the content of file "b" in an array - i.e. the file you lookup for matching pattern of file "a" is processed only once. If you need only a part of the file or of a line, assign the required value only.

found_in_a
is your condition for matching lines of "a" you want to lookup in "b"; it could be something like
/mypattern/
$3 == "wow"
NF == 42
or you just leave it to get every line.

mypatt=
this selects the string you want to 'grep' for in "b";
mypatt=$0 searches the whole line
mypatt=$3"-"$4"-"$5" searches for '-'seperated values of field 3 to 5.

index function
if you do not need pattern for matching but a simple string, index() is your friend. If you need a regexp, use the match operator "~" or the match() function.


mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Please use plain text.
Valued Contributor
rmueller58
Posts: 851
Registered: ‎02-19-2001
Message 13 of 13 (814 Views)

Re: Grep in AWK

I do something pretty simple.

if I am looking for a particular string in a field, I do the following.

This example looks at output from ONSTAT and prints output. In this example I filter out data prior to running it against AWK as it reduces processing time by doing so.


onstat -u |grep -v informix|grep -v total |awk '{print $9, $4, $5, $10, $11}' |sort -n |tail -10

Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation