Operating System - Linux
1827868 Members
889 Online
109969 Solutions
New Discussion

what awk statement to use?

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

what awk statement to use?

Hi,

I have a file with 12 rows and I want to use awk to search a second file with that particular string:

file to search is pcm.out
file with data to be searched is disk.lst

so i am looking for a way so that data in disk.lst can be founf in pcm.out.

I was kinda thinking:

awk '$1 ~ /$0/ {print}' pcm.out disk.lst

this dont work ....

any help please?
hello
3 REPLIES 3
Dennis Handly
Acclaimed Contributor
Solution

Re: what awk statement to use?

You need to give an example of the two files.
You want to take each of the 12 lines in the first file and find a line in the second that has that line plus a few more fields?? (Or you want to find an exact match?)

If so you can use:
fgrep -f disk.lst pcm.out
Hein van den Heuvel
Honored Contributor

Re: what awk statement to use?

Does it have to be awk? (why?)
What constitues a match? anywhere on the line? First field? Leading whitespace optional?

Dennis shows the classic grep solution.

You can do something really similar, buf not the same, with awk.

create disk.lst with slashes surrounding the target strings:
/string1/
/string2/
:
/last-string/

now run as

awk -f disk.lst pgm.out.

What you really did here is write a minimalistics awk program with as many conditionals/expressions as there are lines and the expression being defaulted to {print $0}.

Note. For the awk program 'disk.lst', just like in the grep case, the pieces of string are regular expressions.
So a "." matched anything and so on.

hth,
Hein.

lawrenzo_1
Super Advisor

Re: what awk statement to use?

ok thanks chaps - I need to take the blinkers off as I knew this solution just couldnt think of it!!

Chris.
hello