Replace a string with each line from another file repeatedly


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace a string with each line from another file repeatedly
# 8  
Quote:
Originally Posted by perseous
sorry, i don't know well :
sed 's/XXXX/'$LINE'/' file1.txt >>file3.txt
set -x done file2.txt
cat file3.txt

and i get file3:
Code:
Monday
Tuesday
     Wednesday
Thursday

Do i miss sth?

set -x goes at the top (may be first line after the hashbang line). Not before done.
# 9  
Code:
cat file2 | xargs -L1 -I{} sed -e 's/XXXX/{}/g' file1
Monday home
Tuesday home
home Wednesday
Thursday home
Monday school
Tuesday school
school Wednesday
Thursday school
Monday cinema
Tuesday cinema
cinema Wednesday
Thursday cinema

# 10  
Quote:
Originally Posted by krishmaths
Do you use bash or ksh?
Do you have the first line of the script as #!/bin/bash based on the shell?
Can you paste the entire script for us to debug?
Code:
# sed 's/XXXX/'$LINE'/' file1.txt>>file3.txt
+ sed s/XXXX// file1.txt
# set -x done file2.txt          
+ set -x done file2.txt
# cat file3.txt                 
+ cat file3.txt
Monday
Tuesday
 Wednesday
Thursday

# 11  
Quote:
Originally Posted by perseous
Code:
# sed 's/XXXX/'$LINE'/' file1.txt>>file3.txt
+ sed s/XXXX// file1.txt
# set -x done file2.txt          
+ set -x done file2.txt
# cat file3.txt                 
+ cat file3.txt
Monday
Tuesday
 Wednesday
Thursday

set -x is at the wrong place. Could you please post the actual script?

Alternatively you may try the solution posted by rajamadhavan in post #9. It works.
# 12  
Here's what i get when tried rajamadhavan's post:
Code:
Monday home
Tuesday home
 Wednesday
Monday school
Tuesday school
 Wednesday
Monday cinemaol
Tuesday cinema
cinema Wednesday
Thursday cinema

I don't know much about these stuff as i use ssh connecting to my host. so please tell me clearly what am doing wrong.
# 13  
Cant make out whats going wrong. It works on my system. Please double check the contents of file1.

-Raja
This User Gave Thanks to rajamadhavan For This Post:
# 14  
content is fine. Think it's just not work for me. Could u explain why i get this on first solution? SOrry if i bother you because i really need this whereas i'm noob myself

Code:
#!/bin/bash

set -x
sed 's/XXXX/'$LINE'/' file1.txt >>file3.txt
+ sed s/XXXX// file1.txt
done file2.txt
-bash: syntax error near unexpected token `done'


Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #27
Difficulty: Medium
Linux was the first fully supported 64-bit operating system for consumers.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Search for a string,delete the line and replace with new string in a file

Hi Everyone, I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file. I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
10 Replies

2. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

HI Can any one guide me how to achieve this task. I have 2 files env.txt #Configuration.Properties values identity_server_url = http://identity.test-hit.com:9783/identity/service/user/register randon_password_length = 6 attachment_file_path = /pass/temp/attachments/... (1 Reply)
Discussion started by: nikilbr86
1 Replies

3. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

4. Shell Programming and Scripting

sed or awk to replace a value in a certain line from another file containing a string

Hi experts, In my text file I have the following alot of lines like below. input.k is as follows. 2684717 -194.7050476 64.2345581 150.6500092 0 0 2684718 -213.1575623 62.7032242 150.6500092 0 0 *INCLUDE $# filename... (3 Replies)
Discussion started by: hamnsan
3 Replies

5. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

Hi I am not the best scripter in the world and have run into a issue which you might be able to guide me on... I have two files. File1 : A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB File2: C345,... (5 Replies)
Discussion started by: luckycharm
5 Replies

6. 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

7. Shell Programming and Scripting

To trim Certain field in a line of a file and replace the new string in that position

To trim 3rd field in for all the lines of a file and replace the modified string in that particular field. For example i have a file called Temp.txt having content Temp.txt ----------------- 100,234,M1234 400,234,K1734 300,345,T3456 ---------------- So the modified file output should... (4 Replies)
Discussion started by: rpadhi
4 Replies

8. Shell Programming and Scripting

replace string in file.1 with line from file.2

Hello all, the title makes this sound simple, and maybe it should be. This is by code: #!/bin/sh cp ch25.txt ch25.fn.tex n=`grep -c '^\' ch25_footnotes.txt > temp` r=`awk -F] '{print $2}' temp` `sed 's/\/\\footnote{$r}/' ch25.fn.tex` done This is what I am trying to... (6 Replies)
Discussion started by: ccox85
6 Replies

9. Shell Programming and Scripting

Replace string in a file within a range of line

Hi, I want to replace the srting '; with ABCD'; in a file from line 1 to line 65. Is there any single command to do it without using awk Thanks for quick reply https://www.unix.com/images/misc/progress.gif (3 Replies)
Discussion started by: tosattam
3 Replies

10. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 Replies

Featured Tech Videos