使用awk从文本制作csv

时间:2010-12-04 09:32:37

标签: csv awk

我有很多这样的txt文件:

Title 1
Text 1

我想从所有人那里制作一个csv文件,它看起来像这样:

Title 1,Text 1
Title 2,Text 2
Title 3,Text 3
etc

我怎么能用awk做到?

1 个答案:

答案 0 :(得分:1)

在不知道任何细节的情况下,以下答案看起来是不错的选择:

awk '{printf "%s,", $0; getline; print}'
# every second line gets merged with the previous line

awk \
'
  $0 ~ /^Title/ {printf "\n"}
  {printf "%s,", $0}
'
# every line that starts with Title starts
# a newline and the rest is merged into one
# long line separated by commas.