AWK/Shell script for reading text file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK/Shell script for reading text file
# 1  
Old 01-25-2012
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:
Code:
grep <string> <file1> >> <file2>

A sample of the file2 is:
Code:
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.

Moderator's Comments:
Mod Comment How to use code tags

Last edited by Franklin52; 01-25-2012 at 03:58 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 01-25-2012
Hi
Code:
$ 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.
# 3  
Old 01-25-2012
Thanks, but giving error ...

Thanks for the above suggestions, Guru, but it is failing with below error:
Code:
grep: illegal option -- o
Usage: grep -hblcnsviw pattern file . . .

I am using the below OS:
Code:
SunOS 5.10 Generic_144488-17 sun4u sparc SUNW,SPARC-Enterprise

Please advice.

Last edited by Franklin52; 01-25-2012 at 03:58 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 01-25-2012
Your grep does not have '-o' option. Try this:


Code:
$ 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.
This User Gave Thanks to guruprasadpr For This Post:
# 5  
Old 01-25-2012
Perl.

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

This User Gave Thanks to balajesuri For This Post:
# 6  
Old 01-25-2012
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!
# 7  
Old 01-25-2012
@guruprasadpr-:
Can you please explain
grep -o 'PS_[^ ]*' file2
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Reading XML file and print the values in the text file using Linux shell script

hi guys, i want help... Reding XML file and print the values into the text file using linux shell script file as per below xml file <sequence> <Filename>aldorzum.doc</Filename> <DivisionCode>US</DivisionCode> <ContentType>Template</ContentType> <ProductCode>VIMZIM</ProductCode> </sequence>... (1 Reply)
Discussion started by: sravanreddy
1 Replies

2. Shell Programming and Scripting

Creating new users using a text file as imput (using only shell script and/or awk)

I need somebody who could help with an exercise. You have a text file called users.txt with this info inside: users.txt: user1:1234:/home/homedir1 ; user2:1234:/home/homedir2 ; user3:1234:/home/homedir3 ; user4:1234:/home/homedir4 ; The script should create an user using the... (2 Replies)
Discussion started by: marcosruiz
2 Replies

3. UNIX for Dummies Questions & Answers

C-Shell script help reading from txt file

I need to write a C-Shell script with these properties: It should accept two arguments on the command line. The first argument is the name of a file which contains a list of names, and the second argument is the name of a directory. For each file in the directory, the script should print the... (1 Reply)
Discussion started by: cerce
1 Replies

4. Programming

awk processing / Shell Script Processing to remove columns text file

Hello, I extracted a list of files in a directory with the command ls . However this is not my computer, so the ls functionality has been revamped so that it gives the filesizes in front like this : This is the output of ls command : I stored the output in a file filelist 1.1M... (5 Replies)
Discussion started by: ajayram
5 Replies

5. Shell Programming and Scripting

Reading a word from a text file into shell script

Hi, I am new to shell programming.I need to write a script that would accept a word from each line fo an input text file.Can anyone help me with this?Exact requirement: word1 word2 word3 (separated by space) .Now I need word3 from each such line in the text file. Thanks in Advance, Manish (3 Replies)
Discussion started by: manish007
3 Replies

6. Shell Programming and Scripting

Reading the Properties File From Shell script

Hi, I am new to the shell script please I need help for following question. I have properties file name called "com.test.properties" I have No of key values in this properties. com.person.name = xyz com.person.age = 55 com.person.address = hello I want read this properties but i... (1 Reply)
Discussion started by: venukjs
1 Replies

7. Shell Programming and Scripting

File reading problem via shell script

Hi, Data file named parameter contains : DB=y Alter_def.sql Create_abc.sql SQL=y database.sql my_data.sql To read this file I use var_sql=$(awk -F= '$1 == "SQL" { print $2 }' parameter.txt) if then sql_f_name=`grep "\.sql" parameter.txt` echo $sql_f_name fi (2 Replies)
Discussion started by: Dip
2 Replies

8. Shell Programming and Scripting

file reading through shell script

For reading a file through shell script I am using yhe code : while read line do echo $line done<data.txt It reads all the line of that file data.txt. Content of data.txt looks like: code=y sql=y total no of sql files=4 a.sql b.sql c.sql d.sql cpp=n c=y total no of c files=1 (4 Replies)
Discussion started by: Dip
4 Replies

9. Shell Programming and Scripting

Reading file names from a file and executing the relative file from shell script

Hi How can i dynamically read files names from a list file and execute them from a single shell script. Please help its urgent Thanks in Advance (4 Replies)
Discussion started by: anushilrai
4 Replies
Login or Register to Ask a Question