How to get content of a variable into text file (sed)?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get content of a variable into text file (sed)?
# 1  
Old 03-06-2010
How to get content of a variable into text file (sed)?

Hello,

Im working on this problem for 3 days now and i just cant get it to work.. I tried with alot of different sed methods but didnt find any solution. Its proberly verry simple but i just started bash scripting for a month or so..

i have a file called: file.nfo and file.txt
the content of file.nfo is
Code:
test23423
test 234
test3443
whole lot of text

the content of file.txt is
Code:
[nfo]nfotxt[/nfo]

I want to replace nfotxt with the content of the file.nfo
i do that with:
Code:
txtfile=file.txt
nfo=file.nfo
nfotext="$(cat "$nfo")"

then i use sed to search and replace like:
Code:
sed -e "s/nfotxt/$nfotext/g" $txtfile

i also did an escape \ or qoute "$nfotext"

The problem i have is that it doesnt replace the nfotxt with the content of the file.nfo and that it wil replace it with $nfotext rather then the content.. Hope someone can enlighten me or point me in the right direction..
# 2  
Old 03-06-2010
what is the o/p of:
Code:
echo $nfotext

and how you want the o/p:
Code:
[nfo]test23423 test 234 test3443 [/nfo]

or
Code:
[nfo]test23423
test 234
test3443[/nfo]

# 3  
Old 03-06-2010
Quote:
Originally Posted by anchal_khare
what is the o/p of:
Code:
echo $nfotext

and how you want the o/p:
Code:
[nfo]test23423 test 234 test3443 [/nfo]

or
Code:
[nfo]test23423
test 234
test3443[/nfo]

Code:
what is the o/p?

the content of echo $nfotxt file is what i cat.

Code:
and how you want the o/p?

untouched..how it is in the nfo file.. Like this:
edited by scottn: LINK REMOVED

Last edited by Scott; 03-06-2010 at 05:24 PM..
# 4  
Old 03-06-2010
Not sure about the sed thing, but if you are okay with alternative solutions, then -

Code:
$ 
$ cat file.nfo
test23423
test 234
test3443
whole lot of text
$ 
$ cat file.txt
[nfo]nfotxt[/nfo]
$ 
$ perl -pi.bak -e "s/nfotxt/`cat file.nfo`/" file.txt
$ 
$ cat file.txt
[nfo]test23423
test 234
test3443
whole lot of text[/nfo]
$ 
$

tyler_durden
# 5  
Old 03-06-2010
Code:
{
 printf "%s" "[nfo]"
 cat "file.nfo"
 printf "%s\n" "[/nfo]"
} > file.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacement of variable by their content in a file

Dear all, I have a "SQL request" in a file: that request include different "host variable" and I would like to substitute the different "host variable" by their respective content before executing the request. For example: $ echo $SHELL /bin/bash $ cat dae2.txt DELETE FROM ... (11 Replies)
Discussion started by: dae
11 Replies

2. UNIX for Dummies Questions & Answers

Getting ls content into a file using variable

hi i just cant figure out how can i do this ls -lt > log.txt using $PWD what i mean is how can i get the ls command content into a file using $PWD variable? :confused: (4 Replies)
Discussion started by: chinababy
4 Replies

3. Shell Programming and Scripting

Variable resolution in File content

I have a file File1 containing lines like below apple ${FRUIT}-Color orange ${FRUIT}-Color banana ${FRUIT}-Color Now, in my shell I'm reading the file like below while read FRUIT DESC; do echo $FRUIT $DESC; done < File1 which outputs - apple ${FRUIT}-Color orange ${FRUIT}-Color... (3 Replies)
Discussion started by: nexional
3 Replies

4. Shell Programming and Scripting

Sed: replace content from file with the content from file

Hi, I am having trouble while using 'sed' with reading files. Please help. I have 3 files. File A, file B and file C. I want to find content of file B in file A and replace it by content in file C. Thanks a lot!! Here is a sample of my question. e.g. (file A: a.txt; file B: b.txt; file... (3 Replies)
Discussion started by: dirkaulo
3 Replies

5. Shell Programming and Scripting

Script to create a text file whose content is the text of another files

Hello everyone, I work under Ubuntu 11.10 (c-shell) I need a script to create a new text file whose content is the text of another text files that are in the directory $DIRMAIL at this moment. I will show you an example: - On the one hand, there is a directory $DIRMAIL where there are... (1 Reply)
Discussion started by: tenteyu
1 Replies

6. Shell Programming and Scripting

How to put content of file into a variable?

For example, I have a simple text file note: this a note a simple note a very very simple notewhen I use this command, temp=$(cat "note.txt")then I echo temp, the result is in one line. echo $temp note: this a note a simple note a very very simple noteMy variable doesn't have newline. How... (7 Replies)
Discussion started by: 14th
7 Replies

7. Shell Programming and Scripting

how to get the specified content of the text into a variable.

I am having one string like ./usr1/Server/temp/app.env ./usr1/Server/temp/upp/app.env ./usr1/Server/ORIG_temp/app.env ./usr1/Server/ORIG_temp/upp/app.env ./usr1/Server/work_temp_40/app.env ./usr1/Server/work_temp_40/upp/app.env ./usr1/fd/app.env ./usr1/PurgeArchive/app.env ./usr1/bm/bin/app.env... (6 Replies)
Discussion started by: dineshmurs
6 Replies

8. Shell Programming and Scripting

Variable of Content From Part of Other File

I may not being doing this description justice, but I'll give it a try. I created a mailx script; there will be several messages using the same script where the only difference is the content. So I figured I'd make the content of the message a variable retrieved from a separate file. I have five... (5 Replies)
Discussion started by: royarellano
5 Replies

9. Shell Programming and Scripting

Inserting text to file, sed and variable not acting right

I want to put text stored in a variable into a file on the 7th line. I'm having trouble with this line: sed '7i\'$text'' $file It works perfectly for a single word, but fails if there are two words because of the space. I've tried several forms of quoting and this is the only one that... (2 Replies)
Discussion started by: fubaya
2 Replies

10. Shell Programming and Scripting

redirecting variable content to a file!

Hello! I'm having problems trying to extract the contents of a variable and placing it into a text file. Grateful for any help. Been trying something along the lines of: $variable > file.txt or `cat < $variable` > file.txt As you can see I'm a newbie to this :D (2 Replies)
Discussion started by: lloowen
2 Replies
Login or Register to Ask a Question