Register
It is currently Sat Oct 25, 2014 4:05 pm

Help Searching for String and Moving to end of Previous Line


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Thu Nov 10, 2011 1:45 pm   

Joined: Mon May 31, 2010 7:40 pm
Posts: 25
I am embarrassed having to ask for help with this issue, but I don't know sed well enough.

If a string is matched then I want to move it to the end of the previous line and a space in between.

Code:
Linux
2

Linux
3


I want it to result in
Linux 2
Linux 3

I have been looking at the sed on liners but don't know it well enough. I will continue testing.

http://sed.sourceforge.net/sed1line.txt

Thanks!


Top
 Profile  
 PostPosted: Thu Nov 10, 2011 6:27 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
Like this?
Code:
sed '/Linux/{N;s/\n/ /}'


Top
 Profile  
 PostPosted: Thu Nov 10, 2011 6:58 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
canit0 wrote:
If a string is matched [...]


Which string?

At first, I thought that you were talking about the string « Linux », as does choroba,
but then the rest of your sentence confused me.

----

Let's try to guess...

Assuming you name:
Code:
Linux
2

a string, and want to have:
Code:
Linux 2


then:
Code:
sed ':loop; N; $! b loop; /^$/! s/\n/ /g; s/  /\n/' file


Which is easier to understand in awk(1):
Code:
awk 'NF { l = $0; getline; print l, $0 }' file


Top
 Profile  
 PostPosted: Sun Nov 13, 2011 3:09 pm   

Joined: Mon May 31, 2010 7:40 pm
Posts: 25
Thank you both. I will be giving this a try.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 12 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP