Loop through text file > Copy Folder > Edit XML files in bulk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop through text file > Copy Folder > Edit XML files in bulk?
# 1  
Old 04-27-2011
Loop through text file > Copy Folder > Edit XML files in bulk?

I have a text file which contains lines in this format - it contains 105 lines in total, but I'm just putting 4 here to keep it short:
Code:
58571,east_ppl_ppla_por
58788,east_pcy_hd_por
58704,east_pcy_ga_por
58697,east_pcy_pcybs_por


It's called id_key.txt

I have a sample folder called start_folder

The folder contains 2 xml files:
Code:
SearchBoxRN.xml
ShoppingHomePG.xml


Each xml file contains, amongst other things, the following text:
Code:
user:responsibilityKey="east_ppl_ppl_por"


I'd really like to be able to loop through the id_key.txt file, and for each line - e.g. the first line in id_key.txt:

Copy the start_folder and its contents
  1. Rename the folder from "start_folder" to the first bit of the current line from the id_key.txt file - e.g. 58571
  2. Update SearchBoxRN.xml and change user:responsibilityKey="east_ppl_ppl_por" to user:responsibilityKey="east_ppl_ppla_por"
  3. Update ShoppingHomePG.xml and change user:responsibilityKey="east_ppl_ppl_por" to user:responsibilityKey="east_ppl_ppla_por"
This seems like a long shot to me, but I'm no expert.

I've tried looking at using a windows batch file on a PC, but we also have a unix environment at work, and if it's possible to do this task in UNIX, then that'd be great, because the alternative involves manually editing 525 XML files by hand, and creating 315 folders...

I'm afraid I am a real novice with unix. I can do simple stuff like copy files, delete files, but have never done anything with vi or other unix text editors.

Any advice much appreciated.

Thanks!
# 2  
Old 04-27-2011
If you have sed -i:
Code:
while IFS="," read N STR
do
        cp -R /path/to/start_folder /path/to/$N
        sed -i "s/east_ppl_ppl_por/$STR/g" /path/to/$N/SearchBoxRN.xml
        sed -i "s/east_ppl_ppl_por/$STR/g" /path/to/$N/ShoppingHomePG.xml
done < /path/to/id_key.txt

# 3  
Old 04-27-2011
Code:
$ cat id_key.txt
58571,east_ppl_ppla_por
58788,east_pcy_hd_por
58704,east_pcy_ga_por
58697,east_pcy_pcybs_por

$ cat  SearchBoxRN.xml
58571:responsibilityKey="east_ppl_ppl_por"
58704:responsibilityKey="east_ppl_ppl_por"
58697:responsibilityKey="east_ppl_ppl_por"
58788:responsibilityKey="east_ppl_ppl_por"
58541:responsibilityKey="east_ppl_ppl_por"
58371:responsibilityKey="east_ppl_ppl_por"

$ awk -F , 'NR==FNR{a[$1]=$2;next} 
        /responsibilityKey/ {if ($1 in a) $2="responsibilityKey=\"" a[$1] "\""}1' id_key.txt FS=":" OFS=":" SearchBoxRN.xml

58571:responsibilityKey="east_ppl_ppla_por"
58704:responsibilityKey="east_pcy_ga_por"
58697:responsibilityKey="east_pcy_pcybs_por"
58788:responsibilityKey="east_pcy_hd_por"
58541:responsibilityKey="east_ppl_ppl_por"
58371:responsibilityKey="east_ppl_ppl_por"

# 4  
Old 04-28-2011
Thanks for your help with this.

I created this .sh file:

Code:
while IFS="," read N STR
do
        cp -R /home/1320238/test1/start_folder /home/1320238/test1/$N
        sed -i "s/east_ppl_ppl_por/$STR/g" /home/1320238/test1/$N/SearchBoxRN.xml
        sed -i "s/east_ppl_ppl_por/$STR/g" /home/1320238/test1/$N/ShoppingHomePG.xml
done < /home/1320238/test1/id_key.txt

When I ran it, I got this output:

Code:
1320238:PLAY$ test.sh
sed: Not a recognized flag: i
Usage:  sed [-n] [-u] Script [File ...]
        sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...]
sed: Not a recognized flag: i
Usage:  sed [-n] [-u] Script [File ...]
        sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...]
sed: Not a recognized flag: i
Usage:  sed [-n] [-u] Script [File ...]
        sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...]
sed: Not a recognized flag: i
Usage:  sed [-n] [-u] Script [File ...]
        sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...]
sed: Not a recognized flag: i
Usage:  sed [-n] [-u] Script [File ...]
        sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...]
sed: Not a recognized flag: i
Usage:  sed [-n] [-u] Script [File ...]
        sed [-n] [-u] [-e Script] ... [-f Script_file] ... [File ...]
1320238:PLAY$

The .sh file did do something though - it created a folder for each of the following entries from the id_key.txt file:

58571,east_ppl_ppla_por
58788,east_pcy_hd_por
58704,east_pcy_ga_por
58697,east_pcy_pcybs_por

Though not for the last line for some reason.

However, it didn't update the "east_ppl_ppl_por" with the value from the text file. Is that because the "i" flag isn't recognised by "sed"?

Does that mean the "sed" route won't work?

I also tried the method suggested by "rcdwayx", but got this output:

Code:
1320238:PLAY$ test2.sh
test2.sh: $:  not found.
test2.sh[2]: 58571,east_ppl_ppla_por:  not found.
test2.sh[3]: 58788,east_pcy_hd_por:  not found.
test2.sh[4]: 58704,east_pcy_ga_por:  not found.
test2.sh[5]: 58697,east_pcy_pcybs_por:  not found.
test2.sh[7]: $:  not found.
test2.sh[8]: 58571:responsibilityKey=east_ppl_ppl_por:  not found.
test2.sh[9]: 58704:responsibilityKey=east_ppl_ppl_por:  not found.
test2.sh[10]: 58697:responsibilityKey=east_ppl_ppl_por:  not found.
test2.sh[11]: 58788:responsibilityKey=east_ppl_ppl_por:  not found.
test2.sh[12]: 58541:responsibilityKey=east_ppl_ppl_por:  not found.
test2.sh[13]: 58371:responsibilityKey=east_ppl_ppl_por:  not found.
test2.sh[15]: $:  not found.
test2.sh[18]: 58571:responsibilityKey=east_ppl_ppla_por:  not found.
test2.sh[19]: 58704:responsibilityKey=east_pcy_ga_por:  not found.
test2.sh[20]: 58697:responsibilityKey=east_pcy_pcybs_por:  not found.
test2.sh[21]: 58788:responsibilityKey=east_pcy_hd_por:  not found.
test2.sh[22]: 58541:responsibilityKey=east_ppl_ppl_por:  not found.
test2.sh[23]: 58371:responsibilityKey=east_ppl_ppl_por:  not found.

I'm not sure what that means?

Any advice much appreciated.

Thanks!

Last edited by biscuitcreek; 04-28-2011 at 08:08 AM..
# 5  
Old 04-28-2011
Hope this will help you

Code:
for i in `cat id_key.txt`
do

#capture the values in the variables
first_bit=`echo $i | awk -F ',' '{print $1}'`
second_bit=`echo $i| awk -F ',' '{print $2}'`
#make sure that variables  are not null
if [ ! -z $first_bit  -a  ! -z $second_bit  ]
then
cp -R  ./start_folder $first_bit
sed s/east_ppl_ppl_por/$second_bit/ ./start_folder/SearchBoxRN.xml > $first_bit/SearchBoxRN.xml
sed s/east_ppl_ppl_por/$second_bit/ ./start_folder/ShoppingHomePG.xml > $first_bit/ShoppingHomePG.xml
fi

done

# 6  
Old 04-28-2011
Thanks for the suggestion / advice!

I created a new "test3.sh" file:

Code:
for i in 'cat id_key.txt'
do

#capture the values in the variables
first_bit=`echo $i | awk -F ',' '{print $1}'`
second_bit=`echo $i| awk -F ',' '{print $2}'`
#make sure that variables  are not null
if [ ! -z $first_bit  -a  ! -z $second_bit  ]
then
cp -R  ./start_folder $first_bit
sed s/east_ppl_ppl_por/$second_bit/ ./start_folder/SearchBoxRN.xml > $first_bit/SearchBoxRN.xml
sed s/east_ppl_ppl_por/$second_bit/ ./start_folder/ShoppingHomePG.xml > $first_bit/ShoppingHomePG.xml
fi

But get this error:

test3.sh: 0403-057 Syntax error at line 1 : `for' is not matched.

Do you know what that means?

Thanks again for your help
# 7  
Old 04-28-2011
I think you havent copied the code completly , you have missed copying "done" .
This User Gave Thanks to palanisvr For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to write in other language in text/xml file by reading english text/xml file using C++?

Hello Team, I have 2 files.one contains english text and another contains Japanese. so i have to read english text and replace the text with Japanesh text in third file. Basically, I need a help to write japanese language in text/xml file.I heard wstring does this.Not sure how do i write... (2 Replies)
Discussion started by: SA_Palani
2 Replies

2. Shell Programming and Scripting

Copy and paste text inside a xml file

I have a really big XML file. I need copy the value of one tag inside another one tag. I try to publish one example. <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1">Rai 1</channel> <channel update="i" site="merge-xmltv" site_id="" xmltv_id="Rai 1 +2HD">Rai 1... (6 Replies)
Discussion started by: Tapiocapioca
6 Replies

3. Shell Programming and Scripting

How to copy files with the same filenames as those in another folder to that same folder?

Hello All A similar question like this was asked before but I need to change part of the question. I've two folders, Folder A contains some image files in 150 subfolders; Folder B contains text files in 350 subfolders. All image files in Folder A have the same filename as the text... (5 Replies)
Discussion started by: chlade
5 Replies

4. Shell Programming and Scripting

How to copy all the contents of a list of files present in a folder to a particular file?

Hi All, I want to copy all the contents of a list of files in a folder to a particular file. i am using following command: cat dir/* >> newFile.txtIt's not working. Could you please help? Thanks, Pranav (3 Replies)
Discussion started by: Pranav Bhasker
3 Replies

5. Shell Programming and Scripting

Shell or perl script to replace XML text in bulk

Hi, I am looking for assistance over shell or perl (without XML twig module) which replace string in XML file under particular branch..example of code file sample.. Exact requirment : Replace "Su saldo es" in below file with "Your balance" but only in XML branch of Text id=98 and Text Id=12... (7 Replies)
Discussion started by: Ashu_099
7 Replies

6. UNIX for Dummies Questions & Answers

copy files grabbing destination folder from file name

Hi all... Below is what I am trying to do: 1. Having the following folder with the files... /source_folder/dodiddone.tar.gz /source_folder/gowentgone.tar.gz /source_folder/gowentgone.log 2. I need to copy and chown files with extension .tar.gz to another folder copy... (1 Reply)
Discussion started by: pedroz
1 Replies

7. Homework & Coursework Questions

copy files inside a text file

Hi Guys , I am new to this and Hi to all ,Need your help I am trying to copy Files which are inside file.txt The files inside file.txt are inthe below order file1.log file2.log file3.log ....... I want to copy these files to an output Directory , Please help (1 Reply)
Discussion started by: hc17972
1 Replies

8. Shell Programming and Scripting

Find all text files in folder and then copy to a new folder

Hi all, *I use Uwin and Cygwin emulator. I´m trying to search for all text files in the current folder (C/Files) and its sub folders using find -depth -name "*.txt" The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with ... (4 Replies)
Discussion started by: cgkmal
4 Replies

9. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies

10. UNIX for Dummies Questions & Answers

grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so... (7 Replies)
Discussion started by: coppertone
7 Replies
Login or Register to Ask a Question