AWK/Shell script for reading text file

Hello,

I have a text file which has list of SQL Commands, from where I have grepped the lines where there is a specific string:

grep <string> <file1> >> <file2>

A sample of the file2 is:

INSERT INTO PS_PWC_SP_ERN_DATA SELECT A.EMPLID ,B.COMPANY ,B.PAYGROUP ,
  B.OTH_PAY FROM PS_PAY_EARNINGS A , PS_PAY_OTH_EARNS B WHERE A.COMPANY =
  MAX(PAY_END_DT) FROM PS_PAY_OTH_EARNS WHERE COMPANY = B.COMPANY AND
 PS_REVW_RATING_TBL RR WHERE RR.EFFDT = ( SELECT MAX(A_ED.EFFDT) FROM
  PS_REVW_RATING_TBL A_ED WHERE RR.RATING_MODEL = A_ED.RATING_MODEL AND

Now, my task is to list all the words in file2 which starts with PS_. The problem is, these words can be placed anywhere in the lines in file2.

Can someone suggest an AWK/Shell script to do so?
For any queries, kindly let me know.
Thanks.

Hi

$ grep -o 'PS_[^ ]*' file2
PS_PWC_SP_ERN_DATA
PS_PAY_EARNINGS
PS_PAY_OTH_EARNS
PS_PAY_OTH_EARNS
PS_REVW_RATING_TBL
PS_REVW_RATING_TBL

Guru.

Thanks for the above suggestions, Guru, but it is failing with below error:

grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .

I am using the below OS:

SunOS 5.10 Generic_144488-17 sun4u sparc SUNW,SPARC-Enterprise

Please advice.

Your grep does not have '-o' option. Try this:

$ tr -s " " "\n" < file2 | grep PS_
PS_PWC_SP_ERN_DATA
PS_PAY_EARNINGS
PS_PAY_OTH_EARNS
PS_PAY_OTH_EARNS
PS_REVW_RATING_TBL
PS_REVW_RATING_TBL

Guru.

1 Like

Perl.

perl -ne 'while(/(PS_\S+)/g){print "$1\n"}' file2
1 Like

Thanks to both of you.
I have not tried with the perl command, but the TR command worked perfectly in my case.
Many thanks, once again!

@guruprasadpr-:
Can you please explain
grep -o 'PS_[^ ]*' file2

grep, by default, prints the entire line which matches the pattern. -o option tells only to print the pattern alone which matched, instead of the entire line.

PS_[^ ]* : means PS_ followed by a series of characters other than space. In other words, it means to stop once the space is encountered which, for us, indicates the end of the word.

Guru.