LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

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

Rep: Reputation: 0
[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.
 
Old 03-30-2010, 07:58 AM   #2
PMP
Member
 
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
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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.
 
Old 03-30-2010, 08:37 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,561

Rep: Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939Reputation: 1939
and this one two:

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

Rep: Reputation: 115Reputation: 115
http://mywiki.wooledge.org/TemplateFiles
 
Old 03-31-2010, 05:30 AM   #6
Winterburn
LQ Newbie
 
Registered: Mar 2010
Posts: 2

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


Reply

Tags
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 02:08 AM.

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