LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 03-23-2007, 05:06 PM   #1
cashton2k
Member
 
Registered: May 2004
Posts: 46

Rep: Reputation: 15
sed and bash variables


hi im trying to make a script which gets a value from a text file, increments it then amend the text file. the sed command im using doesnt appear to be working with the bash variables and doesnt amend the file, am i doing it right?

Code:
oldcount=$(awk '{print $1}' count)
let newcount=oldcount+1

sed s/"$oldcount"/"$newcount"/ count
echo $oldcount
echo $newcount
thanks for any help
 
Old 03-23-2007, 09:37 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
You're doing almost everything right. Change your sed command so it looks like this:

Code:
sed -i s/"$oldcount"/"$newcount"/ count
If you have an older version of sed which does not have the -i option (mine doesn't), you'll get an error message indicating this. In that case, do this instead:

Code:
sed s/"$oldcount"/"$newcount"/ count > count.tmp
mv count.tmp count
Hope this helps.
 
Old 03-24-2007, 06:30 AM   #3
cashton2k
Member
 
Registered: May 2004
Posts: 46

Original Poster
Rep: Reputation: 15
cheers got it going with the -i

is there a way to awk a specific line in a text file?

thanks again
 
Old 03-24-2007, 08:00 AM   #4
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
Code:
oldcount=$(awk '/pattern/{print $1}' count)
Which is the same as
Code:
oldcount=$(sed '/pattern/!d' count)

Last edited by nx5000; 03-24-2007 at 08:02 AM.
 
Old 03-24-2007, 09:39 AM   #5
cashton2k
Member
 
Registered: May 2004
Posts: 46

Original Poster
Rep: Reputation: 15
what about if i didnt know what was on the line but i knew the line number?
 
Old 03-24-2007, 09:58 AM   #6
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
This will output line 2 of file

Code:
awk '{ if (NR==2) print; }' file

sed -n 2p file
 
Old 03-28-2007, 12:09 PM   #7
cashton2k
Member
 
Registered: May 2004
Posts: 46

Original Poster
Rep: Reputation: 15
thanks for your help guys

i put this into my script

awk '{ if (NR=="$newcount") print; }' file1 > file2

but all it does is clear file2

entering the command on its own works fine e.g.

awk '{ if (NR==2) print; }' file1 > file2

am i using the bash variable wrong again?

echoin newcount gives the correct output

thanks for any help again
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 and bash variables cashton2k Programming 1 03-23-2007 05:43 PM
SED and shell variables 3saul Linux - Software 4 12-29-2005 12:37 AM
sed with bash variables MurrayL Linux - Software 2 03-21-2005 09:13 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
using bash variables in sed jimieee Programming 6 02-04-2004 05:11 AM


All times are GMT -5. The time now is 10:23 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration