Results 1 to 7 of 7
$ cat ggg
Code:
/E*Fare/testteam/public/pf3/nggfstatic/4k-pf3_3.case
REGION1:
/E*Fare/dist/src/nggfstaticbase/EFare/Server
CODEBASE1:
/dev_tools/LINUXMTP-4/EFS070718E/EFare/Server
DATABASE1: nggfstatic
SCRIPT: /efare1/admin/ezlcn/scripts/pf3_3_scriptlist.input
PROLOGINITSIZE not yet set
You are using: time
PROLOGINITSIZE is 200M
DATABASE CONFIG FILE: config/spserver-nggfstatic
.
.
...
- 08-07-2007 #1
How to use sed substitution using a $variable for a line containing a word
$ cat ggg
(1) /efare1/dist/sparc/home/aksutil/pawanCode:/E*Fare/testteam/public/pf3/nggfstatic/4k-pf3_3.case REGION1: /E*Fare/dist/src/nggfstaticbase/EFare/Server CODEBASE1: /dev_tools/LINUXMTP-4/EFS070718E/EFare/Server DATABASE1: nggfstatic SCRIPT: /efare1/admin/ezlcn/scripts/pf3_3_scriptlist.input PROLOGINITSIZE not yet set You are using: time PROLOGINITSIZE is 200M DATABASE CONFIG FILE: config/spserver-nggfstatic . . . . . . 200 - 300 lines
[efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\nTime1\nTime2\nTime3/" ggg
(1) /efare1/dist/sparc/home/aksutil/pawanCode:/E*Fare/testteam/public/pf3/nggfstatic/4k-pf3_3.case REGION1: /E*Fare/dist/src/nggfstaticbase/EFare/Server CODEBASE1: /dev_tools/LINUXMTP-4/EFS070718E/EFare/Server DATABASE1: nggfstatic SCRIPT: /efare1/admin/ezlcn/scripts/pf3_3_scriptlist.input PROLOGINITSIZE not yet set You are using: time PROLOGINITSIZE is 200M Time1 Time2 Time3 DATABASE CONFIG FILE: config/spserver-nggfstatic . . . . . . 200 - 300 lines
[efare@e1dsgalnggf18] $ echo $lines
(1) /efare1/dist/sparc/home/aksutil/pawanCode:Time1 Time2 Time3 Time4 Time5
[efare@e1dsgalnggf18] $ echo "$lines"
Now, when I substituted the values of line starting with "PROLOGINITSIZE is " at command prompt using exact value...system was good. But when I placed the values inside a variable "$lines" then why it is not giving us the same result.. KINDLY SUGGEST.Code:Time1 Time2 Time3 Time4 Time5
See below:
(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\n"$lines"/" ggg
(1) /efare1/dist/sparc/home/aksutil/pawanCode:sed: -e expression #1, char 40: unterminated `s' command
[efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\n'"$lines"'/" ggg
(1) /efare1/dist/sparc/home/aksutil/pawanCode:sed: -e expression #1, char 41: unterminated `s' command
[efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\n'"${lines}"'/" ggg
(1) /efare1/dist/sparc/home/aksutil/pawanCode:sed: -e expression #1, char 41: unterminated `s' command
[efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\n"${lines}"/" ggg
(1) /efare1/dist/sparc/home/aksutil/pawanCode:sed: -e expression #1, char 40: unterminated `s' command
[efare@e1dsgalnggf18] $
Any helps...while using sed and substituting data that is stored inside a variable...(I have done that in one of my other script...skeptical how that one is working fine????)
Thanks -
- 08-07-2007 #2
See here, it's also working when using with variables...but am i missing something in my Q? above:
(1) /efare1/dist/sparc/home/aksutil/pawan
[efare@e1dsgalnggf16] $ tail +15 ~/aksutil/GF_BDY_AKS.htm|head -n 1
(1) /efare1/dist/sparc/home/aksutil/pawanCode:<meta name=Originator content="Microsoft Word 9">
[efare@e1dsgalnggf16] $ m=meta;a=arun; sed "15 s/"$m"/"$a"/" ~/aksutil/GF_BDY_AKS.htm|tail +15|head -n 1
(1) /efare1/dist/sparc/home/aksutil/pawanCode:<arun name=Originator content="Microsoft Word 9">
Kindly suggest!!!!!!!!
- 08-08-2007 #3Output:Code:
#!/bin/bash replacement="It worked!" string="Replaced" read text echo $text | sed "s/$string/${replacement}/ g" -
sagacious@sagacious-desktop:~$ ./replace
Replaced
It worked!
sagacious@sagacious-desktop:~$
Output:Code:#!/bin/bash replacement="It worked!" string="Replaced" read text echo $text | sed "s/$string/"${replacement}"/ g" -
sagacious@sagacious-desktop:~$ ./replace
Replaced
sed: -e expression #1, char 13: unterminated `s' command
sagacious@sagacious-desktop:~$
To my knowledge the {} are not necessary, but I included them just to be sure. The real problem appears to be the double-quotes existing within the sed statement.
I'm not sure why it worked with the quotes of your examples in your second post, but it seems when using "g" the quotes inside just won't work.
I have a question for more sed afficianados..
How can I simply delete a matched character or string rather than replacing it? I've tried 'sed 's/string//g" but that doesn't work.
- 08-09-2007 #4
Quoting your search and replace string isn't what you want to do here. For example:
KJB, maybe you want to do something else? This works here...Code:[root@xxx ~]# echo $LINES 24 [root@xxx ~]# sed 's/"$LINES"/works/g' 24 24 $LINES $LINES "$LINES" works [root@xxx ~]# sed s/$LINES/works/g $LINES $LINES 24 works [root@xxx ~]#
Code:[root@xxx ~]# sed 's/SOMETHING//g' SOMETHING sdwd sdwd
- 08-10-2007 #5
Problem is that it creates whitespace for that character. That's all right when using urls or text with no space ( I can just assignit to a variable to remove white-space, but if it were actually text, it'd create a space in the middle of it. I guess that's not really too bad, though.
- 08-10-2007 #6
Okay. Sangal-Arun, I don't follow exactly what your problem is. Is it the difference between :echo $lines: and :echo "$lines":? I will here assume that it is.
The problem here involves the quoting. The value of $lines is:
Now, because of the way Bash scripting works, variables are expanded and passed exactly the way they are written. So for instance, the following code:Code:Time1\nTime2\nTime3\nTime4\nTime5
invokes the program "my_program" with TWO parameters. The first parameter is "hello", and the second is "there".Code:param=hello there my_program $param
So looking at your case, the value of $lines contains a number of phrases separated by newlines. So we see the following:
So instead of invoking echo with a single parameter separated by newlines, you are instead invoking echo with 5 parameters, that happened to be written on different lines. echo's behavior prints out all parameters separated by spaces. To get echo to print out on different lines, you quote the parameter.Code:echo $lines => echo Time1 \ Time2 \ Time3 \ Time4 \ Time5
Now, echo receives only one parameter, and will follow the rules you gave it.Code:echo "$lines"
In Bash scripting, you ALWAYS quote variables whenever you use them as parameters. Because not quoting leads to weird situations like this.
@ Sagacious
I'm not sure exactly what you mean about whitespace:
You will note that there is no whitespace between the colons.Code:alex@danu ~ $ result=$(echo string | sed 's/string//'); echo ":$result:" ::
The blank line appears in the sed output in likwid's example because if you don't give sed any input on the commandline, it reads the input that you type in. It then prints out the modified version directly below each line of input. In this case, the modified version is nothing, so a blank line appears.DISTRO=Arch
Registered Linux User #388732
- 08-10-2007 #7
Thanks everyone. As sed don't work on multiple lines and "$lines" variable was containing "\n" newline characters in it...so it looked like i showed my poverty in creating that script....but i have correct it now using sed by adding a tag, say ..XYZZYX and later replaced this tag with \n character so all the insertables lines finally looks like we inserted those lines inside a file at a particular line no or after a particular string.
when i was giving sed ' or "/srcstring/Time1\nTime2\nTime3/' or " then..sed was easily understanding what to do...but it will not work if i put the replacing string in a variable. ex below:
Hint:
[e027020@stpbuild e027020]$ echo $a
t1\nt2\nt3
[e027020@stpbuild e027020]$ echo "$a"
t1\nt2\nt3
[e027020@stpbuild e027020]$ echo -e "$a"
t1
t2
t3
But, your suggestions are the final resolution. thanks all.


Reply With Quote
