我有一个像这样的文件test.txt (但包含更多的行)
/foo/bar/how hello /foo/bar/are hello /foo/bar/you hello我想得到这个输出:
/foo/bar/how how /foo/bar/are are /foo/bar/you you我试过这个:
while read line do bla=$(echo $line | cut -f4 -d"/" | cut -f1 -d" ") sed -i "s/hello/$bla/" done <test.txt但是输出结果是:
sed: no input files sed: no input files sed: no input files当我提供文件名( while read line; do bla=$(echo $line | cut -f4 -d"/" | cut -f1 -d" "); sed -i "s/hello/$bla/" test.txt ; done <test.txt ),我得到这个:
/foo/bar/how how /foo/bar/are how /foo/bar/you how我想在每一行上用一个出现在同一行上的模式替换一些不变的模式,并且在行之间改变。 任何想法如何我可以做到这一点(使用SEED或AWK)? 非常感谢!
I have a file test.txt like this (but containing many more lines)
/foo/bar/how hello /foo/bar/are hello /foo/bar/you helloI want to get this output:
/foo/bar/how how /foo/bar/are are /foo/bar/you youI have tried this:
while read line do bla=$(echo $line | cut -f4 -d"/" | cut -f1 -d" ") sed -i "s/hello/$bla/" done <test.txtBut the output is:
sed: no input files sed: no input files sed: no input filesWhen I provide the filename (while read line; do bla=$(echo $line | cut -f4 -d"/" | cut -f1 -d" "); sed -i "s/hello/$bla/" test.txt ; done <test.txt), I get this:
/foo/bar/how how /foo/bar/are how /foo/bar/you howI would like to replace on each line some constant pattern by a pattern appearing before on the same line and that changes from line to line. Any idea on how I could do that (using sed or awk)? Many thanks!
最满意答案
$ sed 's~\([^/]*\) .*~\1 \1~' file /foo/bar/how how /foo/bar/are are /foo/bar/you you $ sed 's~\([^/]*\) .*~\1 \1~' file /foo/bar/how how /foo/bar/are are /foo/bar/you you更多推荐
发布评论