Using varibles within awk/sed commands

Reply

Join Date: Dec 2008
Posts: 5
Reputation: mustaine85 is an unknown quantity at this point 
Solved Threads: 0
mustaine85 mustaine85 is offline Offline
Newbie Poster

Using varibles within awk/sed commands

 
0
  #1
Jan 3rd, 2009
Can I use my own variables within awk and sed for example:
I've written a while loop with a counter $i and I want to use the value of $i within sed and awk to edit certain lines of text within a data file.

I want to use :
sed '1s/$/texthere/g' birthday.csv
Like this:
sed '$is/$/$age/g' birthday.csv
but it only displays the name of the variable not the value it contains

$age is the result of a calculation created within the while loop that I would like to append to the end of each line.

Any help is greatly appreciated
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Using varibles within awk/sed commands

 
0
  #2
Jan 3rd, 2009
You have to read up on the (sometimes strange) rules about quoting and variable substitution in various shells.

Try say
sed $i's/$/'$age'/g' birthday.csv
Or maybe
sed "${i}s/$/${age}/g" birthday.csv
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 5
Reputation: mustaine85 is an unknown quantity at this point 
Solved Threads: 0
mustaine85 mustaine85 is offline Offline
Newbie Poster

Re: Using varibles within awk/sed commands

 
0
  #3
Jan 3rd, 2009
Shell Scripting Syntax (Toggle Plain Text)
  1. sed "${i}s/$/${age}/g" birthday.csv
Worked great thanks very much
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC