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.

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.

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

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

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

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

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

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.

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:

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:

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

Thanks yes I did try that but it did not print out. when I changed the command from

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

Thanks yes. I tried with a number of senarios with " " around the

-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

-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

I think you're overthinking it a bit. The quotes join things together. If you don't want them joined, don't put them in quotes.

Put your expressions in double quotes.

sed -e '/./{H;$!d;}' "-e "x;/$string1/!d;" -e "x;/$string2/!d" filename

Thanks Agama
I will re-write the script with what you showed me there and let you know.

---------- Post updated at 05:12 PM ---------- Previous update was at 12:05 PM ----------

Hi I tried the modified script

#!/usr/bin/env ksh
cd /usr/logs/appserver/
ls -ltr XSLog*

printf "enter filename: "
read filename
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

sed -e '/./{H;$!d;}' -e "x;/$string1/!d"  $p2  $filename 

bash-2.05$ 

but I could not get the second variable to be recognized.
Trying the first variable works but two, it seems to just hang and not print out.
I tried with the ";" before and after the first

 -e "x;/$string1/!d"

.
sorry to take up your time with this.

---------- Post updated 04-05-12 at 03:27 PM ---------- Previous update was 04-04-12 at 05:12 PM ----------

agama, Corona688 many thanks for your suggestions and help. I managed to get it working. I noticed I had an extra

 -e "x;/$string2/!d" 

on the second string when actually it only required

  "/$string2/!d"
 sed -e '/./{H;$!d;}' -e "x;/$string1/!d; $p2" filename   

Many thanks again
Regards:)