The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Need Replacement for sed dbsurf Shell Programming and Scripting 0 01-25-2008 06:47 PM
Awk variable replacement iAm4Free Shell Programming and Scripting 5 08-30-2007 12:14 PM
Regarding Replacement rajx UNIX for Dummies Questions & Answers 2 05-22-2007 10:19 AM
Replacement using sed handak9 UNIX for Dummies Questions & Answers 5 07-13-2004 12:28 PM
cpu replacement. help IMPTRUE UNIX for Dummies Questions & Answers 3 07-25-2003 09:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-01-2005
ppass's Avatar
ppass ppass is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 93
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 (permalink)  
Old 06-01-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,635
...
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 (permalink)  
Old 06-01-2005
ppass's Avatar
ppass ppass is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 93
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 (permalink)  
Old 06-01-2005
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
  
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,635
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 (permalink)  
Old 06-01-2005
ppass's Avatar
ppass ppass is offline
Registered User
  
 

Join Date: Jan 2005
Posts: 93
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:27 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0