Help with passing multiple variables into SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with passing multiple variables into SED
# 1  
Old 04-02-2012
Question 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 recognize the second variable.
Examples I can find dont seem to cover this in this form.

Code:
cat filename | sed -e '/./{H;$!d;}' -e "x;/$AAAA/!d;" -e '/./{H;$!d;}' -e "x;/$BBBB/!d"

I am using this SED to specifically strip out paragraphs containing text that the user inputs.
Normal I would simply use the below command if I were not using variables and that works fine - for example I have known values of 12345 and 8888.

Code:
cat filename | sed -e '/./{H;$!d;}' -e 'x;/12345/!d; -e 'x;/8888/!d'

If you are able to help or spot what I am doing wrong here I would appreciate it.
Regards
# 2  
Old 04-02-2012
I don't see anything obviously wrong with it -- your variables are in properly double-quoted strings -- but surely it depends on what the values of AAAA and BBBB are...
# 3  
Old 04-02-2012
Data

Hello.
Yes, the strings are user input, just numbers or straight text no special characters or symbols.
Regards.
Smilie

---------- Post updated at 02:55 PM ---------- Previous update was at 12:47 PM ----------

I made a cut and paste error earlier. Here is the more expanded version of what I am doing.
Code:
printf "enter search string: "
read string1
printf "Another search string [yes|no] "
read ans
if [ "$ans" == "yes" ]
then
printf "enter 2nd search string: "
read string2
cat filename | sed -e '/./{H;$!d;}' "-e 'x;/$string1/!d; -e 'x;/$string2/!d"

If that shows any noticeable problems.
I have tried double quotes around both strings "$string" and further out
# 4  
Old 04-03-2012
You don't need to cat the file and pipe it to sed, sed can read the file directly. The problem seems to me to be the quotes. I believe this is what you need; double quotes round the expressions that you wish to have shell variables expanded in.

Code:
sed -e '/./{H;$!d;}'  -e "x;/$string1/!d;"  -e "x;/$string2/!d" filename >output-file

---------- Post updated at 23:22 ---------- Previous update was at 23:13 ----------

You might also consider logic like the sample below, so the sed is executed with the correct number of expressions when the user does not enter 'yes' at the prompt:

Code:
printf "enter search string: "
read string1
printf "Another search string [yes|no] "
read ans
if [ "$ans" == "yes" ]
then
    printf "enter 2nd search string: "
    read string2
    p2="-e x;/$string2/!d"
fi

/usr/bin/sed -e '/./{H;$!d;}' -e "x;/$string1/!d;"  $p2  filename >output-file


Note that $p2 is purposely not in quotes on the sed command.

---------- Post updated at 23:37 ---------- Previous update was at 23:22 ----------

And one last thing....
I'm not quite sure what your sed is trying to accomplish. If I had to guess, I would say that you are trying to use sed to print all lines in the file that contain either string1 or string2. At least when I run it, with that assumption, it isn't doing that.

If that is what you are trying to accomplish, I'd suggest the following:

Code:
printf "enter search string: "
read string1

printf "Another search string [yes|no] "
read ans
if [ "$ans" == "yes" ]
then
    printf "enter 2nd search string: "
    read string2
    p2="/$string2/!d;"
else
    p2="d"
fi

sed -e "/$string1/b" -e "$p2" input-file >output-file


Last edited by agama; 04-03-2012 at 12:41 AM.. Reason: grammar; compatable with BSD style seds
# 5  
Old 04-03-2012
Thanks yes I did try that but it did not print out. when I changed the command from
Code:
-e "x;/$string1/!d;"  to   -e "x;/$string/d"

then I get a limited print out.
It closes with
Quote:
-e: not found
# 6  
Old 04-03-2012
Quote:
Originally Posted by lostincashe
Thanks yes I did try that but it did not print out. when I changed the command from
Code:
-e "x;/$string1/!d;"  to   -e "x;/$string/d"

then I get a limited print out.
It closes with
This seems like a semicolon in one of your expressions is not quoted and the shell is seeing that as the end of the command and is trying to execute the -e as a command.
# 7  
Old 04-03-2012
Thanks yes. I tried with a number of senarios with " " around the
Code:
-e "x;/12345/!d; -e 'x;/8888/!d" and have tried   -e "x;/12345/!d"; -e "x;/8888/!d" and tried  -e "x;/12345/!d;" -e "x;/8888/!d"

None of which seems to work taking the second variable.
I have also tried
Code:
-e "x;/12345/!d; -e 'x;/8888/!d"

but that dos not work either?

So I am a little stuck. I was wondering if there is a way using ( ) brackets or something I am missing. I am using Solaris 9 ksh

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

ksh passing to awk multiple dyanamic variables awk -v

Using ksh to call a function which has awk script embedded. It parses a long two element list file, filled with text numbers (I want column 2, beginning no sooner than line 45, that's the only known thing) . It's unknown where to start or end the data collection, dynamic variables will be used. ... (1 Reply)
Discussion started by: highnthemnts
1 Replies

3. 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

4. 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

5. 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

6. 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

7. 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

8. Shell Programming and Scripting

Help needed in processing multiple variables in a single sed command.

Is it possible to process multiple variables in a single sed command? I have the following ksh with three variables and I want to search for all variables which start with "var" inside input.txt. I tired "$var$" but it just prints out everyting in input.txt and does not work. $ more test.ksh... (5 Replies)
Discussion started by: stevefox
5 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 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... (3 Replies)
Discussion started by: jfisch
3 Replies
Login or Register to Ask a Question