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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
01-24-2011, 11:49 AM
|
#1
|
LQ Newbie
Registered: Jan 2011
Posts: 10
Rep:
|
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
|
|
|
01-24-2011, 05:30 PM
|
#2
|
Senior Member
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
|
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 
Last edited by Snark1994; 01-24-2011 at 05:33 PM.
|
|
|
01-24-2011, 06:21 PM
|
#3
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by Snark1994
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
Code:
outputFile = open("outputFile","w")
outputFile.write(output)
outputFile.close()
|
Code:
open("outputFile","w").write(output)
Last edited by ghostdog74; 01-24-2011 at 06:23 PM.
|
|
1 members found this post helpful.
|
01-24-2011, 06:41 PM
|
#4
|
LQ Newbie
Registered: Jan 2011
Posts: 10
Original Poster
Rep:
|
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?
|
|
|
01-24-2011, 07:14 PM
|
#5
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by justina
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
Last edited by ghostdog74; 01-24-2011 at 07:17 PM.
|
|
|
01-24-2011, 07:23 PM
|
#6
|
LQ Newbie
Registered: Jan 2011
Posts: 10
Original Poster
Rep:
|
Yes, this is correct /usr/bin/python
I have check whereis command
|
|
|
01-25-2011, 09:46 AM
|
#7
|
Senior Member
Registered: Sep 2010
Distribution: Debian
Posts: 1,632
|
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.
|
|
|
01-25-2011, 01:19 PM
|
#8
|
LQ Newbie
Registered: Jan 2011
Posts: 10
Original Poster
Rep:
|
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 08:50 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|