LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 10-31-2013, 01:49 PM   #1
jv61
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
Code:
$`c1`
[1] "test1"
[2] "test2"
[3] "test3"


$`c2`
[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

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

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

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

In Perl, you could do this:

Code:
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:

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

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

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


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


All times are GMT -5. The time now is 06:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration