Sponsored Content
Top Forums Shell Programming and Scripting Replace variable names in text file with its value Post 302382677 by aigles on Thursday 24th of December 2009 08:54:53 AM
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.
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
mtext_get_prop(3m17n)						 The m17n Library					     mtext_get_prop(3m17n)

NAME
mtext_get_prop - Get the value of the topmost text property. SYNOPSIS
void* mtext_get_prop (MText * mt, int pos, MSymbol key) DESCRIPTION
Get the value of the topmost text property. The mtext_get_prop() function searches the character at pos in M-text mt for the text property whose key is key. RETURN VALUE
If a text property is found, mtext_get_prop() returns the value of the property. If the property has multiple values, it returns the topmost one. If no such property is found, it returns NULL without changing the external variable merror_code. If an error is detected, mtext_get_prop() returns NULL and assigns an error code to the external variable merror_code. Note: If NULL is returned without an error, there are two possibilities: o the character at pos does not have a property whose key is key, or o the character does have such a property and its value is NULL. If you need to distinguish these two cases, use the mtext_get_prop_values() function instead. ERRORS
MERROR_RANGE, MERROR_SYMBOL SEE ALSO
mtext_get_prop_values(), mtext_put_prop(), mtext_put_prop_values(), mtext_push_prop(), mtext_pop_prop(), mtext_prop_range() COPYRIGHT
Copyright (C) 2001 Information-technology Promotion Agency (IPA) Copyright (C) 2001-2011 National Institute of Advanced Industrial Science and Technology (AIST) Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License <http://www.gnu.org/licenses/fdl.html>. Version 1.6.2 12 Jan 2011 mtext_get_prop(3m17n)
All times are GMT -4. The time now is 08:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy