LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to append a pattern to the previous line? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-append-a-pattern-to-the-previous-line-4175482941/)

jv61 10-31-2013 12:49 PM

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

Robhogg 10-31-2013 01:35 PM

Quote:

Originally Posted by jv61 (Post 5055999)
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

jv61 10-31-2013 01:56 PM

Thank you, that solved the problem


All times are GMT -5. The time now is 06:03 PM.