我正在编写一个脚本,该脚本将在所有行中的文件中查找以下行中是否在字符串A后跟字符串B。如果不正确,它将添加字符串C。
我已经设法做到了,但是我想在文件中的几个地方做到这一点。像这样
#!/bin/bash
match="match1"
match_following_line="following1"
insert='line_insert1'
file='words'
testfile='testfile'
#find linenumber for the first match
linenumber=$(awk -v myvar="$match" '$0~myvar {print NR}' $file)
#calculate linenumber for the comming line
linenumber2=$(( linenumber + 1 ))
#find string of that line
linecontent=$(sed -n "$linenumber2"p < $file)
echo $linecontent
#if it isn't match2 then add match2
if [ "$linecontent" != "$match_following_line" ]
then
awk -v line="$linenumber" -v text="\n$insert" ' NR!=line{print} NR==line{print $0 text}' $file > $testfile
fi
该文件words
如下所示:
A
B
C
D
match1
After execusion the line before this line will become "line_insert1"
E
该文件的输出testfile
将是:
A
B
C
D
match1
line_insert1
After execusion the line before this line will become "line_insert1"
E
我尝试了几行:
match[0]='match1'
match_follower[0]='following1'
insert[0]='line_insert1'
match[1]='match2'
match_follower[1]='following2'
insert[1]='line_insert2'
match[2]='match3'
match_follower[2]='following3'
insert[2]='line_insert3'
file='1.csv'
for i in 0 1 2
do
#find linenumber for the first match
linenumber=$(awk -v myvar="${match[$i]}" '$0~myvar {print NR}' $file)
echo $linenumber
#calculate linenumber for the comming line
linenumber2=$(( linenumber + 1 ))
#find string of that line
linecontent=$(sed -n "$linenumber2"p < $file)
#if it isn't match_follower then add insert
if [ "$linecontent" != "${match_follower[$i]}" ]
then
awk -v line="$linenumber" -v text="${insert[$i]}" ' NR!=line{print} NR==line{print $0 text}' $file > $file
fi
done
当我尝试对几个字符串执行此操作时,我不知道要做什么,因为awk会写入$ testfile并在每个循环中都覆盖该文件。有谁知道一个好的解决方案?
match[0]='match1'
match_follower[0]='following1'
insert[0]='line_insert1'
match[1]='match2'
match_follower[1]='following2'
insert[1]='line_insert2'
match[2]='match3'
match_follower[2]='following3'
insert[2]='line_insert3'
file='1.csv'
for i in 0 1 2
do
sed -E -i '/'"${match[$i]}"'/!b;n;/^'"${match_follower[$i]}"'|^'"${insert[$i]}"'/! i '"${insert[$i]}" ${file}
done
为了使用shell变量sed
的''
被迫关闭和开放""
的变量然后重新打开''
等等等等,这就是为什么在sed的线这么多qoute。
首先,对于sed周期,它看起来像这样:
sed -E -i '/match1/!b;n;/^following1|^line_insert1/! i line_insert1' 1.csv
哪里
/match1/!
找到NOT(!
)包含的行,match1
然后b
阻止在当前行执行其余的sed脚本。如果不是这种情况,则sed脚本继续n
(;
是命令块之间的分隔符)
n
会将下一行读入模式空间,因此其余的sed脚本将在下一行执行。
/^following1|^line_insert1/!
这确保脚本只插入行,如果当前模式空间(线),NOT( !
)开始(^
)用following1
或line_insert1
,-E
开关需要这个正则表达式工作。
i line_insert1
这是在实际行中插入带有给定文本的新行i
的插入操作符。
sed -i
更改文件到位。
使用AWK只需用以下命令替换sed行:
awk -v v_match="${match[$i]}" -v v_follower="${match_follower[$i]}" -v v_insert="${insert[$i]}" '
{if (find_match == 1 && $0 !~ v_insert && $0 !~ v_follower ) { find_match=0 ; print v_insert } { find_match=0 ;print } }
$0 ~ v_match { find_match=1 }' ${file} > tmp_file && mv tmp_file ${file}
$0 ~ v_match { find_match=1 }
如果将line${match[$i]}
设置find_match
为1
{if (find_match == 1 && $0 !~ v_insert && $0 !~ v_follower ) {find_match=0 ; print v_insert } { find_match=0 ;print } }
如果find_match
是,1
并且行不是${match_follower[$i]}
或${insert[$i]}
然后打印${insert[$i]}
,则仅打印当前行,在两种情况下均设置find_match
为0
。
> tmp_file && mv tmp_file ${file}
因为没有sed之类的功能,因此需要临时文件(tmp_file
)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句