替换从变量中获取数字的完整行 [英] Replace complete line getting number from variable
问题描述
我有一个包含某行的文件,比方说...
I have a file with a certain line, let's say...
AAA BBB CCC
我需要在找到后替换整行,所以我做到了:
I need to replace that entire line, after finding it, so I did:
q1=`grep -Hnm 1 "AAA" FILE | cut -d : -f 2`
这会向我输出第一次出现的行号(在 q1 中),因为它出现了多次,现在,我的问题来了......在上一步中,我使用这个 sed 来替换文件:
That outputs me the line number of the first occurrence (in q1), because it has more than one occurrence, now, here comes my problem... In a previous step I was using this sed to replace a certain line in the file:
sed -e '3s/.*/WHATEVER/' FILE
要用WHATEVER替换(在示例中的第3行)整行,但是现在如果我尝试使用$q1而不是指示行号的3",则它不起作用:
To replace (in the example, line 3) the full line with WHATEVER, but now if I try to use $q1 instead of the "3" indicating the line number it doesn't work:
sed -e '$q1s/.*/WHATEVER/' FILE
这可能是一个愚蠢的语法错误,欢迎任何帮助;提前致谢
It's probably a stupid syntax mistake, any help is welcome; thanks in advance
推荐答案
尝试:sed -e "${q1}s/.*/WHATEVER/";文件
这篇关于替换从变量中获取数字的完整行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!