Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 10-31-2013, 12:49 PM   #1
LQ Newbie
Registered: May 2012
Posts: 24

Rep: Reputation: Disabled
How to append a pattern to the previous line?

Hello everyone,

I have a file like this
[1] "test1"
[2] "test2"
[3] "test3"

[1] "test11"
[2] "test21"
I want to append the lines that start with [] to a previous line that doesn't start with []. My result file would look something like this

$`c1` [1] "test1" [2] "test2" [3] "test3"
$`c2` [1] "test11" [2] "test21"
Any suggestions of how to do this with sed/awk?

Old 10-31-2013, 01:35 PM   #2
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 97
Originally Posted by jv61 View Post
Any suggestions of how to do this with sed/awk?

In Perl, you could do this:

perl -ne 'BEGIN {$f = 0}; chomp; print "\n" if (/^[^[]/ and $f); print "$_ "; $f = 1; END {print "\n"}' filename
Or, if you don't care about an initial blank line:

perl -ne 'chomp; print "\n" if (/^[^[]/); print "$_ "; END {print "\n"}' filename

Last edited by Robhogg; 10-31-2013 at 01:39 PM.
1 members found this post helpful.
Old 10-31-2013, 01:56 PM   #3
LQ Newbie
Registered: May 2012
Posts: 24

Original Poster
Rep: Reputation: Disabled
Thank you, that solved the problem


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
how can i append the text after pattern match using variable in sed ravikushal Linux - Newbie 18 07-23-2013 08:56 AM
[SOLVED] Trying to number every other line and append those numbers to end of line kmkocot Programming 7 04-23-2010 11:17 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
Attempting to append a line of text to the end of the previous line market_garden Linux - General 4 12-11-2008 11:37 AM
Unix command to delete the previous and next line of the searched pattern navin29 Linux - Newbie 7 03-14-2008 01:23 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:49 PM.

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