Replace variable names in text file with its value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace variable names in text file with its value
# 1  
Old 12-24-2009
Question Replace variable names in text file with its value

Hi

I have a text file (mytext.txt), the content of which is as following:
Code:
My name is <@MY_NAME@> and my age is <@MY_AGE@> .

Now i have another property file (myprops) which is as following:
Code:
MY_NAME=abcdefgh
MY_AGE=000000

I was wondering, how can i replace the tags of mytext.txt, with its corresponding values in the property file. I tried using sed:
. ./myprops
sed -e "s/<@\(.*\)@>/$(\1)/g" mytext.txt

IN VAIN!!! Smilie

Anyone got ideas??

Thanks.

Last edited by Yogesh Sawant; 12-24-2009 at 10:21 AM.. Reason: added code tags
# 2  
Old 12-24-2009
A possible solution :
Code:
awk -F= '
NR==FNR {
   Value[$1] = $2;
   next;
}
{
   text=$0;
   out = ""; 
   while ( pos=index(text, "<@") ) {
      out  = out substr(text, 1, pos-1);
      text = substr(text, pos+2) 
      pos  = index(text, "@>");
      prop = substr(text, 1, pos-1);
      out  = out Value[prop] 
      text = substr(text, pos+2);
   }
   out = out text;
   print out;
}
' myprops.txt mytext.txt

Or
Code:
awk -F= '
NR==FNR {
   Value[$1] = $2;
   next;
}
FNR==1 {
   Value[""] = "";
   FS = SUBSEP;
}
{
   gsub(/<@|@>/, SUBSEP);
   out = "";
   for (f=1; f<=NF; f+=2) {
      out = out $f ( $(f+1) in Value ? Value[$(f+1)] : "<@" $(f+1) "@>"i );
   }
   print out;
}
' myprops.txt mytext.txt

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find matching file in bash with variable file names but consisent prefixs

As part of a bash the below line strips off a numerical prefix from directory 1 to search for in directory 2. for file in /home/cmccabe/Desktop/comparison/missing/*.txt do file1=${file##*/} # Strip off directory getprefix=${file1%%_*.txt} ... (5 Replies)
Discussion started by: cmccabe
5 Replies

2. UNIX for Dummies Questions & Answers

Replace variable string with text

Hi All, Hoping someone can help.... I am trying to work out how I can ammend a log file to remove variable strings in order to remove confidential information which I cant pass on. As an example I have used phone numbers. A large log file contains multiple lines containing something like the... (6 Replies)
Discussion started by: mutley2202
6 Replies

3. Shell Programming and Scripting

Replace names in a file

Hi, I have a file like this: >Contig1 TTTTCATCAGCAATAGGTGCCTTCAACAAGTTTGAGAATTTTTTACACATGACTACAGGG CACGTGCACAATGTTTTGAGCACTGCCAATGTCGTCTTTTCTTCCCTGCTGATTCTATTT AGCTAGATTCACTTTGTTTCCAAAAAACGACAACAAATTCCAAGGTGTACCAAGGTGTAT >Contig2 CCCGTAGTAAAGATCAAAACTATCCCCTGCTGTATTCTCAACCAAATCCAACAAACTCTC... (3 Replies)
Discussion started by: the_simpsons
3 Replies

4. Shell Programming and Scripting

replace text in file using variable

Hi, I'm making a script that automaticaly set file size and path in xml file. I tried with : sed -i 's/BOOTPATH/TEST/g' file.xml it works fine but if I use a viriable : sed -i 's/BOOTPATH/$bootpathf/g' file.xml with this one, no change are made. I don't understand why. If a make a ... (13 Replies)
Discussion started by: Toug
13 Replies

5. Shell Programming and Scripting

File Names in a Variable in a loop

Hi All , I am having confusion in a shell script. Please guide me. I need to get multiple files (number of files vary time to time, file names are separated by '|') using FTP get from the remote server. Actually, i call the FTP function in a loop. At the last step, i need to move all the get... (3 Replies)
Discussion started by: spkandy
3 Replies

6. UNIX for Dummies Questions & Answers

Find and replace portion of file names

Hey all, So I know you can easily find and replace words and strings in text files, but is there an easy way to find and replace just a sub-portion of text in the file name. For example, in a directory I have tons of file names that start with F00001-0708, and I want to change all the files to... (2 Replies)
Discussion started by: hertingm
2 Replies

7. UNIX for Dummies Questions & Answers

sed to replace text with a variable

I'm trying to replace text in a file with text from a variable I have the following in my script, but its not working: #!/bin/ksh echo "Enter the path to load scripts" read x echo "updating the templates" sed "s/CHANGE_ME_TO_LOAD_PATH/"$x"/g" LoadFiles.sh > LoadFiles2.sh I thought... (1 Reply)
Discussion started by: orahi001
1 Replies

8. Shell Programming and Scripting

Replace characters in all file names in a particular directory

Hi, I have searched the forum on how to mass replace the file names. We are doing the migration and I am trying to accomplish a task where I have to replace all UNIX scripts in a particular directory that start with bdw to fdm... For example: bdw0110137.sh should be fdm0110137.sh Keep the... (4 Replies)
Discussion started by: madhunk
4 Replies

9. Shell Programming and Scripting

find and replace text with a variable?

Is there a way that I can make the following work with using variables? perl -pi -e 's#blah#hrm#ig' replacetext but like this var=blah perl -pi -e 's#$var#hrm#ig' replacetext (3 Replies)
Discussion started by: doublejz
3 Replies

10. UNIX for Dummies Questions & Answers

Variable assignment for file names.

I am trying to process error files in selected directories. I can count the files that are there and export the contents to a file for either emailing or printing. The next step is to move the files to a processed directory with the name changed to .fixed as the last extension. for file in... (2 Replies)
Discussion started by: jagannatha
2 Replies
Login or Register to Ask a Question