Move next line to current line after "+"

80 views Asked by At

I have text file with data as you can see below

  1  0.751E-04  0.000E+00  0.113E-04  0.735E-05 -0.530E-05  0.410E-06 -0.805E-06      +
    -0.442E-06  0.476E-06 -0.252E-06
  2  0.792E-04  0.000E+00  0.134E-04  0.680E-05 -0.504E-05  0.435E-06 -0.895E-06      +
    -0.216E-06  0.149E-06 -0.133E-06

I want to move the line after + to the above or current line as shown below

  1  0.751E-04  0.000E+00  0.113E-04  0.735E-05 -0.530E-05  0.410E-06 -0.805E-06 -0.442E-06  0.476E-06 -0.252E-06
  2  0.792E-04  0.000E+00  0.134E-04  0.680E-05 -0.504E-05  0.435E-06 -0.895E-06 -0.216E-06  0.149E-06 -0.133E-06

I tried awk using below code

awk '{if($0 ~ /+/) print $0; getline; print}' A1.txt

With this code I get the output same as the input and the line/row is not moving up.

I also tried tr

 tr '      +' ' ' < A1.txt >> ddg.txt

But got the same as output without the + sign.

I can share an example input file but I do not see an option here. Can I upload it in google drive and share the link or would that be against the forum's rules?

4

There are 4 answers

2
Gilles Quénot On BEST ANSWER

With :

awk 'BEGIN{RS=""}{gsub(/ +\+\n +/, " ")}1' file

With GNU :

sed -z 's/ \++\n \+/ /g' file

With :

perl -0777 -pe 's/\s+\+\n\s+/ /g' file
  1  0.751E-04  0.000E+00  0.113E-04  0.735E-05 -0.530E-05  0.410E-06 -0.805E-06 -0.442E-06  0.476E-06 -0.252E-06
  2  0.792E-04  0.000E+00  0.134E-04  0.680E-05 -0.504E-05  0.435E-06 -0.895E-06 -0.216E-06  0.149E-06 -0.133E-06
0
Allan Wind On

This should do the trick (revised for \r\n line ending). It will leave non-matching lines alone just in case:

$ sed '/ \{1,\}+\r$/{N; s/ \{1,\}+\r\n  //}' input.txt
  1  0.751E-04  0.000E+00  0.113E-04  0.735E-05 -0.530E-05  0.410E-06 -0.805E-06 -0.442E-06  0.476E-06 -0.252E-06
  2  0.792E-04  0.000E+00  0.134E-04  0.680E-05 -0.504E-05  0.435E-06 -0.895E-06 -0.216E-06  0.149E-06 -0.133E-06
0
RARE Kpop Manifesto On

if you don't mind an extra trailing newline ::

awk $$ RS='[ +]+\n' ORS=' '
  1  0.751E-04  0.000E+00  0.113E-04  0.735E-05 -0.530E-05  0.410E-06 -0.805E-06     -0.442E-06  0.476E-06 -0.252E-06
  2  0.792E-04  0.000E+00  0.134E-04  0.680E-05 -0.504E-05  0.435E-06 -0.895E-06     -0.216E-06  0.149E-06 -0.133E-06
  3
0
RavinderSingh13 On

Based on your shown samples and attempts please try following awk code.

awk '
/^   /{
  sub(/[[:space:]]+\+/,"",val)
  sub(/^[[:space:]]+/,"")
  print val,$0
  val=""
  next
}
/^  /{
  val=$0
}
'  Input_file