LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-31-2010, 04:32 AM   #1
Eddie Adams
LQ Newbie
 
Registered: Sep 2007
Posts: 22

Rep: Reputation: 0
sed move to prev line if match


file:
12312
desc: bleh...
9938
desc: blah...
iueud
desc: bloh...

desired result:
12312, desc: bleh...
9938, desc: blah...
iueud, desc: bloh...
 
Old 01-31-2010, 05:05 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
$ awk 'ORS=(NR%2)?",":"\n"' file
12312,desc: bleh...
9938,desc: blah...
iueud,desc: bloh...
 
Old 01-31-2010, 06:20 AM   #3
Eddie Adams
LQ Newbie
 
Registered: Sep 2007
Posts: 22

Original Poster
Rep: Reputation: 0
Thanks!

Ok that fixes it for a format where the lines are always paired. That is not always...

file:
12312
desc: bleh...
9938
desc: blah...
iueud
desc: bloh...
desc: blih...
desc: bluh...
9912
desc: blah...

So it must look at the first characters [desc:] and only these lines must be moved to the previous line

desired output:
12312 desc: bleh...
9938 desc: blah...
iueud desc: bloh...
desc: blih...
desc: bluh...
9912 desc: blah...
 
Old 01-31-2010, 06:28 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
then why don't show your actual data structure in the first place?
Code:
awk '!/desc:/{
 printf "%s ",$0
 getline
 printf "%s \n",$0
}
/desc/{print}' file

Last edited by ghostdog74; 01-31-2010 at 06:32 AM.
 
Old 01-31-2010, 06:42 AM   #5
Eddie Adams
LQ Newbie
 
Registered: Sep 2007
Posts: 22

Original Poster
Rep: Reputation: 0
I presumed the initial answer would check the first characters of the line. Well its working now. Thanks again.
 
  


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
Insert line on match only once with sed? lhouk Programming 38 08-10-2010 09:23 AM
quick sed question - how do you add a line after a match? BrianK Programming 2 09-16-2009 07:22 PM
[SOLVED] move a line with either sed/awk/perl script don_wombat_73 Linux - Newbie 9 07-07-2009 01:06 PM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 01:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:56 PM.

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