LinuxQuestions.org
Help answer threads with 0 replies.
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 09-11-2012, 07:26 AM   #1
masavini
Member
 
Registered: Jun 2008
Posts: 285

Rep: Reputation: 6
bash: merge lines if string is found...


i have a file like this:

Code:
ext1
return gigi
ext2
ext4
ext3
return bubu

i need lines starting with "return" to be merged with the previous one:

Code:
ext1 return gigi
ext2
ext4
ext3 return bubu
how to get it with bash?
i tried with:

Code:
sed -e :a -e '$!N; s/\nreturn/ return/; ta' file.txt
but only the first occurrence is processed... can you help me?

thanks!
 
Old 09-11-2012, 07:33 AM   #2
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
solved with:
Code:
awk '!/^return/ { if (line) print line; line = $0; next } { sub(/^[ \t]+/, " "); line = line $0 } END { print line }' file.txt
 
Old 09-11-2012, 08:42 AM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,879

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by masavini View Post
i tried with:
Code:
sed -e :a -e '$!N; s/\nreturn/ return/; ta' file.txt
but only the first occurrence is processed... can you help me?
Your sed was close. Try this ...
Code:
sed -e :a -e '$!N; s/\nreturn/ return/;ta' -e 'P;D' $InFile
Daniel B. Martin

Last edited by danielbmartin; 09-11-2012 at 08:43 AM. Reason: Cosmetic improvement
 
Old 09-11-2012, 09:20 AM   #4
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
Quote:
Code:
sed -e :a -e '$!N; s/\nreturn/ return/;ta' -e 'P;D' $InFile
you right... it works!
thanks
 
Old 09-11-2012, 10:37 AM   #5
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
FYI, you can read how to do it with sed here:

http://sed.sourceforge.net/sedfaq4.html#s4.26

As you can see, sed isn't really well-designed for multiple-line editing, particularly when it involves moving "back" in the file. The usual workflow for it is one-way.


Incidentally, this can also be done with ed fairly easily.

Code:
ed -s file.txt <<HEREDOC
#start global command list -- find lines starting with "return", move back one, and
#add a space to the end. final backslash means the "g" setting continues to the next command.
g/^return/-1 s/$/ /\'
#join the current line (the one just modified above) with the one after it (end of global command list).
.,+1j
#print the altered buffer to stdout.
%p
#write the buffer back to the original file.
w
#write the buffer to a new file.
w newfile.txt
HEREDOC
I used the heredoc format so I could add comment lines to the script, but any technique that feeds the commands to stdin separated by linefeeds will work. e.g., more compactly:

Code:
printf '%s\n' 'g/^return/-1 s/$/ /\' '.,+1j' '%p' | ed -s file.txt
One limitation, however, that it will error out if "return" ever happens to be the first line of the file.

How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)
 
Old 09-11-2012, 11:29 PM   #6
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Ugly solution. REMOVED.

Last edited by firstfire; 09-11-2012 at 11:46 PM.
 
Old 09-13-2012, 12:04 PM   #7
masavini
Member
 
Registered: Jun 2008
Posts: 285

Original Poster
Rep: Reputation: 6
Quote:
Originally Posted by firstfire View Post
Ugly solution. REMOVED.
what does it mean?
 
Old 09-13-2012, 01:38 PM   #8
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

That means that the solution I suggested in that post (there were 2 of them actually) turned out to be ugly and useless when I reread the thread once again. There are no simple way to remove wrong post, so I replaced its contents with that phrase.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] Bash script to merge files together (given as a comma separated string) DomeKor Linux - Newbie 10 09-27-2011 11:29 PM
Merge Two Lines in perl ankitjainfsl Programming 1 09-14-2011 02:57 AM
[SOLVED] Sed, Awk, Perl - Merge lines unless they match a certain string Dsw0002 Programming 5 04-16-2011 05:20 AM
Merge lines from a file lourencojunior Linux - Newbie 5 05-12-2008 01:54 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM

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

All times are GMT -5. The time now is 03:28 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