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