[英]Swap columns in bash using SED without using loop
I'm new to Sed, I'm trying to learn some pattern using Sed. 我是Sed的新手,我正在尝试使用Sed学习一些模式。 I got a filenamne.txt that has the following entry:
我得到了具有以下条目的filenamne.txt:
ppp/jjj qqq/kkk rrr/lll
ppp / jjj qqq / kkk rrr / lll
My goal is to swap the word before the slash and the word after the slash in each of the three word1/word2 columns: 我的目标是在三个word1 / word2列中的每一个之间交换斜线前的单词和斜线后的单词:
jjj/ppp kkk/qqq lll/rrr
jjj / ppp kkk / qqq lll / rrr
I tried using sed –re 's!(.*)(/)(.*)!\\1\\2\\!' filename.txt
我尝试使用
sed –re 's!(.*)(/)(.*)!\\1\\2\\!' filename.txt
sed –re 's!(.*)(/)(.*)!\\1\\2\\!' filename.txt
, but it didn't work. sed –re 's!(.*)(/)(.*)!\\1\\2\\!' filename.txt
,但是没有用。 Any idea how can I go about it? 知道我该怎么办吗?
$ echo "ppp/jjj qqq/kkk rrr/lll" | sed -e 's/$/ /' -e 's!\([^/]*\)/\([^ ]*\) !\2/\1 !g'
jjj/ppp kkk/qqq lll/rrr
Use replacement in perl
command-line is a lot more straight-forward :- 在
perl
命令行中使用替换更为简单:-
perl -pe 's/(\w+)\/(\w+)/$2\/$1/g' file
jjj/ppp kkk/qqq lll/rrr
$ sed 's#\([^ ]*\)/\([^ ]*\)#\2/\1#g' file
jjj/ppp kkk/qqq lll/rrr
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.