如何使用awk,grep,perl或sed操作bash中的文本?
输入:
ted foo,bar,zoo
john ket,ben
预期产出:
foo,ted
bar,ted
zoo,ted
ket,john
ben,john
答案 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