-   Programming (
-   -   [LINUX] substituting variable upon cat execution (

Winterburn 03-30-2010 07:48 AM

[LINUX] substituting variable upon cat execution

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


this one worked with your sample code:


while read LINE
    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:


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

tuxdev 03-30-2010 02:04 PM

Winterburn 03-31-2010 05:30 AM


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 08:42 PM.