Alberto
Alberto

Reputation: 704

Merge pattern line/s with line before

I need to print all lines merging the lines starting with a white space "^ " with the line/s before. Using awk or sed would be perfect.

From:

ext_bus     3  0/1/1/0/1/1    c8xx           CLAIMED
                             /dev/c8xx3
target      4  0/1/1/0/1/1.0  tgt            CLAIMED
disk        4  0/1/1  sdisk          CLAIMED
                             /dev/c3t0   /dev/c2t0
                             /dev/c4t0

To:

ext_bus     3  0/1/1/0/1/1    c8xx           CLAIMED /dev/c8xx3                          
target      4  0/1/1/0/1/1.0  tgt            CLAIMED
disk        4  0/1/1  sdisk          CLAIMED /dev/c3t0   /dev/c2t0 /dev/c4t0                            

Upvotes: 0

Views: 88

Answers (3)

Gilles Quénot
Gilles Quénot

Reputation: 185005

Using (more portable than Suhas's sed answer in your case):

perl -0777 -pe 's/\n\s+/ /gms'

-0777 read the whole file in a string

The substitution modifiers are mandatory there.

Upvotes: 1

Ed Morton
Ed Morton

Reputation: 203219

sed is an excellent tool for simple substitutions on a single line. For anything else, just use awk:

$ awk '{printf "%s%s", (gsub(/^[[:space:]]+/,"")?" ":nl), $0; nl="\n"} END{print ""}' file
ext_bus     3  0/1/1/0/1/1    c8xx           CLAIMED /dev/c8xx3
target      4  0/1/1/0/1/1.0  tgt            CLAIMED
disk        4  0/1/1  sdisk          CLAIMED /dev/c3t0   /dev/c2t0 /dev/c4t0

The above handles each input line one at a time and doesn't read the whole file into memory but if that's acceptable then here's the GNU awk (for RS='\0' to read the whole file as a string) equivalent of @sputnik's perl script if anyone cares:

$ gawk -v RS='\0' -v ORS= '{gsub(/\n\s+/," ")}1' file
ext_bus     3  0/1/1/0/1/1    c8xx           CLAIMED /dev/c8xx3
target      4  0/1/1/0/1/1.0  tgt            CLAIMED
disk        4  0/1/1  sdisk          CLAIMED /dev/c3t0   /dev/c2t0 /dev/c4t0

In non-gawk awks, just set RS to some control char that doesn't appear in your input file.

Upvotes: 0

user2032663
user2032663

Reputation:

Try this:

sed -e :a -e '$!N;s/\n\s/ /;ta' -e 'P;D' <filename>

Upvotes: 0

Related Questions