如何在bash中操作文本?

时间:2017-12-31 00:43:00

标签: bash perl awk sed grep

如何使用awk,grep,perl或sed操作bash中的文本?

输入:

ted foo,bar,zoo
john ket,ben

预期产出:

foo,ted
bar,ted
zoo,ted
ket,john
ben,john

1 个答案:

答案 0 :(得分:2)

使用awk:

awk 'BEGIN{FS="[ ,]"; OFS=","} {for (i=2; i<=NF; i++) print $i,$1}' file

输出:

foo,ted
bar,ted
zoo,ted
ket,john
ben,john

请参阅:8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR