[英]How to switch a word with a digit in a line in shell using sed
I have a question regarding on how to switch a number (1-infinity) with a character or a word: 我有一个关于如何用字符或单词切换数字(1个无穷大)的问题:
Here is my code: 这是我的代码:
sed 's/ ([a-z]*) ([0-9]*)/\2 \1/g'> $file_output
so I have the output (before using the command above) as 所以我的输出(在使用上面的命令之前)为
1 a
45 adam
and the intended output from using the sed command: 以及使用sed命令的预期输出:
a 1
adam 45
but the line wouldn't work, specifically due to a syntax error. 但该行无法正常工作,特别是由于语法错误。 Here is the error that comes up:
这是出现的错误:
sed: -e expression #1, char 28: invalid reference \2 on `s' command's RHS
How can I achieve my desired output? 如何获得所需的输出?
You need to backslash the parentheses to make them into grouping ones: 您需要对括号进行反斜杠以使其分组:
s/\([a-z]*\) \([0-9]*\)/\2 \1/g'
Or, if your sed supports it, use -r
or -E
to indicate you're using extended regular expressions. 或者,如果您的sed支持它,请使用
-r
或-E
表示您正在使用扩展的正则表达式。
使用sed 's/\\(.*\\) \\(.*\\)/\\2 \\1/'
是将任何字母数字彼此转换的一般方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.