Using variables within awk/sed commands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using variables within awk/sed commands
# 1  
Old 01-03-2009
Using variables 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' data.csv
Like this:
sed '$is/$/$age/g' data.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 Smilie
# 2  
Old 01-03-2009
Hi,

shell variables inside '...' are not expanded. You'll have to
use "...". Further use ${i}s instead of $is.

HTH Chris
# 3  
Old 01-03-2009
Code:
sed "${i}s/$/${age}/g" data.csv

Works great thank you very much Smilie
# 4  
Old 01-08-2009
Hi ,

i used with success the sed command : ( in the date value the slash is protected by \ )

cat crontab-test|sed -e "s/Modifie le : 20\/06\/08/Modifie le : 11\/22\/33/g"

but when i want to put the values in variables

j_modif_old="20/06/08"
j_modif_new="11/22/33"

cat crontab-test|sed -e "s/Modifie le : ${j_modif_old}/Modifie le : ${j_modif_new}/g"

i have the following error :
sed: Function s/Modifie le : 20/06/08/Modifie le : 11/22/33/g cannot be parsed.

how can i do this , protecting the "/" when parsing ?

thanks in advance
Christian
# 5  
Old 01-08-2009
Instead of using s/.../.../ try s^...^...^ or another character.
# 6  
Old 01-08-2009
Thank you very very much !

bye
Christian
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Updating variables using sed or awk

Hi, I have a file(testfile.txt) that contains list of variables as shown below. T $$FirstName=James $$LastName=Fox $$Dateofbirth=1980-02-04 ……and so on there are 50 different variables. I am writing a script(script1.sh) that will update the above three variable one by one with the values... (6 Replies)
Discussion started by: Saanvi1
6 Replies

2. Shell Programming and Scripting

Trouble using awk and sed commands

Hi i have a control file which i need to read. It is ',' separated. the 3rd parameter will be ';' separated. I have 2 files: /home/orig.txt /home/join.txt I need a O/P file name based on firstparameter_1.txt and it should have the content of /home/orig.txt and appended content from... (2 Replies)
Discussion started by: Ravindra Swan
2 Replies

3. UNIX for Dummies Questions & Answers

Use of Variables in a sed/awk script

Hi, After looking at the differents post on this forum, I am convinced that I will benefit from the experience of advanced Unix user on some script I have already done for an aeronautical study. Here is one of them : Step 1 : sed -e "s/??/00/g" Base_Awk.txt > Awk_Cut_00.txt4; sed... (11 Replies)
Discussion started by: Marc_Camoc
11 Replies

4. Shell Programming and Scripting

awk or sed or any commands to stripe data

Say i have: g_gateway domain="abc.com" to="123.123.123.123" relay="false" how do i use awk or sed or any method to get the result below: abc.com 123.123.123.123 (3 Replies)
Discussion started by: timmywong
3 Replies

5. Shell Programming and Scripting

Variables into SED or AWK and multiple commands

Hello I am hoping you may help. I am not sure how to go about this exactly, I know the tools but not sure how to make them work together. I have two SED commands that I would like to run in a shell script. I would like to take the manual input of a user (types in when prompted) to be used... (4 Replies)
Discussion started by: lostincashe
4 Replies

6. Shell Programming and Scripting

Have to learn sed and awk commands in kSH?

Links Please??? (2 Replies)
Discussion started by: Diddy
2 Replies

7. Shell Programming and Scripting

Accessing Shell Variables in awk or sed

Hello, I wonder if it is possible to pass and use variables from shell environment into sed or awk. I am trying to achieve something similar to the following using sed or awk: var=some_regular_expression grep "$var" filename # Will extract lines from filename The following code,... (3 Replies)
Discussion started by: nasersh
3 Replies

8. Shell Programming and Scripting

using sed on bash variables (or maybe awk?)

Hi all- I've been fooling with this for a few days, but I'm rather new at this... I have a bash variable containing a long string of various characters, for instance: JUNK=this that the other xyz 1234 56 789 I don't know what "xyz" actually is, but I know that: START=he other and ... (2 Replies)
Discussion started by: rev66
2 Replies

9. Shell Programming and Scripting

Using variables in sed commands

Hi there, I need to be able to put the hostid of my box into a file (replacing the text "enter_hostid_here" so i tried sed -e 's/enter_hostid_here/`hostid`/g' inputfile > outputfile but it takes the `hostid` literally as text .....how can I get this info into the file (ideally in a single... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

10. Shell Programming and Scripting

Manipulating awk $variables using sed?

I have been searching around the forums here trying to find a solution to my problem but not getting anywhere but closer to baldness. I have a 20 column pipe "|" seperated text file. The 14th variable doesnt always exist, but will have the format of YYYYMM or YYYY if it does. I need to take... (2 Replies)
Discussion started by: r0sc0
2 Replies
Login or Register to Ask a Question