variable replacement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting variable replacement
# 1  
Old 06-01-2005
variable replacement

hi all ,

i want a command that interacts with the input of a user .
i.e.
i want it to serch a file for the first occurance of a variable and replace the value that is after the equal sign .
for example :

my file contains a varible that is $HOME=/home
i want to search for variable $HOME and replace the /home with some input .

thanks
cheers
# 2  
Old 06-01-2005
...
input="x"
while [ -z "$input" ] ; do
input=""
read input
if [ -n "$input" ] ; then
sed '/\$HOME=/ {; s/=.*$/='"$input"'/; q; }' file > file.new
fi
done
...

just enter an empty string to leave the loop.

bakunin
# 3  
Old 06-01-2005
things didnt work as i wanted
i have done the following

the script will prompt for the new path and then read it after that he will search the file and replace this variable ....
this is a kind of cotumization .

input="/home
while [ -z "$input" ] ; do
input=""
echo "please enter the location:\c"
read input
if [ -n "$input" ] ; then
sed '/\$HOME=/ {; s/=.*$/='"$input"'/; q; }' /file > /file.new
fi
done
NOTE : iam using ksh , Solaris 9 over sparc

Last edited by ppass; 06-01-2005 at 08:11 AM..
# 4  
Old 06-01-2005
sorry, I tripped over my own (sed-)foot. ;-))

The sed-script will terminate after the first occurrence of "home=..." and hence the lines following will be lost from the output file.

replace the then ... fi -part with the following:

iFirst=$( print - 'g/^[<blank><tab>]*\'$input'/n' |\
ed - file |\
sed '1 s/<tab>.*$//; 2,$d' \
)
sed $iFirst' s/=.*$/='"$input"'/' file > file.new

iFirst is a variable holding the first occurrence of your search string. You will have to replace <blank> and <tab> by real tabs and blanks.

bakunin
# 5  
Old 06-01-2005
sorry, not working


input="/home
while [ -z "$input" ] ; do
input=""
echo "please enter the location:\c"
read input
if [ -n "$input" ] ; then
iFirst=$( print - 'g/^[<blank><tab>]*\'$input'/n' |\
ed - file |\
sed '1 s/<tab>.*$//; 2,$d' \
)
sed $iFirst' s/=.*$/='"$input"'/' file > file.new
fi
done



i have treid to modify it ut with little luck ...

thanks for ur help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Is this MB, which needs replacement ?

Hello, I am getting below error in fmadm output. This server is not in support, so can't reach them. Is it showing that motherboard is faulty and should be replaced ? It was rebooted a week back and then, there were no errors # fmadm faulty --------------- ------------------------------------ ... (1 Reply)
Discussion started by: solaris_1977
1 Replies

2. 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

3. Shell Programming and Scripting

sed - replacement file path with variable - Escaping / character

Hi,, I have the line below in a file: $!VarSet |LFDSFN1| = '"E:\APC\Trials\20140705_427_Prototype Trial\Data\T4_20140705_Trial_Cycle_Data_13_T_Norm.txt" "VERSION=100 FILEEXT=\"*.txt\" FILEDESC=\"General Text\" "+""+"TITLE{SEARCH=NONE NAME=\"New Dataset\" LINE=1I want to write a script to change... (2 Replies)
Discussion started by: carlr
2 Replies

4. Shell Programming and Scripting

Sed:- Supported variable replacement after string match?

Hi All, I am trying to replace the variable in the file after the particular match string. It is being replaced if i hardcode the value and with use of "&" with sed. sed -e "s/URL./& http:\\localhost:7223/g" But when am trying to pass the variable it is failing. I tried multiple... (9 Replies)
Discussion started by: sharsour
9 Replies

5. Shell Programming and Scripting

sed replacement in file when line is in a variable

Hi, I have a file where I want to replace the 15th field separated by comma, only on specific lines matching lots of different conditions. I have managed to read the file line by line, within the loop my line is held in a variable called $line I assume this will be using sed (maybe... (5 Replies)
Discussion started by: jpt123
5 Replies

6. UNIX for Dummies Questions & Answers

replacement

my filename.txt looks like this: 2079951061790 SCK0000891539000000000000021600R 2079951061790 SCK0000901487000000000000028900R 2079951061790 SCK0000903092000000000000021300R 2079951074758 ... (9 Replies)
Discussion started by: tjmannonline
9 Replies

7. Shell Programming and Scripting

help me :replacement

Hi pls help me for below; i have a file .content is : =================== uid,pcsPricingPlan,refPcsQosProfName 821910002022,smartlimit,SGSNQOS1 i have to replace the value of uid and pricingplan by a unix script. may be the value would be next line or any where in the file. pls... (9 Replies)
Discussion started by: Aditya.Gurgaon
9 Replies

8. Shell Programming and Scripting

awk's gsub variable in replacement

I been trying to figure out how to use element of array as a replacement pattern. This works as I expected: $ echo "one two three" | awk '{ gsub(/wo/,"_BEG_&_END_",$2); print }' one t_BEG_wo_END_ three $ echo "one two three" | awk '{ tmp="foo"; gsub(/wo/,"_BEG_" tmp "_END_",$2);... (5 Replies)
Discussion started by: mirni
5 Replies

9. Shell Programming and Scripting

Awk variable replacement

I have a function awkvarrep() { awk -F'|' '$1~/$1/{printf "%-10s %-30s %-15s %-30s %-15s\n", $2,$3,$4,$5,$6}' testfile } I'm calling it by this VARREP=XYZ awkvarrep $VARREP since i'm passing $VARREP to the awkvarrep() function I want to use this with $1 but it dosen't seem to be... (5 Replies)
Discussion started by: iAm4Free
5 Replies

10. UNIX for Dummies Questions & Answers

Regarding Replacement

I have two files: file1: somedata <html> <head> This is sample statement ...... ...... </head> </html> somedata file2: olga 81 91 B A rene 82 92 B A zack 83 93 Expextd Result: (2 Replies)
Discussion started by: rajx
2 Replies
Login or Register to Ask a Question