Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 >> outputExpected 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

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

![]() |
![]() |
![]() |

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