LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [LINUX] substituting variable upon cat execution (https://www.linuxquestions.org/questions/programming-9/%5Blinux%5D-substituting-variable-upon-cat-execution-798871/)

Winterburn 03-30-2010 07:48 AM

[LINUX] substituting variable upon cat execution
 
Hi.

I have a file named "test.txt". It contains the following:

Hello $USER1
Your boss is $ADMIN1

Now, I want to do something like this:

export USER1="World"
export ADMIN1="Me"
cat test.txt > test2.txt

And achieve something like this when I view the contents of test2.txt:

Hello World
Your boss is Me

The problem is, it's not that easy to achieve. I don't want to do the "echo Hello $USER1 >> test2.txt" because I'm planning to implement this on a MB-sized text file.

I can't google the right term that's why I explained it right away. ;)

PMP 03-30-2010 07:58 AM

How many variables need to be replaced, That is the no of variable not the occurence ?

crts 03-30-2010 08:28 AM

Hi,

this one worked with your sample code:

Code:

while read LINE
do
    eval echo $LINE >> test2
done < test

You can execute this in cli or add a shabang and make a script out of it.
Hope this helps.

grail 03-30-2010 08:37 AM

and this one two:

Code:

awk '/\$/{ print | "echo "$0}' test.txt > test2.txt

tuxdev 03-30-2010 02:04 PM

http://mywiki.wooledge.org/TemplateFiles

Winterburn 03-31-2010 05:30 AM

Quote:

Originally Posted by tuxdev (Post 3918246)

This worked for me; exactly what I want to achieve. Thanks!


All times are GMT -5. The time now is 12:14 AM.