LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   replaced variable field within shell script using sed (https://www.linuxquestions.org/questions/linux-newbie-8/replaced-variable-field-within-shell-script-using-sed-645234/)

hamimi 05-28-2008 01:46 AM

replaced variable field within shell script using sed
 
Hi
I have a file called myfile:
contents as follows:
/usr/snapshot-efb/snapshot -i 10.3.15.25 -a '/mnt/i/a/a' -2 483326bf -3 '/apps/m
edsys/' -x 7 restore
/usr/snapshot-efb/snapshot -i 10.3.15.25 -a '/mnt/i/a/a' -2 483326bf -3 '/apps/m
edtst/' -x 7 restore
/usr/snapshot-efb/snapshot -i 10.3.15.25 -a '/mnt/i/a/a' -2 483326bf -3 '/u1/med
sys/wrk/' -x 7 restore
/usr/snapshot-efb/snapshot -i 10.3.15.25 -a '/mnt/i/a/a' -2 483326bf -3 '/u1/prn
tsys/' -x 7 restore
/usr/snapshot-efb/snapshot -i 10.3.15.25 -a '/mnt/i/a/a' -2 483326bf -3 '/usr/pr
ntsys/' -x 7 restore
/usr/snapshot-efb/snapshot -i 10.3.15.25 -a '/mnt/i/a/a' -2 483326bf -3 '/usr/sy
s_data/' -x 7 restore
/usr/snapshot-efb/snapshot -i 10.3.15.25 -a '/mnt/i/a/a' -2 483326bf -3 '/usr/ut
il/' -x 7 restore
/usr/snapshot-efb/snapshot -i 10.3.15.25 -a '/mnt/i/a/a' -2 483326bf -3 '/backup
/medsys/' -x 7 restore

my problem is trying to amend the 7th field which is "483326bf" using this script:
# Add the Tagnumber to change
TAGNUM=`awk '/snap/ {print $7}' myfile`
NEWTAGNUM=`awk '/snap/ {print $7}' myfile.out`
echo "Enter the The Tag Number for Restore, or Q to quit if not required."
echo "-->\c "
read TAG
case $TAG in
q|Q) break ;;
*) echo "Tag Number -->\c "
echo $TAG >newfile
echo $TAG >>newfile
echo $TAG >>newfile
echo $TAG >>newfile
echo $TAG >>newfile
echo $TAG >>newfile
for FILE in `ls myfile`
do
sed -e 's/"$TAGNUM"/"$newfile"/g' myfile >myfile.out
# sed -e 's!$TAGNUM!$newfile!g' myfile >myfile1
done
esac

I somehow cannot get the variable changed withing my script -If I manually replace $TAGNUM to 483326bf it works.
Any help will be much appreciated

thanks
Hamim

ophirg 05-28-2008 02:37 AM

I'm not sure what exactly are you trying to do
but try this:

Code:

#!/bin/bash
echo enter old tag:
read OLDTAG
echo enter new tag:
read NEWTAG
sed "s/$OLDTAG/$NEWTAG/" oldfile > newfile

update:
also try
Code:

#!/bin/bash
echo enter new tag:
read NEWTAG
awk "{\$7=\"$NEWTAG\"; print \$0}" oldfile > newfile


hamimi 05-28-2008 08:01 AM

works 100%
 
Thanks -It works
that is what i was looking for

Hamim


All times are GMT -5. The time now is 04:29 PM.