使用 sed 交换列 [英] swap columns using sed
本文介绍了使用 sed 交换列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在文件 test
中有如下数据:
I have a data like below in file test
:
2233:Jorge Bill :56Blue Pkwy :99999
1111:Mary Smith :33Red Street :123456
我想使用 sed 和分组交换姓氏和名字,但我的代码不起作用!
I want to swap the family name and first name using sed and grouping but my code doesn't work!
sed 's/\([^:]*:\)\([^' ']*' '\)\([^:]*\)/\1\3\2' test
你知道哪里出了问题吗?
Do you know what is wrong?
推荐答案
您忘记对正则表达式中的单引号进行转义,并且您还需要在替换字符串的末尾多加一个正斜杠.我去掉了单引号,这里是.
You forgot to escape the single-quotes within your regex, and also you need to have one more forward slash at the end of your replacement string. I got rid of the single quotes, and here it is.
sed 's/\([^:]*:\)\([^ ]* \)\([^:]*\)/\1\3\2/' test
这篇关于使用 sed 交换列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文