Regex 如何在文档中使用正则表达式对字符串进行完全匹配,以修改单词后面的所有内容
我有一个类似的文件,名为listofnames.txtRegex 如何在文档中使用正则表达式对字符串进行完全匹配,以修改单词后面的所有内容,regex,awk,sed,match,Regex,Awk,Sed,Match,我有一个类似的文件,名为listofnames.txt this-is-name-1 this-is-name-2 ... this-is-name-11 this-is-name-12 this-is-name-13 this-is-name-14 ... this-is-name-21 .... 我想对某个单词进行完全匹配,并在其旁边添加一个“0”。我使用以下命令: sed-i'/\b这个-is-name-1\b/s/$/0/'listofnames.txt this-is-name-1
this-is-name-1
this-is-name-2
...
this-is-name-11
this-is-name-12
this-is-name-13
this-is-name-14
...
this-is-name-21
....
我想对某个单词进行完全匹配,并在其旁边添加一个“0”。我使用以下命令:
sed-i'/\b这个-is-name-1\b/s/$/0/'listofnames.txt
this-is-name-1 0
this-is-name-2
...
this-is-name-11
this-is-name-12
this-is-name-13
this-is-name-14
...
this-is-name-21
....
但在那之后,我希望能够替换单词后面的所有内容,并用1替换它,使其看起来像这样
this-is-name-1 1
this-is-name-2
...
this-is-name-11
this-is-name-12
this-is-name-13
this-is-name-14
...
this-is-name-21
....
我使用以下命令,
sed-i'/\b这个-is-name-1\b/s/$/1/'listofnames.txt
但我明白了
this-is-name-1 0 1
this-is-name-2
...
this-is-name-11
this-is-name-12
this-is-name-13
this-is-name-14
...
this-is-name-21
....
如何修复代码,正确的命令是什么?如果您同意
awk
,请尝试以下操作。使用GNUawk
中显示的样本编写和测试。此外,shell变量在赋值时不应该有空格
currentname='this-is-name-1'
awk -v str="$currentname" '$0 ~ "^"str"$"{print $0,1;next} 1' Input_file > temp && mv temp Input_file
说明:添加上述内容的详细说明
awk -v str="$currentname" ' ##Starting awk program from here and setting variable str value to shell variable currentname here.
$0 ~ "^"str"$"{ ##Checking if line starts with str and ends with it.
print $0,1 ##Printing current line and 1 here.(change 0 to get 0 printed here)
next ##next will skip all further statements from here.
}
1 ##1 will print edited/non-edited lines here.
' Input_file ##Mentioning Input_file name here.
如果您对
awk
没有问题,请尝试以下内容。使用GNUawk
中显示的样本编写和测试。此外,shell变量在赋值时不应该有空格
currentname='this-is-name-1'
awk -v str="$currentname" '$0 ~ "^"str"$"{print $0,1;next} 1' Input_file > temp && mv temp Input_file
说明:添加上述内容的详细说明
awk -v str="$currentname" ' ##Starting awk program from here and setting variable str value to shell variable currentname here.
$0 ~ "^"str"$"{ ##Checking if line starts with str and ends with it.
print $0,1 ##Printing current line and 1 here.(change 0 to get 0 printed here)
next ##next will skip all further statements from here.
}
1 ##1 will print edited/non-edited lines here.
' Input_file ##Mentioning Input_file name here.
另一个
gnuawk
命令,可以在两种情况下工作,并在线保存更改
# place 0 in the end
awk -i inplace -v n='0' -v s='this-is-name-1' '$1 == s { $2 = n } 1' file
# place 1 in the end
awk -i inplace -v n='1' -v s='this-is-name-1' '$1 == s { $2 = n } 1' file
在python中运行此命令:
import subprocess
currentname = 'this-is-name-1'
argList = ["awk", "-i", "inplace", "-v", "n=1", "-v", "s=" + currentname, '$1 == s{$2 = n} 1', 'file']
subprocess.call(argList)
另一个
gnuawk
命令,可以在两种情况下工作,并在线保存更改
# place 0 in the end
awk -i inplace -v n='0' -v s='this-is-name-1' '$1 == s { $2 = n } 1' file
# place 1 in the end
awk -i inplace -v n='1' -v s='this-is-name-1' '$1 == s { $2 = n } 1' file
在python中运行此命令:
import subprocess
currentname = 'this-is-name-1'
argList = ["awk", "-i", "inplace", "-v", "n=1", "-v", "s=" + currentname, '$1 == s{$2 = n} 1', 'file']
subprocess.call(argList)
你能帮我修改第一个部分以使用awk而不是sed吗?sed的替换文件标志是什么-i@anarchy,您的意思是希望
0
分离输出,而1
分离输出,对吗?如果是这种情况,那么对于获取0
使用awk-v str=“$currentname”'匹配($0,“^”str“$”){print$0,0;next}1'输入_文件
,如果有任何查询,请告诉我。您能帮我修改第一部分以使用awk而不是sed吗?sed这样的替换文件标志是什么-i@anarchy,你的意思是你想0
分离输出和1
分离输出,对吗?如果是这种情况,那么对于获取0
使用awk-v str=“$currentname”'匹配($0,“^”str“$”){打印$0,0;下一个}1'输入_文件
,如果有任何查询,请告诉我。让我们。让我们。