I am new in unix and need to do followings: I have 10 files with name:
t1_20160322_load_8977854_49843489_1.xml
t1_20160322_load_8977855_47536364_1.xml
t1_20160322_load_8977856_93435735_1.xml
t1_20160322_load_8977857_78637575_1.xml
t1_20160322_load_8977858_97437542_1.xml
....
and I received one a file (loadnumber.txt) with following content:
8977854;EUROLOADSAMPLE;c
8977855;ASIENLOADSAMPLE;i
8977856;AFRICANLOADSAMPLE;s
8977857;USALOADSAMPLE;l
8977858;CANADALOADSAMPLE;o
I need to replace the number with value from the text file, for example the file t1_20160322_load_8977854_49843489_1.xml should be changed to
t1_EUROLOADSAMPLE_c.xml
and the file t1_20160322_load_8977854_49843489_1.xml should be changed to t1_ASIENLOADSAMPLE_i.xml
I tried to google how to write the unix commando, but I think I am missing the basic knowledge, so I wrote followings:
for f in $(t1*xml);do;for x in $(cat contracts_generator.txt);do;g=$(echo $x | cut -d\; -f1); n=$(echo $x | cut -d\; -f2); m=$(echo $x | cut -d\; -f3);mv $f t1_$n.xml;done;done
This didn't work of course. Can you please help me to figure out, how I should write the code?
Thank you so much for your answers in advance.
If you want to understand something, you should create a shell script to handle your issue. Create a file called myscript.sh with your content.
Your where not too far, so I fixed it so:
for f in $(ls t1*xml)
do
#echo $f
mymatch=$(echo $f |cut -d"_" -f4)
#echo $mymatch
for x in $(cat contracts_generator.txt)
do
g=$(echo $x | cut -d\; -f1)
if [[ "$g" == "$mymatch" ]]
then
n=$(echo $x | cut -d\; -f2)
m=$(echo $x | cut -d\; -f3)
fi
done
mv ${f} t1_${n}_${m}.xml
done
(remove #
before the echo
for debug)
Then call your script so:
sh myscript.sh