Easy script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Easy script
# 1  
Old 02-08-2005
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 :-)
# 2  
Old 02-08-2005
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

Code:
#!/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
# 3  
Old 02-08-2005
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 :-)
# 4  
Old 02-22-2005
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 :-)
# 5  
Old 02-22-2005
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...
Code:
#!/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
# 6  
Old 02-22-2005
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
# 7  
Old 02-22-2005
"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 :-)
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Easy script

Write a script, which of the directory including sources of kernel, choose files with sources in C (files with .c extension), including in name "io" with dirvers (located in random subdirectory drivers) and place their content in file FILE. (to 20 lines). Could someone help me with this task?... (1 Reply)
Discussion started by: Czabi
1 Replies

2. Shell Programming and Scripting

Opinion on an easy shell script (mv)

:wall:I've this simple code: STF=/opt/aaa cat $STF | nice sort -u > $STF.new && mv $STF.new $STF Which works until today. What happened is that this script has been corrupted the FS, so I've to use fschk to repair the filesystem. I presume the move command executed just a little too early... (1 Reply)
Discussion started by: accolito
1 Replies

3. Shell Programming and Scripting

I need help to write easy shell script

Hello every one :D I am very new in Linux ... that why I do not have any idea to write the script :confused: I am trying to read some tutorial , but I do not have enough time to do it ! because I have to submit my project results in next Wednesday I need your help to write script ! I will... (2 Replies)
Discussion started by: seereen
2 Replies

4. UNIX for Dummies Questions & Answers

Easy About Bash Script function

Hi to all, first of all,i am working on MINIX 3 OS. I want to create a bash script file,which will create a list of files(not directories) that have been modified one specific day (i.e today) under my home directory. thank you very much!! :) (8 Replies)
Discussion started by: kostis1904
8 Replies

5. Shell Programming and Scripting

Easy Checkup Script

Hi, I am planning the following to do. On my linux system I've got different users in the /home/ directory. These users have file limitations. So every user below the /home/ directory should get a text file in a seperate folder /home/$user/files/ which tells him how many files he is already... (2 Replies)
Discussion started by: crusher
2 Replies

6. Shell Programming and Scripting

My first script of day, it looks easy, but...

I've began my journey at 7:50, and at this time i've lost 40 minutes in this easy but :confused::eek:-script. Someone can help me ? I want to see the differences beetween all xml files in two directories (they must be equals), and this is my script: #!/bin/bash dir1="261108"... (5 Replies)
Discussion started by: trutoman
5 Replies

7. UNIX for Dummies Questions & Answers

Need help on installing an EASY to use and easy to install command line text editor

Hi again. Sorry if it seems like I'm spamming the boards a bit, but I figured I might as well ask all the questions I need answers to at once, and hopefully at least get some. I have installed Solaris 10 on a server. The default text editors are there (vi, ex, ed, maybe others, I know emacs is... (4 Replies)
Discussion started by: EugeneG
4 Replies

8. UNIX for Dummies Questions & Answers

Help with an 'easy' script

Hello guys, Forgive me if I duplicate the post but i think i make a mistake choosing the correct forum. I need some help to solve a little problem, I have a big file with almost 100.000 lines of data, here is an example of line: 100099C01101C00000000059399489283CREMOVISTAR_TX ... (3 Replies)
Discussion started by: lestat_ecuador
3 Replies

9. Shell Programming and Scripting

Help with an 'easy' script

Hello guys, I need some help to solve a little problem that I have here: I have a big file with almost 100.000 lines of data, here is an example of line: 100099C01101C00000000059399489283CREMOVISTAR_TX 010001C00000000000099069799MOVISTAR_Tx ... (7 Replies)
Discussion started by: lestat_ecuador
7 Replies

10. Shell Programming and Scripting

easy script

a script, cheer that prints its parameter as shown in the example below. eg: $ cheer U N I X Give me a U! U! Give me a N! N! Give me a I! I! Give me a X! X! #!/bin/sh for letter do echo "Give me a $letter!";echo "$letter!" done this is the code i used for the above script (2 Replies)
Discussion started by: problems
2 Replies
Login or Register to Ask a Question