Combine matching lines with sed or awk?

I have a file like:

1,  
cake:01351  
12,  
bun:1063  
scone:13581  
biscuit:1931  
14,  
jelly:1385

I need to convert it so that when reading the number at the beginning of the line, it matches the line below it, but if there is no number at the beginning, the line remains as it is. This will be the result I need:

1,cake:01351  
12,bun:1063  
scone:13581  
biscuit:1931  
14,jelly:1385

Having a lot of trouble achieving this with sed, it seems like this is not the best way for what, in my opinion, should be fairly simple.

Any suggestions that were highly appreciated.

+3
source share
5 answers

Very simple implementation sed:

sed -e '/^[0-9]/{N;s/\n//;}'

It depends on the first character only in the lines of 'number', which are a number (as you indicated).

it

  • , , ^[0-9]
  • , N
  • , s/\n//
+18

. , sed . -, "sed one-liner"


- , , .

, , , :

14/04/2003,10:27:47,0
IdVg,3.000,-1.000,0.050,0.006
GmMax,0.011,0.975,0.005
IdVg,3.000,-1.000,0.050,0.006
GmMax,0.011,0.975,0.005
14/04/2003,10:30:51,600
IdVg,3.000,-1.000,0.050,0.006
GmMax,0.011,0.975,0.005
IdVg,3.000,-1.000,0.050,0.006
GmMax,0.010,0.975,0.005
14/04/2003,10:34:02,600
IdVg,3.000,-1.000,0.050,0.006
GmMax,0.011,0.975,0.005
IdVg,3.000,-1.000,0.050,0.006
GmMax,0.010,0.975,0.005

(14/04/2003) - , .

Excel - .

, G I, , :

14/04/2003,10:27:47,0,IdVg,3.000,-1.000,0.050,0.006,GmMax,0.011,0.975,0.005,IdVg,3.000,...  
14/04/2003,10:30:51,600,IdVg,3.000,-1.000,0.050,0.006,GmMax,0.011,0.975,0.0005,IdVg,3.000,...
14/04/2003,10:34:02,600,IdVg,3.000,-1.000,0.050,0.006,GmMax,0.011,0.975,0.0005,IdVg,3.000,...

" ", , , sed .

sed:

sed -e :a -e '$!N;s/\n\([GI]\)/,\1/;ta' -e 'P;D' filename >newfilename

, , ?

, - , .

+1

, :

sed '/[0-9]\+,/{N}s/\n//)'

(0-9) , , .

+1

awk-, , :

awk '/^[0-9]/ {n = $0; getline; print n $0; next} 1'
+1
$ awk 'ORS= /^[0-9]+,$/?" ":"\n"' file
1, cake:01351
12, bun:1063
scone:13581
biscuit:1931
14, jelly:1385
0

Source: https://habr.com/ru/post/1729309/


All Articles