pass variable to sed like in awk (-v switch)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pass variable to sed like in awk (-v switch)
# 1  
Old 03-21-2010
pass variable to sed like in awk (-v switch)

hi all

is possible to pass shell (bash) variable to sed like it is in awk?

example:

awk script is storred in awk.awk file and I am passing variable called var to this file.
Code:
$ cat awk.awk
{if ($5==var) print $0}

so it works when i issue
Code:
$ bash_var=24
$ ls -l | awk -v var="$bash_var" -f awk.awk
-rw-r--r-- 1 cepi cepi     24 2010-03-20 13:01 awk.awk

Is possible to do similar thing to sed ?
Code:
$ cat sed.sed
sed "$res""s/$/(-,"$name",time2change.co.uk)/"

$res should be number of line which sed will be applied to
$name should be some replace string

so i will run it like
Code:
cat file | sed -f sed.sed     # variable definition will go somewhere here

Thank you

---------- Post updated at 09:42 AM ---------- Previous update was at 09:41 AM ----------

If is not this possible what is your favorite way to pass bash variables to sed ?
I wanted to try this way because sed program could be stored in separate file (so it would not be need to use " and so avoiding escaping and white space interpretation)
Thank you
# 2  
Old 03-21-2010
Here is generic method to use dynamic templates.
Code:
#!/bin/ksh or bash or ...
parse_file()
{
 eval echo  "\"$(cat $1 | sed 's+\"+\\"+g'   )\""
}

###########
replace=newvalue
export replace
parse_file sed1.sed  > $$.tmp
sed -f $$.tmp sed1.txt
rm -f $$.tmp 2>/dev/null

sed1.sed file
Code:
s/$replace/replaced/g

If you have only a sed rule, use something like this
Code:
rule="${res}s/$/(-,$name,time2change.co.uk)/"
sed -e "$rule" somefile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Red Hat

pass a variable line number to sed

num=10 sed -n '$num p' test.txt sed -n '10 p' test.txt works however i am putting the sed command in a loop and the line number is not static Can someone please help me how to achive this. (1 Reply)
Discussion started by: figure20012
1 Replies

2. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

3. Shell Programming and Scripting

pass an awk variable to kill

Does anyone know of a way to do something similar to this with awk and kill? I want to create the variable in awk and pass that variable to kill. ps -ef | grep -i chromium | awk '{$2=x}' | kill -9 $x 2>/dev/null (9 Replies)
Discussion started by: cokedude
9 Replies

4. Shell Programming and Scripting

ksh variable pass to awk

I'm trying to store the response from a nawk command inside of a ksh script. The command is: text=$(nawk -F: '$1 ~ /${imgArray}/ {print $2}' ${etcDir}/captions.txt) From what I can tell, the imgArray variable is not being expanding when it is inside the single quote ('). Is there something I... (4 Replies)
Discussion started by: meman1188
4 Replies

5. Shell Programming and Scripting

Pass perl variable to sed

Hello, I'd like to pass a variable to a sed command in a perl script. The script is like this : #!/usr/bin/perl -w $newline="new"; system q(sed '/insert/ i\ '$newline <sed1.txt >sed2.txt); But the interpretor wouldn't recognize $newline, it inserts a "\n" instead. I've also... (4 Replies)
Discussion started by: hi_ryo
4 Replies

6. Shell Programming and Scripting

pass a variable to sed p in a loop?

Hi, Thanks for looking,,,, (kornshell) tmp3 is a list of line numbers I want to print the lines from my list code: while read j do echo $j #works fine echo $filename #works fine #sed "'$jp'" "$filename" ... (7 Replies)
Discussion started by: JohnMario
7 Replies

7. UNIX for Dummies Questions & Answers

How do I pass a variable to awk?

I have an awk statement where I Need to pass an environment variable but I cannot get it to work: My evironment varible examples below: $FILE1=/dev/fs/file.new $FILE2=/dev/fs/file.old Code below: awk -F"|" ' BEGIN { while( getline < "$FILE1" ) { arr=1 } } arr != 1 { print } '... (12 Replies)
Discussion started by: eja
12 Replies

8. Shell Programming and Scripting

How to pass a variable to Awk ?

I am trying to pass 2 shell variable's ("START" and "END") define earlier in the script to this awk statement, but i can't seem to pass it on. PLs help. set START = xxxx set END = yyyy set selected_file = `awk '/$START/,/$END/' filename` (24 Replies)
Discussion started by: Raynon
24 Replies

9. UNIX for Dummies Questions & Answers

pass variable to awk

i would like to pass a variable to awk wherein the variable comes from external loop. i tried this... let x=0 until test $x -eq 32 do cat file | awk '{ print $1 , "Number" , $($x) }' >> output done thanks, (4 Replies)
Discussion started by: inquirer
4 Replies

10. Shell Programming and Scripting

Pass variable to sed?

Hi there. If variables are named inside of a ksh script, how is it possible to pass these to sed? The affected portion of my script goes something like this: A=`cut -d. -f1 $FILE` B=`cut -d. -f2 $FILE` C=`cut -d. -f3 $FILE` sed 's/1111/$A/g;s/2222/$B/g;s/3333/$C/g' file > anotherfile ... (2 Replies)
Discussion started by: kristy
2 Replies
Login or Register to Ask a Question