交换句子中的第一个和第三个单词(sed)



我想用sed交换句子中的第一个和第三个单词。

我的代码看起来像这样:

sed 's/^ ([a-zA-Z]*) (^[a-zA-Z])  ([a-zA-Z]*) (^[a-zA-Z]) ([a-zA-Z]*) (.*) / 5 2 3 4 1 6 /g' $1

单词只包含小写和大写字符。不是A-z-A-z的字符是分隔符。我的代码什么都不做:)

不要转义*字符:

sed 's/^([a-zA-Z]*)([^a-zA-Z])([a-zA-Z]*)([^a-zA-Z])([a-zA-Z]*)(.*) /523416/g' $1

此外,你不需要重组之间的空间。

#!/bin/sh
echo "wordone wordtwo wordthree wordfour wordfive" 
| sed -Ee 's/^([[:alpha:]]+) ([[:alpha:]]+) ([[:alpha:]]+)/3 2 1/'

试试这个:

$ sed -r 's/^([a-zA-Z]*)([^a-zA-Z]+[a-zA-Z]+[^a-zA-Z]+)([a-zA-Z]*)(.*)/3214/' infile

示例#1

$ line="one two three four five"
$ echo "$line" | sed -r 's/^([a-zA-Z]*)([^a-zA-Z]+[a-zA-Z]+[^a-zA-Z]+)([a-zA-Z]*)(.*)/3214/'

输出:

three two one four five

示例#2

$ line="one,two,three,four,five"
$ echo "$line" | sed -r 's/^([a-zA-Z]*)([^a-zA-Z]+[a-zA-Z]+[^a-zA-Z]+)([a-zA-Z]*)(.*)/3214/'

输出:

three,two,one,four,five

最新更新