Script to firt find if a file exist's then change a path on that file.

I need a script that take a file in parameters.
The file in parameters looks like :

Line 1 : Path to file 1/name_file_1
Line 2: Path to file 1/name_file_2
Line 3: Path to file 3/name_file_3
Line 4: Path to file 4/name_file_4
Line 5: Path to file 5/name_file_5
Line 6: Path to file 6/name_file_6
And so on .
There is one path to file in each line end the line ended whith /file_name

The script should first extract on each line the name of the file only and then verify whether the file exist or not (ls / find)

If the file exist , modify a pattern "A" by the pattern "B" in all file using sed or other tools and write what was done in file named "res_ok.txt"
else
write in file "res_not_ok.txt" that the file don't exist.

Shell redirects happen before the command is run. So this EMPTIES the file $fname before the cat reads it. Then it copies the now-empty file onto itself, so it is still empty.

Then it empties all the other files, too.

Apart from that, the substitution is wrong too. tr does not replace pattern It replaces on single character with another fixed character.

Lessons to be learned here:

.. ALWAYS tar off your data securely before doing any major work on original data.

.. NEVER believe something is going to work blindly. The chances are about 20% it was maliciously written, 70% it was written by a clown, and 100% that it has at least one bug in it, apart from my own stuff, of course ;-).

.. BE SURE you understand what something is doing before you run it. It's your neck on the line, not the posters.

Incidentally, your request did not specify where patterns A and B come from, or whether they are the same for all the files, or depend on the file.

There is not much wrong with Sana's outline idea, apart from the issues
already noted (like clearing all the input files). However, I have some style
and method advice.

(a) I have put in more comments than I usually would.

(b) I am using awk to do the substitution, because the syntax is more
obvious if you need to change it, and because it will count the number
of changes made. Note the string you specified has a special character
in - the dot matches anything - so I have escaped this. Also, because of
the / in the string, I have quotes the pattern instead of the normal /.../
syntax.

(c) I declared all the file names up front to make them easier to change.

(d) I used a /tmp file for the corrected file. /tmp is very fast on some
systems (it's mapped into memory on Solaris, for example). You must
finish the conversion before copying back over the original file.

(e) I used the built-in clip syntax ## (in ksh and bash) for the filename,
rather than run it through an external program.

(f) I had awk send the count of substitutions to stderr, and captured it
in a shell variable.

(g) If there were no substitutions, then there is no need to copy back the
file, because it has not changed.

Tested on Puppy Linux 4.3.1, ksh93, and GNU awk, but should be very standard.

It does seem unusual to remove the path names from the list of files.
I assume you have collected them from their original directories first.

Here is the test report. Basically, the test data is the code itself,
with some of the matches made not to match by sticking a Q into the data.

Many thanks for your help.
Finally, the script had to change every pattern "/usr/lib/oscar\/1.2/" by
"logiciels/OSCAR/v1.2" in all the files.
The full path name to modify are all contained in the file named
"files2modif" , one file by line.

No need to find whether the files exists, because all of them are on the
tree of this application and at the right place as indicated by the file
"files2modif.

Glad it works for you, but I have to say your own script does not do what your specification asked for. It doesn't check if each file exists, and it doesn't report into either of the .txt files requested. And the . in the 1.2 part of the pattern is still a wildcard.

Copyright 1998-2015 Ziff Davis, LLC (Toolbox.com). All rights reserved. All product names are trademarks of their respective companies. Toolbox.com is not
affiliated with or endorsed by any company listed at this site.