Go Job Hunting at the LQ Job Marketplace
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-30-2010, 07:48 AM   #1
LQ Newbie
Registered: Mar 2010
Posts: 2

Rep: Reputation: 0
[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.
Old 03-30-2010, 07:58 AM   #2
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
How many variables need to be replaced, That is the no of variable not the occurence ?
Old 03-30-2010, 08:28 AM   #3
Senior Member
Registered: Jan 2010
Posts: 1,605

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447

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.
Old 03-30-2010, 08:37 AM   #4
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 8,125

Rep: Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192Reputation: 2192
and this one two:

awk '/\$/{ print | "echo "$0}' test.txt > test2.txt
Old 03-30-2010, 02:04 PM   #5
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
Old 03-31-2010, 05:30 AM   #6
LQ Newbie
Registered: Mar 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Originally Posted by tuxdev View Post
This worked for me; exactly what I want to achieve. Thanks!


cat, variable

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Piping cat output to variable? zizou86 Programming 12 01-13-2010 03:17 PM
cat file with $variable linuxbin Linux - Newbie 6 08-27-2009 03:27 AM
cat in asm/ cat --show-all option Tux Linux - Software 1 09-02-2006 09:31 PM
how to cat a text file and save it as a variable mrobertson Programming 37 07-05-2005 08:20 AM
need help substituting with sed parker Programming 2 08-08-2003 10:03 AM

All times are GMT -5. The time now is 10:30 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration