I want to match the rule with or without redundant string in the end. And swap the match patterns.
Example:
我想要匹配规则是否有冗余字符串。交换匹配模式。例子:
$ "aaa bbb ccc" --> "bbb aaa"
$ "aaa bbb" --> "bbb aaa"
Here is what I tried but does not work.
这是我尝试过但没有成功的方法。
echo "aaa bbb ccc" | sed -e 's:\(.*\)\s\(.*\)(\s\(.*\))?:\2\s\1:g'
$ aaa bbb ccc
Thank you.
谢谢你!
2 个解决方案
#1
2
Using sed
Try:
试一试:
$ echo "aaa bbb" | sed -Ee 's:([[:alnum:]]+)[[:space:]]+([[:alnum:]]+).*:\2 \1:'
bbb aaa
$ echo "aaa bbb ccc" | sed -Ee 's:([[:alnum:]]+)[[:space:]]+([[:alnum:]]+).*:\2 \1:'
bbb aaa
Notes:
注:
-
sed recognizes POSIX regular expressions.
\s
is a GNU-only extension. Character classes like[[:space:]]
and[[:alnum:]]
are understand by all implementations of sed.sed识别POSIX正则表达式。\s是gnuonly的扩展。像[:space:]和[[:alnum:]这样的字符类被sed的所有实现所理解。
-
I added the option
-E
to get extended regular expressions. That eliminates the need for the backslashes and it also enables to the use of+
to mean one or more.我添加了选项-E来扩展正则表达式。这就消除了对反斜杠的需要,并且还允许使用+来表示一个或多个。
-
There is no need to match parts of the line after the words that you want to exchange.
不需要在你想要交换的词之后匹配行中的部分。
Using bash
$ read a b c<<<"aaa bbb"; echo "$b $a"
bbb aaa
$ read a b c<<<"aaa bbb ccc"; echo "$b $a"
bbb aaa
Using awk
$ echo "aaa bbb" | awk '{print $2,$1}'
bbb aaa
$ echo "aaa bbb ccc" | awk '{print $2,$1}'
bbb aaa
#2
0
awk solution:
awk的解决方案:
echo aaa bbb | awk '{print $2" "$1}'
echo aaa bbb ccc | awk '{print $2" "$1}'
what it does is just print the 1st and the 2nd fields in the reverse order ignoring the rest of the lines: elements are identified by the default FS
of awk
that can be adapted to your needs
它所做的只是以相反的顺序打印第1和第2个字段,而忽略其余行:元素由awk的默认FS标识,可以根据您的需要进行调整
Hope it helps you!
希望它能帮助你!
#1
2
Using sed
Try:
试一试:
$ echo "aaa bbb" | sed -Ee 's:([[:alnum:]]+)[[:space:]]+([[:alnum:]]+).*:\2 \1:'
bbb aaa
$ echo "aaa bbb ccc" | sed -Ee 's:([[:alnum:]]+)[[:space:]]+([[:alnum:]]+).*:\2 \1:'
bbb aaa
Notes:
注:
-
sed recognizes POSIX regular expressions.
\s
is a GNU-only extension. Character classes like[[:space:]]
and[[:alnum:]]
are understand by all implementations of sed.sed识别POSIX正则表达式。\s是gnuonly的扩展。像[:space:]和[[:alnum:]这样的字符类被sed的所有实现所理解。
-
I added the option
-E
to get extended regular expressions. That eliminates the need for the backslashes and it also enables to the use of+
to mean one or more.我添加了选项-E来扩展正则表达式。这就消除了对反斜杠的需要,并且还允许使用+来表示一个或多个。
-
There is no need to match parts of the line after the words that you want to exchange.
不需要在你想要交换的词之后匹配行中的部分。
Using bash
$ read a b c<<<"aaa bbb"; echo "$b $a"
bbb aaa
$ read a b c<<<"aaa bbb ccc"; echo "$b $a"
bbb aaa
Using awk
$ echo "aaa bbb" | awk '{print $2,$1}'
bbb aaa
$ echo "aaa bbb ccc" | awk '{print $2,$1}'
bbb aaa
#2
0
awk solution:
awk的解决方案:
echo aaa bbb | awk '{print $2" "$1}'
echo aaa bbb ccc | awk '{print $2" "$1}'
what it does is just print the 1st and the 2nd fields in the reverse order ignoring the rest of the lines: elements are identified by the default FS
of awk
that can be adapted to your needs
它所做的只是以相反的顺序打印第1和第2个字段,而忽略其余行:元素由awk的默认FS标识,可以根据您的需要进行调整
Hope it helps you!
希望它能帮助你!