sed 使用正则表达式 [英] Sed using regular expressions
问题描述
所以基本上我有一个包含 1500 个名字的原始列表,每个名字都有一个 4 位数字.我有另一个文件,其中包含 1500 个名称中的 200 个,并带有一个新的 4 位数字与之关联.我需要用这个新号码替换旧号码.
So basically I have a original list of 1500 names with a 4 digit number associated with each name. I have another file which contains 200 of these 1500 names with a new 4 digit number associated with it. I need to replace the old number with this new number.
我有一个包含 1500 行文本的文件 original.txt,如下所示:
I have a file original.txt which contains 1500 lines of text that looks like this:
name AYxxxx
name2 AYxxxx
name3 AYxxxx
......
name1500 AYxxxx
name AYxxxx
name2 AYxxxx
name3 AYxxxx
......
name1500 AYxxxx
xxxx 是 4 位数字,AY 是出现在每 4 位字符前面的字符串
The xxxx is a 4 digit number, AY is a string that appears in front of every 4 digit character
我有一个包含 200 行的 updated_file.txt,如下所示:
I have an updated_file.txt that contains 200 lines like this:
name AYzzzz
name40 AYzzzz
name1300 AYzzzz
name AYzzzz
name40 AYzzzz
name1300 AYzzzz
zzzz 是一个不同的 4 位数字.我需要取那个新号码 (zzzz) 并替换 original.txt 中的旧号码 (xxxx).
zzzz is a different 4 digit number. I need to take that new number (zzzz) and replace the old number (xxxx) in the original.txt.
所以在原文中,我需要的是文本应该是这样的:
So in the original text, I need this is what the text should look like:
name AYzzzz
name2 AYxxxx
name40 AYzzzz
name1300 AYzzzz
name1500 AYxxxx
name AYzzzz
name2 AYxxxx
name40 AYzzzz
name1300 AYzzzz
name1500 AYxxxx
我正在考虑做这样的事情:
I was thinking of doing something like this:
names=updated.txt
while read names
do
sed -E "s/$names[^AY.*]/$names/" original.txt
done < "$names"
推荐答案
您可以像
sed 's:\(.*AY\)\d\+:/\1/s_.*_&_:' UPDATEFILE > SEDUPDATE.SED
然后在原始文件上运行生成的脚本,如:
Then run the generated script on the original file like:
sed -f SEDUPDATE.SED ORIGINAL_FILE
这篇关于sed 使用正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!