LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-28-2008, 01:46 AM   #1
hamimi
LQ Newbie
 
Registered: Mar 2008
Location: Johannesburg
Posts: 5

Rep: Reputation: 0
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
 
Old 05-28-2008, 02:37 AM   #2
ophirg
Member
 
Registered: Jan 2008
Location: Israel
Distribution: Kubuntu 13.10
Posts: 134

Rep: Reputation: 34
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

Last edited by ophirg; 05-28-2008 at 03:06 AM.
 
Old 05-28-2008, 08:01 AM   #3
hamimi
LQ Newbie
 
Registered: Mar 2008
Location: Johannesburg
Posts: 5

Original Poster
Rep: Reputation: 0
works 100%

Thanks -It works
that is what i was looking for

Hamim
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
sed script to append variable text gmartin Linux - General 4 12-27-2006 04:44 PM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM
problem with sed n variable in shell script dipenchaudhary Programming 4 02-12-2006 10:54 PM
sed doesn't accept $variable in bash script chii-chan Programming 6 05-28-2005 07:07 AM
Expanding shell variable in sed goofyheadedpunk Programming 4 09-11-2004 05:17 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration