简体   繁体   English

用sed替换另一个词-问题

[英]Replacing a word by another with sed - issue

On a redhat server, 在Redhat服务器上,

I have multiple files named PO******* Inside them i have a line like this : 我有多个名为PO*******文件,在其中我有这样的一行:

NODE PO******* TCPblahblah blah

On the other hand i have a csv file containing multiple line like : 另一方面,我有一个包含多行的csv文件,例如:

PO*******,XXXXXXX

i want to replace in files PO******* 我想替换为PO*******文件
the NODE PO******* TCPblahblah blah NODE PO******* TCPblahblah blah
by NODE XXXXXX TCPblahblah blah 通过NODE XXXXXX TCPblahblah blah

See my code for what i did to have this output on line with NODE : TCPblahblXXX 请参阅我的代码以了解如何使此输出与NODE一致:TCPblahblXXX

for i in $(ls def)
do 
new= $(grep $i list.csv | cut -d ',' -f2)
sed "/NODE/ s/$i/$new/g" def/$i
done

When each def/* file only has one /NODE/ line, you can try a nested sed . 当每个def/*文件只有一个/NODE/行时,您可以尝试使用嵌套的sed
Remember that sed can use other characters like , or # when you don't want the / . 请记住, sed可以使用其他字符,如,#当你不希望/

# First look at the generated commands by
sed 's#.*#/NODE/ s,&,#' list.csv
# next try
sed -f <(sed 's#.*#/NODE/ s,&,#' list.csv) def/*
# Make backup before changing
cp -r def def_bak
# Get serious
sed -if <(sed 's#.*#/NODE/ s,&,#' list.csv) def/*

When you are not sure about this (overlapping files like PO1 and PO12), you can loop through the files, starting with your csv. 如果不确定(重叠的文件,如PO1和PO12),则可以从csv开始遍历文件。

while IFS= read -f line; do
   test -f def/"${line%,*}" || continue
   sed -i "s,${line}," def/"${line%,*}"
done < list.csv

you have too much blah blah and lacking testable input files, but I think this will do 你等等太多了,缺少可测试的输入文件,但是我认为这可以做到

$ awk 'NR==FNR{a[$1]=$2; next} {f=FILENAME; sub(f,a[f]); print > f".mod"}' list.csv def/*

this will create ".mod" extensions for the processed files. 这将为处理的文件创建“ .mod”扩展名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM