Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

sed append with variable

Options
  • 03-04-2014 7:32pm
    #1
    Registered Users 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 Posts: 880 ✭✭✭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 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 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