Editing/Replacing content in multiple files in Uni

2019-01-12 08:39发布

问题:

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.



标签: unix rename vi