LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash shell script read file word by word part 2 (http://www.linuxquestions.org/questions/programming-9/bash-shell-script-read-file-word-by-word-part-2-a-858382/)

justina 01-24-2011 12:49 PM

bash shell script read file word by word part 2
 
I have a output file look like this:
test1
test2
test3
test4

I have a text file look like this:
<url> /text.doc&url=$word1</url>
<url> /text.doc&url=$word2</url>
<url> /text.doc&url=$word3</url>
<url> /text.doc&url=$word4</url>

How can I read output file and past test1 in $word1 in this text file.

This is Part 1 code:
#!/bin/bash
while IFS='{:,}' read -a array
do
item="${array[3]# \"}"
echo "${item%\"*}"
done < /path/to/filename

Snark1994 01-24-2011 06:30 PM

You could probably use bash to do this, but I like Python, and it seems to be rarely used to solve problems... Schmeh, here goes...
Code:

#!/usr/bin/python
import re

output = ""

words = []
replacementsFile = open("repfile")
for word in replacementsFile:
    words.append(word.rstrip("\n"))
replacementsFile.close()

wordToReplace = re.compile("\$word\d")

htmlFile = open("htmlfile")
for line in htmlFile:
    output += wordToReplace.sub(line,words.pop(0))
htmlFile.close()

outputFile = open("outputFile","w")
outputFile.write(output)
outputFile.close()

Disclaimer: this hasn't been tested, but it should work :) *grins in trustworthy manner*

However, that's horrible and kludgey because your problem is. Is every line in the output file going to be in the same format? Why not just read in the first file, have the HTML coded into the programme and just output the lines? You need very little modification to the bash code you have already in order to do that - just a couple of "echo" statements. You seem to have unnecessary steps... Apologies in advance if I've actually just misunderstood you ;)

ghostdog74 01-24-2011 07:21 PM

Quote:

Originally Posted by Snark1994 (Post 4236552)
You could probably use bash to do this, but I like Python, and it seems to be rarely used to solve problems... Schmeh, here goes...
Code:

#!/usr/bin/python
...
words = []
replacementsFile = open("repfile")
for word in replacementsFile:
    words.append(word.rstrip("\n"))
replacementsFile.close()
..


Code:

words = open("repfile").readlines()
words = [ i.rstrip() for i in words ]
# words = [ i.rstrip() for i in open("repfile").readlines() ]

Quote:

Originally Posted by Snark1994 (Post 4236552)
Code:

outputFile = open("outputFile","w")
outputFile.write(output)
outputFile.close()


Code:

open("outputFile","w").write(output)

justina 01-24-2011 07:41 PM

This will overwrite my output text file.

Code:

#!/usr/bin/bash/python

import re

output = ""

words = []

words = open("test").readlines()
words = [ i.rstrip() for i in words ]
print words

wordToReplace = re.compile("\$word\d")

htmlFile = open("test2")
for line in htmlFile:
    output += wordToReplace.sub(line,words.pop(0))
    print output
htmlFile.close()

open("test2","w").write(output)

Is there any error in output?

ghostdog74 01-24-2011 08:14 PM

Quote:

Originally Posted by justina (Post 4236582)
This will overwrite my output text file.

if you don't want to overwrite your original html file, then create a new one.
Code:

open("test3","w").write(output)
and are you sure your shebang is correct?
Code:

#!/usr/bin/bash/python
better change it to
Code:

#!/usr/bin/env python

justina 01-24-2011 08:23 PM

Yes, this is correct /usr/bin/python
I have check whereis command

Snark1994 01-25-2011 10:46 AM

Thanks for the comments ghostdog :)

justina, ghostdog was just pointing out that you had "#!/usr/bin/bash/python" rather than "#!/usr/bin/python" in your script.

justina 01-25-2011 02:19 PM

I got the find and replace code.
Now how can I make a loop test1, test2, etc.
and replace to word1, word2, etc...

Code:

sed -e 's/$word1/test1/g' test2.txt


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