我正在寻找一种在 Linux 中更改列位置的解决方案。 在我的特殊情况下,从第一个位置到最后一个位置。
qty | sender | recipient | some email subject
sender | recipient | some email subject | qty
如果我有一个包含以下内容的文件:
4 | one | two | the first subject
5 | one | four | other interesting subject
我想要以下输出:
one | two | the first subject | 4
one | four | other interesting subject | 5
分隔符是“|”。如果我有“|”并不重要在每一行的开头或结尾。
谢谢!
最佳答案
这种东西是sed的经典之作:
sed 's/\(^[^|]*\)|\(.*$\)/\2 | \1/' yourFile.txt >newFile.txt
将更改直接保存在同一个文件中:
sed -i 's/\(^[^|]*\)|\(.*$\)/\2 | \1/' yourFile.txt
- ^[^|]* --> 从行首到第一个竖线。
- .*$ --> 直到行尾的任何剩余字符。
- \(\) --> 这是为了保存这些部分,稍后用\{number} 恢复
- \2 |\1 --> 使用新订单恢复保存的部分。
了解更多关于 sed here .
关于linux - 更改文本 linux 文件中的列位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31566643/