注意:这个问题看起来像以前发布的一个,但正如评论中提到的,它原来是一个变色龙问题。 我接受了答案,我在这里发布了同样的问题,但“解决方案条件”略有不同。
我有一个test.txt这样的文件(但包含更多行)
/foo/bar/how /SOME_TEXT_HERE/hello
/foo/bar/are hello/SOME_OTHER_TEXT
/foo/bar/you hello
Run Code Online (Sandbox Code Playgroud)
我想得到这个输出:
/foo/bar/how /SOME_TEXT_HERE/how
/foo/bar/are are/SOME_OTHER_TEXT
/foo/bar/you you
Run Code Online (Sandbox Code Playgroud)
我试过这个:
while read line
do
bla=$(echo $line | cut -f4 -d"/" | cut -f1 -d" ")
sed -i "s/hello/$bla/" test.txt
done <test.txt
Run Code Online (Sandbox Code Playgroud)
但输出是:
/foo/bar/how /SOME_TEXT_HERE/how
/foo/bar/are how/SOME_OTHER_TEXT
/foo/bar/you how
Run Code Online (Sandbox Code Playgroud)
注意:
我想解决以下问题:
允许我定义一个变量(这里,bla),我将手动定义一个文件到另一个文件(所以不要像基本字段位置那样使用 sthg),而是cut像我的例子一样使用或 其他命令)
用这个变量替换行中其他地方的特定字符串(所以不是像 那样的字段位置$2,而是像 那样s/hello/$bla)
我不确定这是否可行(显然我自己不知道如何做到这一点......),但感谢您的时间尝试!:)
小智 5
awk '{sub(/are hello/,"are are")sub(/you hello/,"you you")}1' file
/foo/bar/how /SOME_TEXT_HERE/hello
/foo/bar/are are/SOME_OTHER_TEXT
/foo/bar/you you
Run Code Online (Sandbox Code Playgroud)