使用sed切换成对行的第一个单词

时间:2012-05-01 11:25:54

标签: sed line switch-statement

我想要做的就是使用sed切换成对行的第一个单词。我知道使用其他东西会更容易,但不幸的是需要sed(自己测试)

所以,例子就是

One line here
Two line here as well
Third line could be here
fourth line to end

最终将成为

Two line here
One line here as well
fourth line could be here
third line to end

我可能在sed中使用了错误的操作,但是,不知道。任何线索?

2 个答案:

答案 0 :(得分:3)

sed -e 'N; s/\([a-zA-Z]*\)\(.*\)\n\([a-zA-Z]*\)\(.*\)/\3\2\   ⏎
\1\4/'

(是的,这是替换中反斜杠转义的显式换行符)

答案 1 :(得分:1)

这可能适合你(可能是GNU sed):

sed '$!N;s/\(\S*\)\(.*\n\)\(\S*\)/\3\2\1/' file
Two line here
One line here as well
fourth line could be here
Third line to end