使用 awk 连接连续的行
Joining consecutive lines using awk
如何使用 awk 将连续的行连接成一行?实际上我的 awk 命令有这个:
awk -F "\"*;\"*" '{if (NR!=1) {print }}' file.csv
我去掉第一行
44895436200043
38401951900014
72204547300054
38929771400013
32116464200027
50744963500014
我想要这个:
44895436200043 38401951900014 72204547300054 38929771400013 32116464200027 50744963500014
csv 文件
我假设您想修改现有的 awk,以便它打印水平 space 分隔列表,而不是每行一个单词。
你可以在命令中替换print
动作,你可以这样做:
awk -F "\"*;\"*" 'NR!=1{u=u s ; s=" "} END {print u}' file.csv
或替换ORS(输出记录分隔符)
awk -F "\"*;\"*" -v ORS=" " 'NR!=1{print }' file.csv
或管道输出到xargs
:
awk -F "\"*;\"*" 'NR!=1{print }' file.csv | xargs
这是 tr
的工作:
# tail -n +2 prints the whole file from line 2 on
# tr '\n' ' ' translates newlines to spaces
tail -n +2 file | tr '\n' ' '
使用 awk,您可以通过将输出记录分隔符更改为 " "
:
来实现此目的
# BEGIN{ORS= " "} sets the internal output record separator to a single space
# NR!=1 adds a condition to the default action (print)
awk 'BEGIN{ORS=" "} NR!=1' file
如何使用 awk 将连续的行连接成一行?实际上我的 awk 命令有这个:
awk -F "\"*;\"*" '{if (NR!=1) {print }}' file.csv
我去掉第一行
44895436200043
38401951900014
72204547300054
38929771400013
32116464200027
50744963500014
我想要这个:
44895436200043 38401951900014 72204547300054 38929771400013 32116464200027 50744963500014
csv 文件
我假设您想修改现有的 awk,以便它打印水平 space 分隔列表,而不是每行一个单词。
你可以在命令中替换print
动作,你可以这样做:
awk -F "\"*;\"*" 'NR!=1{u=u s ; s=" "} END {print u}' file.csv
或替换ORS(输出记录分隔符)
awk -F "\"*;\"*" -v ORS=" " 'NR!=1{print }' file.csv
或管道输出到xargs
:
awk -F "\"*;\"*" 'NR!=1{print }' file.csv | xargs
这是 tr
的工作:
# tail -n +2 prints the whole file from line 2 on
# tr '\n' ' ' translates newlines to spaces
tail -n +2 file | tr '\n' ' '
使用 awk,您可以通过将输出记录分隔符更改为 " "
:
# BEGIN{ORS= " "} sets the internal output record separator to a single space
# NR!=1 adds a condition to the default action (print)
awk 'BEGIN{ORS=" "} NR!=1' file