DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Shell Scripting (http://www.daniweb.com/forums/forum113.html)
-   -   Using varibles within awk/sed commands (http://www.daniweb.com/forums/thread165836.html)

mustaine85 Jan 3rd, 2009 12:28 pm
Using varibles within awk/sed commands
 
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 :)

Salem Jan 3rd, 2009 12:32 pm
Re: Using varibles within awk/sed commands
 
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

mustaine85 Jan 3rd, 2009 12:50 pm
Re: Using varibles within awk/sed commands
 
sed "${i}s/$/${age}/g" birthday.csv
Worked great thanks very much :)


All times are GMT -4. The time now is 8:08 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC