awk get matched line's previous line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk get matched line's previous line
# 8  
Old 08-16-2010
Quote:
Originally Posted by Franklin52
Works fine on my HP-UX system, try <Ctrl-v><Enter> instead of \n:
Code:
sed -n 'N;/in/s/^M.*//p'

What does the capital "N" do?

---------- Post updated at 04:54 PM ---------- Previous update was at 04:42 PM ----------

@Franklin,
If this is the input file:
Code:
c
a
b in
d
e
f in
g

Now run your sed command.
# 9  
Old 08-16-2010
Ok, that was just "freehand" command...Smilie try it with:
Code:
sed -n -e '/in/ !{x;d;}' -e '/in/{x;p;x;}' file

or with this approach:
Code:
sed -n -e '/in/ !{h;}' -e '/in/ {H;x;s/\n.*//p;}' file

# 10  
Old 08-18-2010
Quote:
Originally Posted by Franklin52
Ok, that was just "freehand" command...Smilie try it with:
Code:
sed -n -e '/in/ !{x;d;}' -e '/in/{x;p;x;}' file

or with this approach:
Code:
sed -n -e '/in/ !{h;}' -e '/in/ {H;x;s/\n.*//p;}' file

Could you please explain each command option?
I did man sed but it's not quite clear to me.
# 11  
Old 08-19-2010
Quote:
Originally Posted by cola
Could you please explain each command option?
I did man sed but it's not quite clear to me.
Ok, here we go, the first one:
Code:
sed -n -e '/in/ !{x;d;}' -e '/in/{x;p;x;}' file

Code:
/in/ !{x;d;}	# Line doesn't contain "in", exchange the pattern space with the hold buffer 
		# and delete the pattern space

/in/{x;p;x;}	# Line contains "in", exchange the pattern space with the hold buffer,
		# print the pattern space and exchange the pattern space with the hold buffer

The second approach:
Code:
sed -n -e '/in/ !{h;}' -e '/in/ {H;x;s/\n.*//p;}' file

Code:
/in/ !{h;}		# Line doesn't contain "in", copy the pattern buffer into the hold buffer

/in/ {H;x;s/\n.*//p;}	# Line contains "in", append the pattern space to the buffer, with a "\n" between the lines,
			# exchange the pattern space with the hold buffer, remove the last line and print the line

If this is abracadabra to you, you can have a read of a sed tutorial:

Sed - An Introduction and Tutorial


Regards
This User Gave Thanks to Franklin52 For This Post:
# 12  
Old 08-19-2010
Quote:
Originally Posted by Franklin52
Ok, here we go, the first one:
Code:
sed -n -e '/in/ !{x;d;}' -e '/in/{x;p;x;}' file

Code:
/in/ !{x;d;}    # Line doesn't contain "in", exchange the pattern space with the hold buffer 
        # and delete the pattern space

/in/{x;p;x;}    # Line contains "in", exchange the pattern space with the hold buffer,
        # print the pattern space and exchange the pattern space with the hold buffer

The second approach:
Code:
sed -n -e '/in/ !{h;}' -e '/in/ {H;x;s/\n.*//p;}' file

Code:
/in/ !{h;}        # Line doesn't contain "in", copy the pattern buffer into the hold buffer

/in/ {H;x;s/\n.*//p;}    # Line contains "in", append the pattern space to the buffer, with a "\n" between the lines,
            # exchange the pattern space with the hold buffer, remove the last line and print the line

If this is abracadabra to you, you can have a read of a sed tutorial:

Sed - An Introduction and Tutorial


Regards
Thanks for your explanation.
Both commands are practical and easy.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

2. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

3. Shell Programming and Scripting

awk script -print line when $2 > $2 of previous line

Hi all, From a while loop I am reading a sorted file where I want to print only the lines that have $1 match and $2 only when the difference from $2 from the previous line is > 30. Input would be like ... AN237 010 193019 0502 1 CSU Amoxycillin AN237 080 ... (2 Replies)
Discussion started by: gafoleyo73
2 Replies

4. Shell Programming and Scripting

sed: how to move matched pattern to end of previous line

Hello, I'm new to this forum. I've been doing a lot of sed work lately and have found many useful tips on this forum. I've hit a roadblock in a project, though, and could really use some help. I have a text file with many lines like the following, i.e., some lines begin with a single word... (3 Replies)
Discussion started by: paroikoi
3 Replies

5. Shell Programming and Scripting

Use AWK to move matched line back one?

Can somebody help me with this? I'm sure it's a no-brainer if you know awk... but I don't. Input: Blah Blah Me love you long time Blah Blah awk magic with 'long time' ==> Output: Blah Blah Me love you long time (0 Replies)
Discussion started by: Ryan.
0 Replies

6. Shell Programming and Scripting

AWK Print Line If Specific Character Is Matched

Hello, I have a file as such: FFFFFFF6C000000 225280 225240 - - rwxs- FFFFFFFF79C00000 3240 3240 - - rwxs- FFFFFFFF7A000000 4096 4096 - - rwxs- FFFFFFFF7A400000 64 64 ... (3 Replies)
Discussion started by: PointyWombat
3 Replies

7. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

8. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

9. Shell Programming and Scripting

SED or AWK "append line to the previous line"

Hi, How can I remove the line beak in the following case if the line begin with the special char “;”? TEXT Text;text ;text Text;text;text I want to convert the text to: Text;text;text Text;text;text I have already tried to use... (31 Replies)
Discussion started by: research3
31 Replies

10. Shell Programming and Scripting

AWK - Extracting matched line

Hi all, I have one more query related to AWK. I have the following csv data: ,qwertyA, field1, field2, field3, field4, field5, field6 ,,,,,,,,,,,,,,,,,,,100,200 ,,,,,,,,,,,,,,,,,,,300,400 ,qwertyB, field1, field2, field3, field4, field5, field6 ,,,,,,,,,,,,,,,,,,,100,200... (9 Replies)
Discussion started by: not4google
9 Replies
Login or Register to Ask a Question