Find the answer to your Linux question:
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 . . ...
  1. #1
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101

    Question How to use sed substitution using a $variable for a line containing a word

    $ 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
    .
    .
    .
    .
    .
    .
    200 - 300 lines
    (1) /efare1/dist/sparc/home/aksutil/pawan
    [efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\nTime1\nTime2\nTime3/" 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
    Time1
    Time2
    Time3
    DATABASE CONFIG FILE: config/spserver-nggfstatic
    .
    .
    .
    .
    .
    .
    200 - 300 lines
    (1) /efare1/dist/sparc/home/aksutil/pawan
    [efare@e1dsgalnggf18] $ echo $lines
    Code:
    Time1 Time2 Time3 Time4 Time5
    (1) /efare1/dist/sparc/home/aksutil/pawan
    [efare@e1dsgalnggf18] $ echo "$lines"
    Code:
    Time1
    Time2
    Time3
    Time4
    Time5
    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.

    See below:

    (1) /efare1/dist/sparc/home/aksutil/pawan
    [efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\n"$lines"/" ggg
    Code:
    sed: -e expression #1, char 40: unterminated `s' command
    (1) /efare1/dist/sparc/home/aksutil/pawan
    [efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\n'"$lines"'/" ggg
    Code:
    sed: -e expression #1, char 41: unterminated `s' command
    (1) /efare1/dist/sparc/home/aksutil/pawan
    [efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\n'"${lines}"'/" ggg
    Code:
    sed: -e expression #1, char 41: unterminated `s' command
    (1) /efare1/dist/sparc/home/aksutil/pawan
    [efare@e1dsgalnggf18] $ sed "/PROLOGINITSIZE is / s/\(.*\)/\1\n"${lines}"/" ggg
    Code:
    sed: -e expression #1, char 40: unterminated `s' command
    (1) /efare1/dist/sparc/home/aksutil/pawan
    [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 -
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  2. #2
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101

    Exclamation

    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
    Code:
    <meta name=Originator content="Microsoft Word 9">
    (1) /efare1/dist/sparc/home/aksutil/pawan
    [efare@e1dsgalnggf16] $ m=meta;a=arun; sed "15 s/"$m"/"$a"/" ~/aksutil/GF_BDY_AKS.htm|tail +15|head -n 1
    Code:
    <arun name=Originator content="Microsoft Word 9">
    (1) /efare1/dist/sparc/home/aksutil/pawan


    Kindly suggest!!!!!!!!
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

  3. #3
    Linux Newbie SagaciousKJB's Avatar
    Join Date
    Aug 2007
    Location
    Yakima, WA
    Posts
    162
    Code:
    #!/bin/bash
    
    replacement="It worked!"
    string="Replaced"
    
    read text
    
    echo $text | sed "s/$string/${replacement}/ g" -
    Output:
    sagacious@sagacious-desktop:~$ ./replace
    Replaced
    It worked!
    sagacious@sagacious-desktop:~$

    Code:
    #!/bin/bash
    
    replacement="It worked!"
    string="Replaced"
    
    read text
    
    echo $text | sed "s/$string/"${replacement}"/ g" -
    Output:
    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.

  4. #4
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Quoting your search and replace string isn't what you want to do here. For example:

    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 ~]#
    KJB, maybe you want to do something else? This works here...
    Code:
    [root@xxx ~]# sed 's/SOMETHING//g'
    SOMETHING
    
    sdwd
    sdwd

  5. #5
    Linux Newbie SagaciousKJB's Avatar
    Join Date
    Aug 2007
    Location
    Yakima, WA
    Posts
    162
    Quote Originally Posted by likwid View Post
    Quoting your search and replace string isn't what you want to do here. For example:

    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 ~]#
    KJB, maybe you want to do something else? This works here...
    Code:
    [root@xxx ~]# sed 's/SOMETHING//g'
    SOMETHING
    
    sdwd
    sdwd
    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.

  6. #6
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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:
    Code:
    Time1\nTime2\nTime3\nTime4\nTime5
    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:
    param=hello there
    my_program $param
    invokes the program "my_program" with TWO parameters. The first parameter is "hello", and the second is "there".

    So looking at your case, the value of $lines contains a number of phrases separated by newlines. So we see the following:
    Code:
    echo $lines
    
    =>
    
    echo Time1 \
    Time2 \
    Time3 \
    Time4 \
    Time5
    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"
    Now, echo receives only one parameter, and will follow the rules you gave it.

    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:
    Code:
    alex@danu ~ $ result=$(echo string | sed 's/string//'); echo ":$result:"
    ::
    You will note that there is no whitespace between the colons.

    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

  7. #7
    Linux Newbie Sangal-Arun's Avatar
    Join Date
    May 2006
    Location
    Gurgaon, India + Denver Colorado USA
    Posts
    101
    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.
    Brgds,

    ARUN SANGAL
    SCM: 1- 720 251 9962
    Email: sangal.ak04@gmail.com
    Email: sangal_ak04@yahoo.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...