LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 01-24-2011, 11:49 AM   #1
justina
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Rep: Reputation: 0
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
 
Old 01-24-2011, 05:30 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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.
 
Old 01-24-2011, 06:21 PM   #3
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by Snark1994 View Post
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 View Post
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.
Old 01-24-2011, 06:41 PM   #4
justina
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Original Poster
Rep: Reputation: 0
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?
 
Old 01-24-2011, 07:14 PM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by justina View Post
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.
 
Old 01-24-2011, 07:23 PM   #6
justina
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Original Poster
Rep: Reputation: 0
Yes, this is correct /usr/bin/python
I have check whereis command
 
Old 01-25-2011, 09:46 AM   #7
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,630
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
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.
 
Old 01-25-2011, 01:19 PM   #8
justina
LQ Newbie
 
Registered: Jan 2011
Posts: 10

Original Poster
Rep: Reputation: 0
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
 
  


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
[SOLVED] bash shell script read file word by word. justina Programming 15 01-22-2011 10:12 AM
How can i read two files word by word at a time using any loop by shell script? vaibhavs17 Programming 16 03-19-2010 03:48 AM
word by word comparison in two files using loop in shell script vaibhavs17 Programming 2 03-05-2010 07:41 AM
BASH: Grepping/sedding/etc out part of a file... (from one word to 'blank' line) elinenbe Programming 2 12-11-2008 01:17 PM
How to read ans parse MS word file using a Linux Shell script. Alek Linux - General 2 11-10-2003 02:07 PM


All times are GMT -5. The time now is 04:05 PM.

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