Computing.Net > Forums > Unix > problems in grep inside awk

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

problems in grep inside awk

Reply to Message Icon

Name: theRBA
Date: November 29, 2004 at 18:40:45 Pacific
OS: sun
CPU/Ram: p4/512
Comment:

Hi,

I have the following:

File2: contain the following lines:
a^ aaa^aa aa^~
b^ bbb^bb bb^~
c^ ccc^cc cc^~
d^ dddd^dd dd^~

File1: contain the following lines:
b^ bbb^bb bb^~
c^ ccc^cc cc^~

I get File2 as input and I want to do as following:
for each line in file2 {
if (line exist in file1) print "!"line
>> output
else
print "+"line >> output
}

I work on tcsh script:
and this is what I wrote it:

cat File2 | awk '{if (grep -c $0 file1 == 0) print "- "$0 >> output
else print "! "$0 >> output

Expected output:
- a^ aaa^aa aa^~
! b^ bbb^bb bb^~
! c^ ccc^cc cc^~
- d^ dddd^dd dd^~

But I get:
! a^ aaa^aa aa^~
! b^ bbb^bb bb^~
! c^ ccc^cc cc^~
! d^ dddd^dd dd^~

Do you have any idea what's wrong?

Thanks,

Roy.

4



Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: November 30, 2004 at 07:20:53 Pacific
Reply:

You can run a shell command like grep from within awk, but you have to use the system function.  awk sees your grep command as several expressions: a non-existant variable named grep, etc.  So it is trying to compare a concatenated expression with zero, which in this case will always be false.

I can show you the system call if you want, but a much better way to do this is to keep it all within awk:

awk 'BEGIN {
while ((getline < "file1") > 0)
   file1[$0] = 1}

{if ($0 in file1)
    print "! " $0
 else
    print "- " $0
}' file2 > output


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: problems in grep inside awk

problem with grep in a for loop www.computing.net/answers/unix/problem-with-grep-in-a-for-loop/3967.html

problems with grep www.computing.net/answers/unix/problems-with-grep/6614.html

Facing problem in ftp................pl. www.computing.net/answers/unix/facing-problem-in-ftppl/3542.html