File Reading for a certain string and echo the line before


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File Reading for a certain string and echo the line before
# 8  
Old 03-09-2012
Quote:
Originally Posted by alister
Hi, ctsgnb:

Works fine for me with a BSD sed. Also, the script should be POSIX-compliant.

If you figure out why it fails on your implementation, I'd be interested to know; I'm not familiar with your system's userland.

Regards,
Alister

Some older seds don't play well without newlines. I was able to get it to work by inserting newlines.



Code:
sed -n '/Failed/{
x;
//!p;
x;
}; 
h' file


I was surprised that the 'one liner' didn't work with the version in xpg4 either.
This User Gave Thanks to agama For This Post:
# 9  
Old 03-10-2012
Quote:
Originally Posted by alister
Just because the problem has been solved does not mean that the fun must come to an end. Smilie
Code:
sed -n '/Failed/{x;//!p;x;}; h' infile

Regards,
Alister
Ingenious and fun indeed!

Quote:
Originally Posted by ctsgnb
@alister

I tried your code (on a SunOS 5.10 machine), it didn't work [..]
Were you using /usr/xpg4/bin/sed ?

Last edited by Scrutinizer; 03-10-2012 at 06:02 AM..
# 10  
Old 03-12-2012
@alister :

Hi

Just to give a feedback : i gave a try inserting new line (as suggested by agama) , i confirm your code works fine that way

Code:
# sed -n '/Failed/{x
> //!p
> x
> }
> h' my
call ttcacheautorefreshstatsget('CGOWNER','CG_CLIENT_PLAN');
#

@Scruti

I also gave a try with /usr/xpg4/bin/sed : it doesn't work one line ... it looks it still need to be run in several lines :

Code:
# /usr/xpg4/bin/sed -n '/Failed/{x;//!p;x;}; h' my










#

Code:
# # /usr/xpg4/bin/sed -n '/Failed/{x
> //# !p
> x
> }
> h' my
call ttcacheautorefreshstatsget('CGOWNER','CG_CLIENT_PLAN');
#


Last edited by ctsgnb; 03-12-2012 at 05:51 AM..
These 2 Users Gave Thanks to ctsgnb For This Post:
# 11  
Old 03-12-2012
Thanks for the feedback. I looked up the latest POSIX specification and I think indeed newlines should be used instead of semicolons...
Quote:
Historically, the sed ! and } editing commands did not permit multiple commands on a single line using a <semicolon> as a command delimiter. Implementations are permitted, but not required, to support this extension.
sed: rationale

There is also this passage:
Quote:
Command verbs other than {, a, b, c, i, r, t, w, :, and # can be followed by a <semicolon>, optional <blank> characters, and another command verb[..]
Editing Commands in sed

Last edited by Scrutinizer; 03-12-2012 at 06:16 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 12  
Old 03-12-2012
Although I already knew that those commands listed in your second quote should not be delimited by a semicolon, it's news to me that POSIX does not require it for the others.

Thank you very much for the POSIX enlightenment, Scrutinizer.

And thanks for reporting back, ctsgnb.

Regards,
Alister
# 13  
Old 03-12-2012
@Scruti & Alister :

Just to validate that this is the handling of the semicolon after the {} clause that caused the error, i gave a last shot :

see :

Code:
# /usr/xpg4/bin/sed -n '/Failed/{x;//!p;x;};h' my










#

Code:
# /usr/xpg4/bin/sed -n '/Failed/{x;//!p;x;}
> h' my
call ttcacheautorefreshstatsget('CGOWNER','CG_CLIENT_PLAN');
#

... worked !
These 3 Users Gave Thanks to ctsgnb For This Post:
# 14  
Old 03-12-2012
Nice job narrowing it down, ctsgnb.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

Echo multi-line string via heredoc syntax

$ cat bashtest #!/usr/local/bin/bash echo <<<"EOF" line1 line2 line3 EOF $ ./bashtest ./bashtest: line 3: line1: command not found ./bashtest: line 4: line2: command not found ./bashtest: line 5: line3: command not found ./bashtest: line 6: EOF: command not found What am i doing... (4 Replies)
Discussion started by: urello
4 Replies

3. Shell Programming and Scripting

Reading line by line from live log file using while loop and considering only those lines start from

Hi, I want to read a live log file line by line and considering those line which start from time stamp; Below code I am using, which read line but throws an exception when comparing line that does not contain error code tail -F /logs/COMMON-ERROR.log | while read myline; do... (2 Replies)
Discussion started by: ketanraut
2 Replies

4. Shell Programming and Scripting

Comparison of fields then increment a counter reading line by line in a file

Hi, i have a scenario were i should compare a few fields from each line then increment a variable based on that. Example file 989878|8999|Y|0|Y|N|V 989878|8999|Y|0|N|N|V 989878|8999|Y|2344|Y|N|V i have 3 conditions to check and increment a variable on every line condition 1 if ( $3... (4 Replies)
Discussion started by: selvankj
4 Replies

5. Shell Programming and Scripting

Reading text file, comparing a value in a line, and placing only part of the line in a variable?

I need some help. I would like to read in a text file. Take a variable such as ROW-D-01, compare it to what's in one line in the text file such as PROD/VM/ROW-D-01 and only input PROD/VM into a variable without the /ROW-D-01. Is this possible? any help is appreciated. (2 Replies)
Discussion started by: xChristopher
2 Replies

6. UNIX for Dummies Questions & Answers

Parsing file, reading each line to variable, evaluating date/time stamp of each line

So, the beginning of my script will cat & grep a file with the output directed to a new file. The data I have in this file needs to be parsed, read and evaluated. Basically, I need to identify the latest date/time stamp and then calculate whether or not it is within 15 minutes of the current... (1 Reply)
Discussion started by: hynesward
1 Replies

7. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

8. UNIX for Dummies Questions & Answers

Reading one line from a multiline string

Hi , I have a string which is of multiple line,I have to split the string by reading one line in each iteration and keep it in a string.I am keeping all the file names in side as string returned by the ls command ,I am stocked at the point of spliting the string to segragate each file. ... (4 Replies)
Discussion started by: Deekay.p
4 Replies

9. Shell Programming and Scripting

how to echo the file contents LINE BY LINE

hello, i have a listing (let say ABC) consists of the below: : public database link public synonym role rollback segment : when i run the below for loop, for i in `more ABC` do echo "$i" done it gives me, : public database (4 Replies)
Discussion started by: newbie168
4 Replies
Login or Register to Ask a Question