使用不同的字符串在多行上编辑文件

自私的

我正在编写一个脚本,该脚本将在所有行中的文件中查找以下行中是否在字符串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( !开始(^)用following1line_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_match1

{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_match0

> tmp_file && mv tmp_file ${file}因为没有sed之类的功能,因此需要临时文件(tmp_file)。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用不同的字符和模式分割字符串

文本架构文件-需要在标签内允许使用不同大小的字符串

Android:如何使用不同的字符串值文件填充布局?

在Python中编辑多行字符串

如何用不同的字符串替换一组文件中的字符串?

替换相同的字符串但使用不同的字符串值?

如何使用图形在多行上输出字符串

如何在第n个元素上使用不同的分隔符连接字符串

如何使用python将多行字符串写入文件

使用不同的字符串大小写 ansible 检查文件名,我可以实现 if/else 吗?

使用sed在两行上编辑字符串

在Powershell中使用搜索字符串编辑文件

如何使用sed编辑以字符串形式保存的文件?

在多行上显示字符串

如何在Android中使用不同的参数格式化不同语言的字符串?

在终端中使用不同的字符串时出现不同的错误消息

查找匹配多行字符串的文件

每次用不同的字符串替换模式(取自外部文件)

根据定位不同的代码(带标题)更正分散在多行上的字符串

Python:文本文件在多行中替换不同的字符串怎么办?

编辑bat文件中的字符串

编辑 XML 文件中的字符串

如何在Eclipse for Java中对字符串和其他项目使用不同的换行

如何使用不同的命名查找同一图像的最短名称(字符串)

为什么在使用不同的字符串时,角度表现出乎意料?

使用不同的子字符串和charindex的实体框架

为什么对Python字符串函数使用不同的方法

C#:使用字符串字典,接口引用不同的类

在Groovy中使用不同的行分隔符分割和连接字符串