Find the answer to your Linux question:
Results 1 to 4 of 4
Hi All, I have a real easy question. The command sed '3a\blah' boo appends blah into line 3 of file boo. If I have the line number and the text ...
  1. #1
    Just Joined!
    Join Date
    Feb 2005
    Posts
    37

    easy sed question with a variable

    Hi All,

    I have a real easy question.

    The command sed '3a\blah' boo appends blah into line 3 of file boo. If I have the line number and the text stored into variables $x and $y, how do I go about inserting them into this sed command?

    I've been reading that I have to use double quotes so that it interprets the variables but I'm stuck at that point

    sed "($x)a\$y" boo doesn't work neither does brackets.

    Any suggestions? Thanks!

    Jon

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Alrighty.

    So the type of quotes that you use matters. Single quotes (') will interpret the string literally. Double quotes (") will evaluate any variables.

    Having now done this, we need to actually place the variables into the command. This is pretty simple:
    Code:
    #!/bin/bash
    
    lineno=3
    text=blah
    
    sed "${lineno}a\\${text}" boo
    Within the double quotes, we are using a special form of variable reference, the ${...} form. This is because there is other text. If we just used $lineno, we would actually have a variable $linenoa (that is, the 'a' for 'append' would have been considered a part of the variable name). By using ${...}, we clearly delimit the variable name.

    Note that we also use "\\". This is because in double-quoted strings, backslash has a special meaning. "\\" means "put a literal backslash here".

    Does this make sense?
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Feb 2005
    Posts
    37
    Thanks! I didn't try that but that totally makes sense. Here is the way I found out how to do it

    sed "$lineno"'a\'"$insert" filename

    you can see the extra single quotes between the a\

    thanks again

  4. #4
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    That works because in Bash, strings are just concatenated together. I could imagine such a technique not working in other languages (though I can't give a specific example currently).

    The main problem with the way you do it is the number of different quotes. Looking at your code, I can definitely tell what it does, but it's a bit harder to read and immediately make sense of. Especially since "'" (that is, a single quote surrounded by double quotes) would appear as a literal single quote, so I need to figure out if your "$lineno"' is $lineno' surrounded by double quotes, or $lineno surrounded by double quotes, with a single quote after.

    There are often many ways to write a line of code, but you do want to consider what is more easily understandable. When you come back to this script in 4 weeks, you should be able to look at this line and figure out what it does with some ease.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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