linux多行折成一行

linux下如何把多行文本按照规律换成一行呢?跪谢大佬

比如:

// a.txt  处理前
a
b
c

aaa
bbb
ccc

  ddd //每行前面可能会有多个空格
  fff
  gg
  gg
  gg




// 处理后,字符间使用空格间隔

a b c
aaa bbb ccc
ddd fff gg gg gg
阅读 3.9k
2 个回答

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.

试试这样:

sed ':a;N;$!ba;s/\n\n/--/g;s/\n/ /g;s/--/\n/g' a.txt
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题