LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   sed command (https://www.linuxquestions.org/questions/programming-9/sed-command-470390/)

ancys 08-03-2006 07:17 AM

sed command
 
hi all

I am writing a unix script in which file name is test.txt .
.txt shoud be replace as .flg
this can be done using sed coomand
but i want the o/p of sed to be stored in a variable

#!bin/sh
var=""
sed -e 's/.txt/.flg' test.txt
echo $var

echo $var should print the result as test.flg
how to do
help me

Agrouf 08-03-2006 07:31 AM

input="test.txt"
var=$(echo $input | sed "s/.txt/.flg/g")
echo $var

or what about :

var=$(echo $input | sed "s/.txt$/.flg/g")
(only replace .txt if at the end)

ancys 08-03-2006 07:51 AM

i tried the above code
but it show an error'var=$' unexpected
yes i want to replace .txt to .flg

Agrouf 08-03-2006 08:56 AM

which shell are you using? Is it bash?

burninGpi 08-03-2006 12:24 PM

input="test.txt"
var=`echo $input | sed "s/.txt/.flg/g"`
echo $var


Using $(...) is just the new way of writing `...`

ancys 08-03-2006 11:39 PM

I am using bash shell

I wrote the above coding
#!bin/sh
input="test.txt"
var='echo $input | sed "s/.txt/.flg/"'
echo $var

It executes without errors but it does not print the value in var
just shows a blank line


All times are GMT -5. The time now is 06:55 AM.