Replace a word with string from another file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a word with string from another file
# 1  
Old 02-25-2009
MySQL Replace a word with string from another file

Hi,

this belongs a little to my other post [1] but only at the starting point.

With
Code:
find -name "*.htm*"

i got a list like this:

Code:
./1999/01/file1.html
./1999/01/file2.html
./1999/02/file1.html
./2000/04/file1.html
./2000/04/file2.html
./2000/04/file3.html
./2000/file1.html
./2000/file2.html
./file1.html
./file2.html
./file3.html

Then i wrote a awk script who gives me this output:

Code:
./1999 | 1999| 1999 | author | mail/01 | 01 | 01 | author | mail/<title> | file1.html | file1.html | author | mail
./1999 | 1999| 1999 | author | mail/ 01 | 01 | 01 | author | mail/<title>|  file2.html | file2.html | author | mail
./2000  | 2000| 2000 | author | mail/<title> | file1.html | file1.html | author | mail
./2000 | 2000| 2000 | author | mail/<title> | file2.html | file2.html | author | mail
./<title> | file1.html | file1.html | author | mail
./<title> | file2.html | file2.html | author | mail

Now i need to replace the "<title>"-part with the real content between the <title>-tag of this file.

I fetch the content with another script:

Code:
for i in `find -name "*.htm*"`
do
    awk -F'<title>' -v RS='</title>' 'RT{print $NF}' $i
done

and put the output in a file "titles.txt":

Sitetitle 1
Sitetitle 2
Sitetitle 3
...

So, now i have two files who have the same number of lines. How i could replace <title> with Sitetitle 1 ?

It this the right way with two files or its better to fetch the content of <title>-tag in the first script?

Thanks in advance
Stefano

[1] https://www.unix.com/shell-programmin...dent-text.html

Last edited by Tonda; 02-26-2009 at 08:24 AM.. Reason: Fix a typo in title
# 2  
Old 02-25-2009
It's not too clear what's your expected final output ..., anyway try to modify the following code:



Code:
$ cat file_1
./1999 | 1999| 1999 | author | mail/01 | 01 | 01 | author | mail/<title> | file1.html | file1.html | author | mail
./1999 | 1999| 1999 | author | mail/ 01 | 01 | 01 | author | mail/<title>|  file2.html | file2.html | author | mail
./2000  | 2000| 2000 | author | mail/<title> | file1.html | file1.html | author | mail
./2000 | 2000| 2000 | author | mail/<title> | file2.html | file2.html | author | mail
./<title> | file1.html | file1.html | author | mail
./<title> | file2.html | file2.html | author | mail


Code:
$ cat file_2
Sitetitle 1
Sitetitle 2
Sitetitle 3
Sitetitle 4
Sitetitle 5
Sitetitle 6


Code:
 awk ' { if( ( getline rec < "file_2" ) > 0 ) sub(/<title>/,rec) } 1' file_1


Output:

Code:
./1999 | 1999| 1999 | author | mail/01 | 01 | 01 | author | mail/Sitetitle 1 | file1.html | file1.html | author | mail
./1999 | 1999| 1999 | author | mail/ 01 | 01 | 01 | author | mail/Sitetitle 2|  file2.html | file2.html | author | mail
./2000  | 2000| 2000 | author | mail/Sitetitle 3 | file1.html | file1.html | author | mail
./2000 | 2000| 2000 | author | mail/Sitetitle 4  | file2.html | file2.html | author | mail
./Sitetitle 5 | file1.html | file1.html | author | mail
./Sitetitle 6 | file2.html | file2.html | author | mail

# 3  
Old 02-26-2009
MySQL [Solved]

Hi,
thats exactly what i want to get.

Thanks rubin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

2. Shell Programming and Scripting

Search and replace the string with new word using xml tags

Hi All i need to replace the url1 inside <remote> tag in below xml in first instance and in the second instance with url2. any help appreciated <locations> <hudson.scm.SubversionSCM_-ModuleLocation> <remote>https://svn2015.com/svn/repos/internalshard</remote> ... (4 Replies)
Discussion started by: madankumar.t@hp
4 Replies

3. Shell Programming and Scripting

Replace a word in a string starting with another word

Hi All, I have a file in which a number of lines are starting with similar first word but different next words. I want to replace the any nth word(not 1st or 2nd) with another word. Eg:- My file contains are like this:- Ram is a boy. Ram is a good boy. Ram plays cricket. Here I want to... (2 Replies)
Discussion started by: mukeshbaranwal
2 Replies

4. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

5. Shell Programming and Scripting

Replace a word after a particular word in a file

Hi, I want to replace a word in a file which occurs after a particular word. For example : $cat file.txt CASE WHEN AND c1 = 'I' AND c2= '2' THEN 1 WHEN AND c1= 'I' AND c2= '0' THEN 2 So in this example i want to replace... (4 Replies)
Discussion started by: ashwin3086
4 Replies

6. Shell Programming and Scripting

Replace first word after string INITIAL

Hi I have a file with hundreds of lines, some of the lines have word INITIAL followed by some numbers like ....INITIAL 1234535 .... ....INITIAL 5768644 .... I would like to replace the number after word INITLA with 4K how can I do it? Cant get my heard around this! The string is always... (3 Replies)
Discussion started by: halacil
3 Replies

7. Shell Programming and Scripting

Replace a word After a string.

Hello Gurus, I have a file which has foll contents scattered: ,TotUnasgndNetAmt FROM DEV_EIS_T.Wkly_SO_CCD_MOSumSnpsht WHERE CalDayRunDt = '2010-07-21' UNION ALL SELECT CalDayRunDt ,BusWkCd ,'N' I want to replace 2010-07-21 that starts after ' and ends before... (8 Replies)
Discussion started by: indrajit_u
8 Replies

8. UNIX for Dummies Questions & Answers

Seach for part of string and replace whole word

I am trying to find words in a text with a certain ending with sed and replace them with themselves but wrapped in tabs ex.: The fish swims in the water. -> searching for -ms ending The fish <tab>swims<tab>in the water. I've been trying all sorts of commands and get either an error... (5 Replies)
Discussion started by: stinnes
5 Replies

9. Shell Programming and Scripting

Replace a word from a string

How can i replace a particular word from string i.e. var="shiv_dutt_para_shar" wrd="para" rep_wrd="PARA" what i am trying to do that first i'll search if $var catains #wrd or not. if it contains then i've to replace $wrd with $rep_wrd. I have tried following #!/bin/sh t="shiv... (5 Replies)
Discussion started by: jadoo_c2
5 Replies

10. Shell Programming and Scripting

replace word in a file

there are 300 files in a directory , some of these files has a word "error" , I word to change this word to "message" , can advise what can i do ? thx. (5 Replies)
Discussion started by: ust
5 Replies
Login or Register to Ask a Question