how to replace a text in a file from second file in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to replace a text in a file from second file in shell
# 1  
Old 04-27-2012
how to replace a text in a file from second file in shell

Hi,
I have file 1 say
Code:
abc abcd
abc abcd <some tag> xyx
abcd xyz
abc abcd <some tag> xyx
xyz abc

And i have another file say file 2 -
Code:
replaced tag1
replaced tag2

Now i want to put value of file2 in file1 whereever it finds <some tag>.

for e.g. first <some tag> should be replaced with "replaced tag1" in file1 , second <some tag> in file1 should be replaced with "replaced tag2" and so on.

I wrote below piece of code which is not working -

Code:
exec 3<file2
   exec 4<file1

   while IFS= read -r line1 <&3
   IFS= read -r line2 <&4
   do

        value_to_be_replaced=`echo "$line1"`
        test=`echo "$line2"`
   if [ "$test" == "abc abcd <some tag> xyx" ]
   then
        sed 's/$test/abc abcd "$value_to_be_replaced" xyx/' $test
  fi
  done


Last edited by Scrutinizer; 04-27-2012 at 05:16 AM.. Reason: code tags
# 2  
Old 04-27-2012
Try:
Code:
awk '/<some tag>/{getline t < "file2";sub("<some tag>",t,$0)}1' file1

# 3  
Old 04-27-2012
hey thanks. It worked
# 4  
Old 04-27-2012
Hi bartus11,

Can you please explain complete line as i understood partially?

Thanks
Krsnadasa
# 5  
Old 04-27-2012
Quote:
Originally Posted by krsnadasa
Hi bartus11,

Can you please explain complete line as i understood partially?

Thanks
Krsnadasa
<some tag> --> if the record (lines of the file1) matches the our pattern ("<some tag>")
{getline t < "file2" } --> then read as "t" variable the line from file2 [ t = "replaced tag1" ] AND
sub("<some tag>",t,$0) --> change (<some tag>) with "t" in the $0 ("abc abcd <some tag> xyx")
}1 --> print the new all filelds

regards
ygemici
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to replace text file from a lookup file

Hi. I need assistance with the replacing of text into a specific file via a bash script. My bash script, once run, currently provides a menu of computer names to choose.The script copies onto my system various files, depending what computer was selected in the menu.This is working OK. Now, I... (1 Reply)
Discussion started by: jonesn2000
1 Replies

2. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

3. Shell Programming and Scripting

Replace text from one file in another file

I have 2 files. Here is a sample from file 1. NETIK0102_UCS_Boot 20000025b510105d NETIK0102_UCS_Boot 20000025b510104d NETIK0102_UCS_HBA0 20000025b510113e NETIK0102_UCS_HBA1 20000025b510112e NETIK0102_UCS_HBA2 20000025b51010de NETIK0102_UCS_HBA3 20000025b51010fe SEIADWFMPRD1... (4 Replies)
Discussion started by: kieranfoley
4 Replies

4. Shell Programming and Scripting

Search and replace from file in awk using a 16 bit text file

Hello, Some time ago a helpful awk file was provided on the forum which I give below: NR==FNR{A=$0;next}{for(j in A){split(A,P,"=");for(i=1;i<=NF;i++){if($i==P){$i=P}}}}1 While it works beautifully on English and Latin characters i.e. within the ASCII range of 127, the moment a character beyond... (6 Replies)
Discussion started by: gimley
6 Replies

5. Shell Programming and Scripting

Replace text in column1 of a file matching columns of another file

Hi all, I have 2 files: species-names.txt Abaca-bunchy-top-virus ((((Abaca-bunchy-top-virus((Babuvirus((Unassigned((Nanoviridae((Unassigned)))) Abutilon-mosaic-virus ((((Abutilon-mosaic-virus((Begomovirus((Unassigned((Geminiviridae((Unassigned))))... (2 Replies)
Discussion started by: thienxho
2 Replies

6. Shell Programming and Scripting

Using shell to replace characters in a text file

Can I just say, this is such a frustrating and yet enormously rewarding field of study. I'm in the middle of configuring GeekTool (Uh oh, stupid n00b) and I really only have one question. I'm using Automator to grab a RSS feed, having GeekTool continually run that application every 10 minutes,... (7 Replies)
Discussion started by: SomeTechGuy
7 Replies

7. Shell Programming and Scripting

shell or perl script needed for ldif file to text file conversion

This is the ldf file dn: sdcsmsisdn=1000000049,sdcsDatabase=subscriberCache,dc=example,dc=com objectClass: sdcsSubscriber objectClass: top postalCode: 29600 sdcsServiceLevel: 10 sdcsCustomerType: 14 givenName: Adelia sdcsBlackListAll: FALSE sdcsOwnerType: T-Mobile sn: Actionteam... (1 Reply)
Discussion started by: LinuxFriend
1 Replies

8. Shell Programming and Scripting

How to replace text in a file with text entered

I am trying to write a shell script that will allow the typing of a value, then using that value to replace data in a text file. I suspect I need sed. The format of the file is: Variable1:Value1 Variable2:Value2 The interaction would be something like: Shell Prompt: "Please enter the... (9 Replies)
Discussion started by: cleanden
9 Replies

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

10. UNIX for Dummies Questions & Answers

search and replace a specific text in text file?

I have a text file with following content (3 lines) filename : output.txt first line:12/12/2008 second line:12/12/2008 third line:Y I would like to know how we can replace 'Y' with 'N' in the 3rd line keeping 1st and 2nd lines same as what it was before. I tried using cat output.txt... (4 Replies)
Discussion started by: santosham
4 Replies
Login or Register to Ask a Question