Easy script

Hi there,

Could you please help me with that ?
I want to grep a specific string in an .xml file and then rename the file according to the string I found.
How can I do that for many files. Because all the files have similar names but different time stamps I want to name them so it's easy to know what is going on.

Thanks for your help in advcance

Aris :slight_smile:

Does the string you're grepping for change for each individual xml file? Because if you rename the file to whatever your searchterm is, you are just going to keep overwriting the file each time a new match is found.

You could try something along the lines of the following

#!/bin/bash

searchterm="something"
count=1

cd /where/my/xml/files/are

ls *.xml | while read file
do
   grep "${searchterm}" ${file} >/dev/null 2>&1
   if [ "$?" -eq "0" ]; then
      mv ${file} ${searchterm}_${count}.xml
      (( count = count + 1 ))
   fi
done

exit 0

Say if five .xml files contain the searchterm "something", these will be renamed something_1.xml through something_5.xml.

Is this what you want? If not, be a little more specific about exactly what it is you're trying to achieve.

Cheers
ZB

Thanks zazzybob,

That was really helpful. I was only needed a boast.
Next time I will be more specific.

Thanks again for your help. I think that script will also help some other posts I 've seen !

Aris :slight_smile:

Hi,

Actually I want to search more than one string and transform the file accordingly.
Here is my case:

Depending on the existance of the string in the file (on the left), I want the file to be renamed to its correspondence in the right.(see below)

The current file name consists of a prefix (data_exp_), date (year, month, day), time (hours, minutes, seconds), a 2-letter identifier, and the xml extension.
Example: data_exp_20030129123503aa.xml

Please notice that the original date and time in the file should be maintained in the final result.

PLMN-PLMN/RNC-201/ ----------> RNC_201_20050219091635.xml
PLMN-PLMN/RNC-202/ -----------> RNC_202_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/GGSN-1/ ------> GGSN_1_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/GGSN-2/ ------> GGSN_2_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/FW-1/ -------> FW_1_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/FW-2/ -------> FW_2_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/FW-3/ -------> FW_3_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/FW-4/ -------> FW_4_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/CG-1/ --------> CG_1_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/BG-1/ --------> BG_1_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/GNS-1/ -------> GNS_1_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/GNS-2/ --------> GNS_2_20050219091635.xml
PLMN-PLMN/SG3G-1/ -----------> SG3G_1_20050219091635.xml
PLMN-PLMN/HLR-106856/ -------------> HLR_106856_20050219091635.xml
PLMN-PLMN/MSC-106857/ -------------> MSC_106857_20050219091635.xml

Please help if you can.

Thanks in advance!

Aris :slight_smile:

The following script assumes a few things. Firstly, create a file called "list" in the same directory as your .xml files containing the information you posted above (as a space delimited file as per the example).

Then, create a script in the same directory containing the following...

#!/bin/sh

while read line
do
  ls | while read file
  do
    grep `echo "$line" | cut -d' ' -f1` $file
    if [ "$?" -eq "0" -a "$file" != "list" ]; then
       cp -p $file `echo "$line" | cut -d' ' -f3`
       rm $file
    fi
  done
done < list

Run the script, and the files will be renamed with timestamps preserved.

If this is not exactly what you want, please elaborate on the problem further.

Cheers
ZB

Thankx alot zazzybob.
Also:
The correspondence is the one bellow (without keeping the timestamps).
One other thing is that the file name should not be changed.
Meaning the original file kept and the one with the new name copied to a new directory.
For example to /var/opt/nokiaoss/monitor/nww/XML_NEW

RNC_201.xml
RNC_202.xml
GGSN_1.xml
GGSN_2.xml
FW_1.xml
FW_2.xml
FW_3.xml
FW_4.xml
CG_1.xml
BG_1.xml
DNS_1.xml
DNS_2.xml
SGSN_1.xml
HLR_106856.xml
MSC_106857.xml

"Firstly, create a file called "list" in the same directory as your .xml files containing the information you posted above (as a space delimited file as per the example).
"

Could please help me out with this. I don't quite understand ?

Thanx

Aris :slight_smile:

Create a file containing the information you posted exactly as you gave it earlier. Give the file the filename "list". Place the "list" file in the same directory as the .xml files.

e.g.

$ cat > list
PLMN-PLMN/RNC-201/ ----------> RNC_201_20050219091635.xml
PLMN-PLMN/RNC-202/ -----------> RNC_202_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/GGSN-1/ ------> GGSN_1_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/GGSN-2/ ------> GGSN_2_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/FW-1/ -------> FW_1_20050219091635.xml 
PLMN-PLMN/GPBB-1/GPST-1/FW-2/ -------> FW_2_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/FW-3/ -------> FW_3_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/FW-4/ -------> FW_4_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/CG-1/ --------> CG_1_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/BG-1/ --------> BG_1_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/GNS-1/ -------> GNS_1_20050219091635.xml
PLMN-PLMN/GPBB-1/GPST-1/GNS-2/ --------> GNS_2_20050219091635.xml
PLMN-PLMN/SG3G-1/ -----------> SG3G_1_20050219091635.xml
PLMN-PLMN/HLR-106856/ -------------> HLR_106856_20050219091635.xml
PLMN-PLMN/MSC-106857/ -------------> MSC_106857_20050219091635.xml
^D

As you can see, there are three space seperated fields, e.g.
Field 1: PLMN-PLMN/MSC-106857/
Field 2: ------------->
Field 3: MSC_106857_20050219091635.xml

Then, create a script in the same directory as the .xml files and the "list" file containing... (I've modified this as per your further requirements)...

#!/bin/sh

while read line
do
  ls | while read file
  do
    grep `echo "$line" | cut -d' ' -f1` $file
    if [ "$?" -eq "0" -a "$file" != "list" ]; then
       cp -p $file /var/opt/nokiaoss/monitor/nww/XML_NEW/`echo "$line" | cut -d' ' -f3`
    fi
  done
done < list

So if you do an ls in the directory, you should see all your xml files, the file named "list" and your script file (let's call this "foo.sh").

Now make foo.sh executable
$ chmod u+x foo.sh

Run foo.sh
$ ./foo.sh

And that should rename and copy all the files as per your requirements.

Hope this helps
Cheers
ZB

Thank you very mych indeed !

Aris :slight_smile: