鍍金池/ 問(wèn)答/Linux/ linux多行折成一行

linux多行折成一行

linux下如何把多行文本按照規(guī)律換成一行呢?跪謝大佬

比如:

// a.txt  處理前
a
b
c

aaa
bbb
ccc

  ddd //每行前面可能會(huì)有多個(gè)空格
  fff
  gg
  gg
  gg




// 處理后,字符間使用空格間隔

a b c
aaa bbb ccc
ddd fff gg gg gg
回答
編輯回答
好難瘦

The main idea is read the file line by line.
Print the line except white space if it has other characters.
You can get the characters using regex, then print it.
An example do it with perl:
cat a.txt | perl -ne 's/^\s*(\S+)\n$/\1 /g;print'
The regex will failed when the line only have white space.

2017年9月19日 03:38
編輯回答
雨萌萌

試試這樣:

sed ':a;N;$!ba;s/\n\n/--/g;s/\n/ /g;s/--/\n/g' a.txt
2018年1月18日 04:55