1848726 Members
4148 Online
104036 Solutions
New Discussion

Upload file

 
SOLVED
Go to solution
peterchu
Super Advisor

Upload file

I ask this question before , but I have one more requirement.

I want to replace all the word "SUCCESS" to "FAIL" in all files under the directory /home ( include all sub-directories ) , what can I do ? thx in advance.
6 REPLIES 6
Dave Olker
Neighborhood Moderator

Re: Upload file

Hi Peter,

Here's a really quick/dirty way to do it. I'm sure there are many more elaborate ways, and several more checks you can add to make it work to your liking:

for FILE in $(find /home/.)
do
if [ ! -x $FILE ]
then
grep "SUCCESS" $FILE > /dev/null 2>&1

if [ $? -eq 0 ]
then
cat $FILE | sed "s/SUCCESS/FAIL/g" > $FILE.new
fi
fi
done


This script checks to see that the file is not an executable and it ensures that the string "SUCCESS" appears in the file before modifying it. The original file is left intact and a new file with a ".new" extension is created.

Good luck,

Dave


I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
R. Sri Ram Kishore_1
Respected Contributor
Solution

Re: Upload file

Hi,

Take a look at this link:
http://www.dslreports.com/forum/remark,10607506~mode=flat

HTH.
Regards,
Sri Ram
"What goes up must come down. Ask any system administrator."
peterchu
Super Advisor

Re: Upload file

thx replies,

I try the methods is ok, but it will copy a backup to $FILE.bak , how can I directly update the files instead of make a backup file ? thx.
Dave Olker
Neighborhood Moderator

Re: Upload file

Hi Peter,

If you wanted to use the logic I posted previously, just change the line:

cat $FILE | sed "s/SUCCESS/FAIL/g" > $FILE.new

to

cat $FILE | sed "s/SUCCESS/FAIL/g" > $FILE


That will replace the existing file with the modified content.

Regards,

Dave


I work at HPE
HPE Support Center offers support for your HPE services and products when and how you need it. Get started with HPE Support Center today.
[Any personal opinions expressed are mine, and not official statements on behalf of Hewlett Packard Enterprise]
Accept or Kudo
Ermin Borovac
Honored Contributor

Re: Upload file

Here's one-liner with perl

find /home -type f -exec perl -pi -e 's,SUCCESS,FAIL,g' {} \;

Be careful with

cat $FILE | sed "s/SUCCESS/FAIL/g" > $FILE

I think shell will process redirections before executing any commands. Which means that $FILE will get clobbered before cat gets to it.
Muthukumar_5
Honored Contributor

Re: Upload file

We can do it with sed / perl here.

Perl is easy to do as,

perl -p -i -e 's/SUCCESS/FAIL/g' filename.

You can collect all files using find command as,

find -name "filenamepattern" -exec perl -p -i -e 's/SUCCESS/FAIL/g' {} \;


You can do it with sed as

echo "sed -e 's/SUCCESS/FAIL/g' filename" > filename

It will do the changes for one file with using temp. files.

You can do on multiple files as,


grep -l "SUCCESS" | awk '{ print "echo \"`sed -e 's/SUCCESS/FAIL/g' "$0 "`\" > "$1 }' | sh

It will change all patterns of SUCCESS on all files. If you want to know what is happeneing just remove | sh on execution it will print single file execution statements for all files.

Regards
Muthu
Easy to suggest when don't know about the problem!