1

My input is in TSV format:

C1
   r1
   r2
   r3
C2
   r1
   r3

Desired output is:

C1 r1
   r2
   r3
C2 r1
   r3

So, basically what I want is printing value of column 1 in next line, is it possible using Linux command?

4

With GNU sed:

$ sed -i.bak '/^[^[:blank:]]/{N;s/\n[[:blank:]]\+//;}' file
C1r1
  r2
  r3
C2r1
  r3
1

Possible solution is with awk:

awk '$1 ~ /C/ { p = 1; save = $1; } $1 !~ /C/ { if (p) { x = save $1; print x; p = 0; } else { print $0; } }' input

Result:

C1r1
  r2
  r3
C2r1
  r3
1

With ed:

$ ed -s input <<EOT
g/^C/+ s/^[[:space:]]*//
g/^C/  j                
w output
EOT

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.