在 Linux 中使用 sed 交换两个单词 [英] exchange two words using sed in Linux
本文介绍了在 Linux 中使用 sed 交换两个单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
伙计们!我试图在一行中交换两个单词,但它不起作用.例如:今天是我大学的第一天"应该是我的今天是大学的第一天"
guys! I am trying to exchange two words in a line but it doesn't work. For example: "Today is my first day of university" should be "my is Today first day of university"
这是我试过的:
sed 's/\([a-zA-z0-9]*\)\([a-zA-z0-9]*\)\([a-zA-z0-9]*\)/\3\2\1/' filename.txt
我做错了什么?
推荐答案
试试这个:
sed -rn 's/(\w+\s)(\w+\s)(\w+\s)(.*)/\3\2\1\4/p' filename.txt
-n 禁止自动打印模式空间
-n suppress automatic printing of pattern space
-r 在脚本中使用扩展的正则表达式
-r use extended regular expressions in the script
\s 表示空格
这篇关于在 Linux 中使用 sed 交换两个单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文