Sed, replace variables in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed, replace variables in file
# 1  
Old 05-01-2013
Sed, replace variables in file

Hi Everyone,

I need some help with sed and I'm totally new to it.

I have a template file with variables in it. These variables start with a '$' sign and are exactly one character long (plus the '$' sign). For example: $a, $b, etc.
Each variable should be replaced with the contents of a specific file. As a last step double dollar signs should be replaced with single dollar signs.

Example:

Template file:
Code:
aaaa$abbbb$bcc$$cc

FileA:
Code:
1234

FileB:
Code:
456
789

The result should be:
Code:
aaaa1234bbbb456
789cc$cc

Any help would be appreciated.

Csarli

Last edited by Scrutinizer; 05-01-2013 at 07:34 AM.. Reason: code tags
# 2  
Old 05-01-2013
If you are totally new to sed, I'm not sure this is the best problem to start on. But it doesn't matter. Smilie Here is one way to do it:
Code:
$ cat test.sh
# : is chosen as a special character not otherwise in any of the files
fileA=`tr "\n" ":" < fileA | sed "s/:$//"`
fileB=`tr "\n" ":" < fileB | sed "s/:$//"`
sed -e "s/\$a/$fileA/g" -e "s/\$b/$fileB/g" -e 's/$\$/$/g' template | tr ":" "\n"

Code:
$ ./test.sh
aaaa1234bbbb456
789cc$cc

# 3  
Old 05-01-2013
Please use code tags as required by forum rules!

Assuming you are using bash; might not work on other, older shells. Although eval is deprecated now and then, it might help in this case, used with caution:
Code:
$ read -d"" a <file1
$ read -d"" b <file2
$ d=$(sed 's:\$\(.\):${\1}:g;s:{$}::' file)
$ eval echo $d
aaaa1234bbbb456
789cc

# 4  
Old 05-01-2013
Thank you very much for your help.

My problem is that all of my files are in HTML. So they are full of special characters. There must be a simpler solution than escaping all of them one by one. At least, I hope so Smilie

And I have to do this on openwrt, so I only have busybox.
Anyway, I don't insist on sed if there is a better solution.
# 5  
Old 05-01-2013
Second output line supposed to be 789cc$cc
This User Gave Thanks to hanson44 For This Post:
# 6  
Old 05-01-2013
Thanks. Replace s:{$}::by s:${$}:\$:.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 05-01-2013
Quote:
My problem is that all of my files are in HTML.
So they are full of special characters.
OK. There is the backtick ` which I would guess is not in your HTML files, as an alternative to : character.

Or, here is an alternative that uses \f formfeed for the special character, which should 100% not be in your HTML files. If it is in there for some reason, it can easily be removed with tr -d "\f" before running the rest of the script.
Code:
$ cat test.sh
# \f formfeed is chosen as special character not otherwise in any of the files
fileA=`tr "\n" "\f" < fileA | sed "s/\f$//"`
fileB=`tr "\n" "\f" < fileB | sed "s/\f$//"`
sed -e "s/\$a/$fileA/g" -e "s/\$b/$fileB/g" -e 's/$\$/$/g' template | tr "\f" "\n"

Code:
$ ./test.sh
aaaa1234bbbb456
789cc$cc

Quote:
There must be a simpler solution than escaping all of them one by one.
Maybe someone else can come up with a simpler solution. It will be interesting to see. In the meantime, what if you try this solution and the RudiC solution on your real files.
This User Gave Thanks to hanson44 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

2. Shell Programming and Scripting

environment variables in a sed script file

Hello Everyone I need to create a script file which must append some lines to a target text file, I'm using sed for windows, the script file look like this: { a\ STRINGTABLE DISCARDABLE\ BEGIN\ 5, 150 {a\ #define RC_SHELL, "%ID_SHELL%"\ #define RC_NAME, "%ID_NAME%"\ END } ... (1 Reply)
Discussion started by: edgarvm
1 Replies

3. UNIX for Dummies Questions & Answers

How to use sed to copy specific lines from a file using shell variables?

hello! I am trying to use sed to copy specific set of lines from a file for which the starting and ending line numbers of the lines to be copied are stored in shell variables. How can i copy those lines? if the input_file is something like this and if the following is the script a=2 b=4... (4 Replies)
Discussion started by: a_ba
4 Replies

4. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

5. Shell Programming and Scripting

replace two character strings by two variables with sed command

Hello, I want to writte a script that replace two character strings by two variables with the command sed butmy solution doesn't work. I'm written this: sed "s/TTFactivevent/$TTFav/g && s/switchSLL/$SLL/g" templatefile. I want to replace TTFactivevent by the variable $TTFav, that is a... (4 Replies)
Discussion started by: POPO10
4 Replies

6. Shell Programming and Scripting

How to use sed to replace the a string in the same file using sed?

How do i replace a string using sed into the same file without creating a intermediate file? (7 Replies)
Discussion started by: gomes1333
7 Replies

7. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

8. Shell Programming and Scripting

Replace environment variables with sed

Hi, I need to write a sed that replaces the value of all environment variables that have PASS in the name with ***** . I have a log file who prints the value of all environment variables ,including those who hold passwords. In the name of these variables I found always the PASS string, ex:... (16 Replies)
Discussion started by: liorfe
16 Replies

9. UNIX for Dummies Questions & Answers

Problems with sed and flat file variables

Hello All, It has been a loooooooooooong time since I had last used sed but decided to use it for a simple task I have . My goal is to use sed to read variables from a flat file then use those same variables in order to make some subsitutions. However what I am finding is that when the... (1 Reply)
Discussion started by: icalderus
1 Replies

10. UNIX for Dummies Questions & Answers

variables use upper case? sed : output to the same file?

Hi, Q1: are the variables in shell script usually UPPER CASE? Q2: can sed output to the same file that it's using it? eg. cat sameFile | sed 's/here/there/g' > sameFile ? I expect the sed replace all "here" to "there" and change it in sameFile. i tried that one, the sameFile... (1 Reply)
Discussion started by: gusla
1 Replies
Login or Register to Ask a Question