Editing/Replacing content in multiple files in Unix AIX without opening it

耗尽温柔 提交于 2019-11-26 17:21:43

问题


I have multiple files in Unix directory. files names are as below.
EnvName.Fullbkp.schema_121212_1212_Part1.expd EnvName.Fullbkp.schema_121212_1212_Part2.expd EnvName.Fullbkp.schema_121212_1212_Part3.expd

In each of the above file there is a common line like below. eg

EnvName.Fullbkp.schema_121212_1212_Part1.expd

is having below data

Log=EnvName.Fullbkp.schema_10022012_0630_Part1.log  
file=EnvName.Fullbkp.schema_10022012_0630_Part1.lst  

EnvName.Fullbkp.schema_121212_1212_Part2.expd

is having below data

Log=EnvName.Fullbkp.schema_10022012_0630_Part2.log  
file=EnvName.Fullbkp.schema_10022012_0630_Part2.lst

I want to replace the 10022012_0630 from EnvName.Fullbkp.schema_121212_1212_Part*.expd files with 22052013_1000 without actully opening those files. Changes should happen in all EnvName.Fullbkp.schema_121212_1212_Part*.expdp files in a directory at a time


回答1:


Assuming you mean you don't want to manually open the files:

sed -i 's/10022012_0630/22052013_1000/' filename*.log

update: since the "-i" switch is not available on AIX, but assuming you have ksh (or a compatible shell):

mkdir modified
for file in filename*.log; do
    sed 's/10022012_0630/22052013_1000/' "$file" > modified/"$file"
done

Now the modified files will be in the modified directory.




回答2:


It's some kind of extreme optimist who suggests sed -i on AIX.

It's a bit more likely that perl will be installed.

perl -pi -e 's/10022012_0630/22052013_1000/' EnvName.Fullbkp.schema_121212_1212_Part*.expd

If no perl, then you'll just have to do it like a Real Man:

for i in EnvName.Fullbkp.schema_121212_1212_Part*.expd
do
  ed -s "$i" <<'__EOF'
1,$s/10022012_0630/22052013_1000/g
wq
__EOF
done

Have some backups ready before trying these.



来源:https://stackoverflow.com/questions/16794584/editing-replacing-content-in-multiple-files-in-unix-aix-without-opening-it

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!