Passing variables to sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing variables to sed
# 1  
Old 03-07-2005
Passing variables to sed

Hi folks,

I'm looking for a solution to pass variables to a sed-command. I'm reading a lot of threats and also the q&a "How can I use a variable in sed?". None of these commands works. I'm using AIX 5.2.

I want to do the following:

NUMBER=` echo 38341` | sed -n '/$NUMBER/p'

an obtained this:

+ + sed -n /$NUMBER/p
+ echo 38341
NUMBER=38341


If I try this command

NUMBER=` echo 38341` | sed -n "/$NUMBER/p"

I obtained this:

+ sed -n //p
+ sed: 0602-410 The first regular expression cannot be null.
+ echo 38341
NUMBER=38341

The problem I have is, I need the NUMBER as a line number to access these line in a second file.

Has anybody an idea about this?

THX
# 2  
Old 03-07-2005
WE can not use enviroment variables with sed externally. With pattern match we can use \1, \2. Using awk we can make your requirement easily.

To put line number in a file with sed then,
Code:
sed = filename | sed 'N;s/\n/  /g'

Plz. Can you explain on " I need the NUMBER as a line number to access these line in a second file." this with example, so that we can get the solution for that easily.

hth.
# 3  
Old 03-07-2005
No need for the backquoted echo, you also need to change the sed syntax slightly, and also specify the file you're searching. Though the syntax you're using would work with the pipe, you're just piping "nothing" to sed (same as doing a "cat /dev/null | sed blah..." - pointless). Either put the commands on seperate lines, or change that pipe to a semicolon...
Code:
NUMBER=38341
sed -n "$NUMBER p" my_file_here
# or
NUMBER=38341; sed -n "$NUMBER p" my_file_here

Cheers
ZB
# 4  
Old 03-07-2005
I found a solution in between:

NUMBER=`printf "38341\n"` | sed -n "$NUMBER"'p'

(watch one the double quotes "$NUMBER" and quotes 'p')

prints:

+ sed -n 38341p
+ + printf 38341\n
NUMBER=38341

and in real live:

NUMBER=`printf "38341\n"`| sed -n "$NUMBER"'p' < save/nb_tables.xml

result:

+ sed -n 38341p
+ + 0< save/nb_tables.xml
+ printf 38341\n
NUMBER=38341
JOBNAME="FCRRAFM"

That's what I want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

2. Shell Programming and Scripting

Passing variables to a sed function

Hi everyone, I've re-written some of our scripts to use more functions and I've run into a situation where passing a variable to a sed function does not work. My function is a one-liner sed command as follows: function StringSub() { sed -i "${1}/${2}/${3}/${4}" ${5} } Where ${1} through... (4 Replies)
Discussion started by: richardsantink
4 Replies

3. Shell Programming and Scripting

Help with passing multiple variables into SED

Hello I am hoping you can help. I use ksh in Solaris9 I am trying to pass user imputed variables into SED but for some reason can only get SED to recognize one variable. I have experimented with te below command with putting ' ' and " " in different places but I cant seem to get it to... (8 Replies)
Discussion started by: lostincashe
8 Replies

4. Shell Programming and Scripting

Passing 2 variables

Hi All, I need to pass 2 variables name 'vamskt' and 'vamsi'. Here is my question: delete from gpi.usergroup where usg_user_id in ('vamskt'); delete from gpi.userroles where uro_user_id in ('vamskt'); delete from gpi.user where usr_id in ('vamskt'); insert into gpi.user... (3 Replies)
Discussion started by: tvamsikiran
3 Replies

5. Shell Programming and Scripting

passing variables to sed in ksh

Hi, i need help passing variables to sed using ksh. My goal is to get particular data from log files. first i put a mark to the log files. echo "TEST_"`date + %m_%d_%Y_%T"` >markFile this will produce a 'markFile' which contain text like this TEST_06_01_2009_21:55:09 then i put the mark... (2 Replies)
Discussion started by: d.anggrianto
2 Replies

6. Shell Programming and Scripting

sed pattern matching or passing variables

I need sed to add a "/>" to the end of a line that contains/starts with <meta. current line is <meta name="keywords" content="kayword 1, kwyword2"> and should be <meta name="keywords" content="kayword 1, kwyword2 " /> i need something like this? find . -name "*.html" -print0 | xargs... (6 Replies)
Discussion started by: sky_rivers
6 Replies

7. Shell Programming and Scripting

Passing variables to sed

Hi Folks, How can I make the following to work from a korn shell? old="OLDSTRING" new="NEWSTRING" file="myfile.txt" sed -n 's/$old/$new/gp' $file Thanks in advance rogers42 (3 Replies)
Discussion started by: rogers42
3 Replies

8. Shell Programming and Scripting

passing variables to sed function in a script ....

Hello , I have a script named testscript.sh wherein I have two variables $var and $final (both of which contain a number) I have a sed write function inside this script as follows: sed '1,2 w somefile.txt' fromfile.txt Now , in the above i want to pass $var and $final instead of... (2 Replies)
Discussion started by: shweta_d
2 Replies

9. Shell Programming and Scripting

passing variables to sed inside script

I am trying to pass a regular expression variable from a simple script to sed to remove entries from a text file e.g. a='aaaa bbbb cccc ...|...:' then executing sed from the script sed s'/"'$a"'//g <$FILE > $FILE"_"1 my output file is always the same as the input file !! any... (5 Replies)
Discussion started by: Daniel234
5 Replies

10. Shell Programming and Scripting

passing variables

Hi, Is there any way to pass variable to a sed script.For awk we have -v option.like that do we have any way to pass variable to a sed script from a awk script or from normal script? Thanx, sounder (1 Reply)
Discussion started by: sounder123
1 Replies
Login or Register to Ask a Question