Good Day!
I have files in Ubuntu like:
- ZAF_MM_CYCLE_K051.XLS
- ZAF_MM_CYCLE_K052.XLS
- ZAF_MM_CYCLE_K053.XLS
which are copy of a file "ZAF_MM_CYCLE_K036", the code of file is K036
File contents are the same
LOADED_AGRS ZAF_MM_CYCLE_K036
AGG_DEFINE 200ZAF_MM_CYCLE_K036 $WERKS K036
AGG_1521 200ZAF_MM_CYCLE_K036
Here I have to replace the code wiith its own code according to filename.
for example:
file: ZAF_MM_CYCLE_K051.XLS
its code is K051, and I have to replace K036 to K051, so that content of the file would be:
LOADED_AGRS ZAF_MM_CYCLE_K051
AGG_DEFINE 200ZAF_MM_CYCLE_K051 $WERKS K051
AGG_1521 200ZAF_MM_CYCLE_K051
Can anyone help out, please?
2
Answers
As Ron mentioned in the comment, you can do something like this with bash to generate the files with modified content that matches the index:
The above will generate the files as described in the current directory. The
cat > ZAF_MM_CYCLE_K${i}.XLS << EOL
uses a heredoc to generate the file. The filename and contents use the$i
variable for the names. As the for loop executes,i
gets incremented, i.e.036
,037
,038
,…051
etc. which is used to generate the file name and the contents accordingly.If you want to use something like
sed
to replace contents. Running the following in the directory where the files are should do the trick:Basically, we iterate over each file, set a variable name that contains the number from the filename by stripping the
ZAF_MM_CYCLE_K
and the.XLS
using bash parameter expansion. Once the number is deduced, use sed to replace the contents on the given file.sed -i
does an in-place replacement on the original file.Hope this helps!
If all the files contain K036 the below script will help you.
where the
will extract the file name k051 from the filename
will replace the old value with new one
So this would help you in your case.