Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

sed append with variable

  • 03-04-2014 07:32PM
    #1
    Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭


    Hi all,

    hopefully someone can help me with this. No solution on the net has worked for me. Its just something simple im missing.

    I want to add a line to a text file after a certain regex. what I have is
    sed ' 
    /dev/ a\ 
    192.168.52.166  host ' ./hosts.txt
    
    I want to insert 192.168.52.166 host after the line with 'dev' in it. Its fine like this but if I need the ip as a variable.
    ipAddress="192.168.52.166  host"
    sed ' 
    /dev/ a\ 
    $ipAddress ' ./hosts.txt
    
    It always uses $ipAddress instead of the value. If I put the whole thing in double quotes I get a sed error. Ive tried loads of different combos of ' ' and " " and I still can get it to use the variable value. Any help would be great


Comments

  • Registered Users, Registered Users 2 Posts: 939 ✭✭✭moycullen14


    jonny666 wrote: »
    Hi all,

    hopefully someone can help me with this. No solution on the net has worked for me. Its just something simple im missing.

    I want to add a line to a text file after a certain regex. what I have is
    sed ' 
    /dev/ a\ 
    192.168.52.166  host ' ./hosts.txt
    
    I want to insert 192.168.52.166 host after the line with 'dev' in it. Its fine like this but if I need the ip as a variable.
    ipAddress="192.168.52.166  host"
    sed ' 
    /dev/ a\ 
    $ipAddress ' ./hosts.txt
    
    It always uses $ipAddress instead of the value. If I put the whole thing in double quotes I get a sed error. Ive tried loads of different combos of ' ' and " " and I still can get it to use the variable value. Any help would be great
    ipAddress="192.168.52.166  host"
    sed "
    /dev/ a\ 
    $ipAddress " ./hosts.txt
    
    replace ' with ". Within " variables are expanded but not filename wildcards. Within ' neither are.


  • Registered Users, Registered Users 2 Posts: 3,500 ✭✭✭Drexel


    ipAddress="192.168.52.166  host"
    sed "
    /dev/ a\ 
    $ipAddress " ./hosts.txt
    
    replace ' with ". Within " variables are expanded but not filename wildcards. Within ' neither are.

    Ive tried it with the double quotes and its garbled. I escaped the \ after the a and it just removed all txt form the file. strange. Cant for the life of me figure this out. Running Solaris 10 if that helps


  • Registered Users, Registered Users 2 Posts: 1,477 ✭✭✭azzeretti


    jonny666 wrote: »
    Ive tried it with the double quotes and its garbled. I escaped the \ after the a and it just removed all txt form the file. strange. Cant for the life of me figure this out. Running Solaris 10 if that helps

    Not sure if I am missing something here but this is an handy online, this works for me anyway:
    ip=" 127.0.0.1 host"; sed "/dev/ s/$/$ip/" ./host.txt
    

    EDIT: Not sure if you meant you want to append the value to the end of the matched line or add a new line under the matched line. This is the former, just add "\n" at the start of $ip to add a new line.


  • Closed Accounts Posts: 4,763 ✭✭✭Fenster


    You've factored your code wrong. Two things are going on:
    1. sed hates variables being used in a regex string (it turns them null, although I've never discovered why), and laughs at your puny attempts to use one. You can do this if you want, through sheer bloody-minded stubbornness (I have), but it isn't easy.
    2. "$foo" and '$foo" are two different things. "$foo" is partially quoted, which means that some characters (~!./$.., etc) have a special meaning to the interpreter. '$foo' is fully quoted, which means that all characters are interpreted as literal characters with no special meaning attached by the interpreter.

      "$ipAddress" is a partially-quoted variable.
      '$ipAddress' is just is just a dollar sign and some other sundry characters.

      Read this for more information on quoting.
    Two Three ways you can approach the problem:

    [LIST=2]
    [*]Treat the entire sed command as a variable, add it to an array, and loop through the array to execute the given sed commands.
    [*]Escape the variable in the manner as given:
    [mark][~] # touch hello.txt
    [mark][~] # echo "hello friend" > hello.txt 
    [mark][~] # bye="goodbye"
    [mark][~] # cat hello.txt | sed -e "s/hello/"$bye"/"
    goodbye friend
    

    [*]Instead of append, try the newline ("\n") character:
    [mark][~] # cat hello.txt | sed -e 's/hello/goodbye\nnew line\n/g;s/ //g'
    goodbye
    newline
    friend
    
    [/LIST]


Advertisement