LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 07-09-2012, 09:41 PM   #1
fievetl
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Rep: Reputation: Disabled
SED merging line


Hi all,

I've been trying to merge several lines based on keyword on 2 different lines.

For example :
key1

key2

should become :
key1 key2

key1 can be found multiple time in the file, but I want to merge lines only when key2 is there 2 lines after.

It is pretty simple to do in vi :
:%s/key1\n\nkey2/key1 key2
but somehow it doesn't work with SED.

I've been surfing around the forum and all similar thread solution didn't work as well...

Any idea anyone ?
Thanks.
 
Old 07-09-2012, 11:24 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Sed reads and operates on lines of text. For 3 lines to be in the line buffer, You need to use the `N' command or add a line to the hold buffer with the `H' command.
 
Old 07-10-2012, 06:42 AM   #3
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 37
Perhaps something like this...
Code:
# Firstly, create a data file
$ cat >data.dat <<EOF
one
key1
two
key2
three
EOF

$ sed -n '/^key1/ {N; N; s/^\(key1[^\n]*\)\n[^\n]*\n\(key2[^\n]*\)$/\1 \2/p }' data.dat
key1 key2
Happy with ur solution... Tick "yes" and mark as Solved!

Last edited by dru8274; 07-10-2012 at 07:16 AM. Reason: Add
 
Old 07-10-2012, 07:10 AM   #4
fievetl
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks dru8274,

It helped. Now I can merge those three lines together, but somehow this is the only line in my result, now I need the rest of the file to remain untouched.
So I try to remove the search part /^key1/, but my result becomes empty.


Any idea ?
Thanks
 
Old 07-10-2012, 07:22 AM   #5
dru8274
Member
 
Registered: Oct 2011
Location: New Zealand
Distribution: Debian
Posts: 105

Rep: Reputation: 37
And howabout this?
Code:
sed '/^key1/ {N; N; s/^\(key1[^\n]*\)\n\([^\n]*\)\n\(key2[^\n]*\)$/\1 \3\n\2/ }' data.dat
Happy with ur solution... Tick "yes", and mark as Solved
 
Old 07-10-2012, 07:33 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
sed is not well-designed for multi-line editing. It normally takes one line at a time into its edit buffer, minus the newline, applies the given expressions to it, and then re-adds the newline to any output afterwards.

For something like this you have to tell it to grab and store multiple lines at once with the "N" command (or perhaps by use of the hold buffer). Only then will there be literal newlines in the buffer for you to work with.

You'll also need to process the lines in a loop if you need to catch multiple sequential occurrences, as it replaces the whole buffer after each operation.

See here for how to handle your situation.
http://sed.sourceforge.net/sedfaq4.html#s4.25


Here are a few more useful sed references:
http://www.grymoire.com/Unix/Sed.html
http://sed.sourceforge.net/grabbag/
http://sed.sourceforge.net/sedfaq.html
http://sed.sourceforge.net/sed1line.txt

The grymoire tutorial in particular will give you all the basics.
 
Old 07-10-2012, 08:18 AM   #7
fievetl
LQ Newbie
 
Registered: Jul 2012
Posts: 3

Original Poster
Rep: Reputation: Disabled
Smile

Wow, thanks dru8274,

I would have never found this on my own!
Problem solved

Appreciate it!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
Small Help required for merging two files per line!!! rockf1bull Red Hat 4 10-05-2011 08:06 AM
[Noob] Merging line of a file for consolidation... dzounar Linux - Newbie 2 09-27-2011 12:56 PM
merging content of two files using sed or awk mukherjee_debajyoti Programming 5 11-30-2009 05:43 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration