Print output of grep command in multuple lines


 
Thread Tools Search this Thread
Operating Systems AIX Print output of grep command in multuple lines
# 1  
Old 08-06-2012
Question Print output of grep command in multuple lines

HI All,

I am using grep command to serach a pattern in a list of files and storing the output in a variable. Then i am applying some logic on that variable to get the required output.
But Thing is that when the pattern is present mutiple times in a file, i am getting the output of grep in a single row and finally my logic is failing on that output.

for example
after excuting the command
Code:
a=`cat LU_SHC_LYLTY_CUST.sql | grep -i "^create temp table"`

Output
Code:
CREATE TEMP TABLE TEMP_SYWR_EMAIL_ID CREATE TEMP TABLE TEMP_JL_SYWR_NO_EMAIL AS CREATE TEMP TABLE TEMP_JL_CNM_EMAIL AS
CREATE TEMP TABLE TEMP_JL_SYWR_CNM_EMAIL AS CREATE TEMP TABLE TEMP_L_SYWR_CNM_EML_OPTIN AS.

Is there any way to store the output in different lines?
Code:
CREATE TEMP TABLE TEMP_SYWR_EMAIL_ID
CREATE TEMP TABLE TEMP_JL_SYWR_NO_EMAIL AS
CREATE TEMP TABLE TEMP_JL_CNM_EMAIL AS
CREATE TEMP TABLE TEMP_JL_SYWR_CNM_EMAIL AS
CREATE TEMP TABLE TEMP_L_SYWR_CNM_EML_OPTIN AS
CREATE TEMP TABLE LU_SHC_LYLTY_CUST_TMP1

Thanks
Goutam Sahoo
Moderator's Comments:
Mod Comment
Please use code tags when posting data and code samples!

Last edited by goutam sahoo; 08-06-2012 at 06:04 PM.. Reason: code tags, please!
# 2  
Old 08-06-2012
It seems like it would be better to process the lines as you find them rather than store them in a variable and parse the variable later, but if that is really what you want to do something like this should work:
Code:
#!/bin/ksh (or your favorite POSIX-conforming shell)
grep -i "^create temp table" LU_SHC_LYLTY_CUST.sql | (
	IFS=""
	a=
	while read line
	do
		a="$a$line
"
	done 
	printf "The result stored in a is:\n%s" "$a"
	exit 0
)

Note that if you don't put the while loop and the processing of the $a in a subshell, the value stored in $a may not be available once you exit the "grep | while read " pipeline.
# 3  
Old 08-06-2012
See also Useless Use of Backticks. The backticks are flattening all whitespace there.
# 4  
Old 08-07-2012
try this..

Code:
 
a=$(grep -i "^create temp table" LU_SHC_LYLTY_CUST.sql )
echo "$a"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Grep a pattern and print following n lines

Hi all, I am struck with the below requirement. I need to grep a particular pattern in a file and then print next n lines of it for further processing. I have used the below code grep -A 3 "pattern" filename But it is throwing error as below. grep: illegal option -- A Can... (14 Replies)
Discussion started by: ssk250
14 Replies

2. Shell Programming and Scripting

Use sed to print first n lines and last n lines of an output.

Use sed to print first n lines and last n lines of an output. For example: n=10 Can it be done? Thanks. (7 Replies)
Discussion started by: carloszhang
7 Replies

3. Shell Programming and Scripting

Grep and print next 10 Lines separated by ,

Hi All, I need to grep through a file for a string and print the next ten lines to a file separating the lines with a , and save it as a csv file to open it as a XL file. The 10 lines should be on a sigle row in xl. Any suggesstions please. Note; I dont have a GNU Grep to use -A flag. ... (6 Replies)
Discussion started by: Nani369
6 Replies

4. Shell Programming and Scripting

Print lines before and after..not grep -A

Hi I have this in my file 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11... (9 Replies)
Discussion started by: zorrox
9 Replies

5. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

6. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

7. Shell Programming and Scripting

Help required on grep command(Skip the first few lines from printing in the output)

Hi experts I want the proper argument to the grep command so that I need to skip the first few lines(say first 10 lines) and print all the remaining instances of the grep output. I tried to use grep -m 10 "search text" file*. But this gives the first 10 instances(lines) of the search string.... (7 Replies)
Discussion started by: ks_reddy
7 Replies

8. Shell Programming and Scripting

print multiple lines using the grep command.

Hi All, Please find my piece of code below. I am trying to grep the word SUCCESS from $LOGFILE and storing in the grepvar variable. And i am placing that variable in a file. Now if i open the file, i can see the four lines but not in seperate four line s but in a paragraph. If am mailing that log... (8 Replies)
Discussion started by: intiraju
8 Replies

9. Shell Programming and Scripting

Print lines after grep

Hi all, I need help in following scenario. I have a file with about 10,000 lines. There are several lines which have word "START" (all upper case) in them. I want to grep line with word "START" and then do the following 1. Print the line number having word "START" 2. Print the next 11 lines. ... (4 Replies)
Discussion started by: jakSun8
4 Replies
Login or Register to Ask a Question