LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-21-2012, 03:46 PM   #16
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled

Ive been testing this in a folder and it doesnt work.

I created a .txt file and added <process></process> to it several times. Pointed the terminal to the .sh file and ran it. No increment. All it does is duplicate the file to results.txt.txt .

Quote:
#!/bin/bash

while IFS= read -r -d '' file
do
i=1
while IFS= read -r -u 3 line
do
if [[ $line = '<process></process>' ]]; then
echo "<process>$((i++))\</process>"
else
echo "$line"
fi
done 3< "$file" > "$file".txt
done < <(find -type f -name \*.txt -print0)
what I put in the results.txt file:
Code:
<process></process>
<process></process>
<process></process>
<process></process>
<process></process>
 
Old 11-22-2012, 05:06 AM   #17
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
Tests OK here:
Code:
c@CW9:~/d/bin/try/LQ$ cp LQ-4175437116.xml /tmp
c@CW9:~/d/bin/try/LQ$ cat LQ-4175437116.xml
jfksaJDFH
<process></process>
<process></process>
<process></process>
<process></process>
jdhkjasdh
<process></process>
<process></process>
<process></process>
<process></process>
jfhksdjf
<process></process>
c@CW9:~/d/bin/try/LQ$ cat LQ-4175437116.sh
#!/bin/bash

dir=/tmp

while IFS= read -r -d '' file
do
    i=1
    while IFS= read -r -u 3 line
    do
        if [[ $line = '<process></process>' ]]; then
           echo "<process>value=\"$((i++))\"</process>"
        else
           echo "$line"
        fi
    done 3< "$file" > "$file.done"
done < <(find $dir -type f -name \*.xml -print0)
c@CW9:~/d/bin/try/LQ$ ./LQ-4175437116.sh
c@CW9:~/d/bin/try/LQ$ cat /tmp/LQ-4175437116.xml.done
jfksaJDFH
<process>value="1"</process>
<process>value="2"</process>
<process>value="3"</process>
<process>value="4"</process>
jdhkjasdh
<process>value="5"</process>
<process>value="6"</process>
<process>value="7"</process>
<process>value="8"</process>
jfhksdjf
<process>value="9"</process>
 
Old 11-23-2012, 12:00 PM   #18
graphicsmanx1
Member
 
Registered: Oct 2012
Posts: 81

Original Poster
Rep: Reputation: Disabled
I think I understand part of it now!!! Got it to work with this:

Code:
#!/bin/bash

dir="/mnt/Desktop/test"

while IFS= read -r -d '' file
do
    i=1
    while IFS= read -r -u 3 line
    do
        if [[ $line = '<process></process>' ]]; then
           echo "<process>value=\"$((i++))\"</process>"
        else
           echo "$line"
        fi
    done 3< "$file" > "$file.xml"
done < <(find $dir -type f -name \*.xml -print0)

but when I change this line < "$file" > "$file.xml" to < "$file" >> "$file.xml" it just repeats inside the file. How could I change it to rename back to the file name?? Like options.xml will go back to options.xml. What is happening is I get options.xml.xml now. I thought about a -f rename but any better options??

Last edited by graphicsmanx1; 11-23-2012 at 01:42 PM.
 
Old 11-23-2012, 11:23 PM   #19
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
You are right; the file needs renaming after the sample script has run.

It is good practice while developing scripts and when posting them onn public forums not to overwrite the original input file. That's why I put the output in "$file.done".

Once you know the script is doing what you want you can rename "$file.done" as "$file".

It would serve nobody for me to tell you how to rename a file. That is a basic shell and command line technique that can be found in many introductory tutorials and easily found by searching the 'net. Valuable knowledge would be gained by reading such tutorials and searching the 'net.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[bash/awk] How to increment hour in a loop? wasim_jd Linux - Newbie 4 12-08-2011 12:59 PM
Bash shell script: Str(007) to int(7),increment it(8) & convert back to string(008) drwatson_droid Programming 5 12-01-2011 11:44 AM
how to increment the date in a file name via bash script tensigh Programming 10 07-23-2010 04:49 AM
[Bash] increment number in a string czezz Programming 4 07-01-2009 12:34 PM
bash equivalent to C increment operator andrewb758 Linux - General 5 02-21-2009 01:25 PM


All times are GMT -5. The time now is 07:07 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration