1838655 Members
3518 Online
110128 Solutions
New Discussion

Re: grep and AWK

 
SOLVED
Go to solution
Chern Jian Leaw
Regular Advisor

grep and AWK

HI
I have the file attached in this message. I would like to read the file contents. I would like to extract the entire record of the disk-names ($1 ) having the pattern wmtpe, as of:
/fs35/wmtpe211
/fs35/wmtpe212
/fs35/wmtpe213

from the file attached, and place them into another file.
I would like to have the following types of record in file_A with pattern wmtpe:
/fs35/wmtpe212 wmt 16.8516 2.6224
/fs35/wmtpe213 wmt 13.6721 6.5676
/fs35/wmtpe214 wmt 16.8516 11.4333

and the other disk-names without the pattern wmtpe to be in file_B:
/fs36/nwdv.apv.bctl wmt 17.890 6.7809
/fs36/nwdv.shark.bctl wmt 18.978 5.4678

I tried the following:
for i in `cat fileName`
do
grep -v wmtpe >> temp_file
done

I'm not sure how I could extract the disk names with pattern wmtpe with its entire record, and place them into a file_A, and have the remaining disk-name entries, with its entire records into file_B.

Could someone please help me out?

Thanks.
3 REPLIES 3
Tom Maloy
Respected Contributor
Solution

Re: grep and AWK

To match the pattern, use "grep". To match every BUT the pattern, use "grep -v". The "-v" inverts the match.

grep wmtpe filename > file_A
grep -v wmtpe filename > file_B

Tom
Carpe diem!
Sridhar Bhaskarla
Honored Contributor

Re: grep and AWK


grep "wmtpe" this_file >> file_A

grep -v "wmtpe" this_file >> file_b

Did I misunderstand your message?.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
S.K. Chan
Honored Contributor

Re: grep and AWK

You just have to be more specific with your grep statement. For example ..

$ grep \^/fs[1-9]*[0-9]/wmtpe fileName > file_A

..will match for all lines that begins with "/fs9>/wmtpe".

$ grep \^/fs[1-9]*[0-9]/nwdv fileName > file_B

..same explaination here except for "nwdv" in place of "wmtpe".