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 multi-line replace

Options
  • 08-06-2011 10:34am
    #1
    Registered Users Posts: 6,762 ✭✭✭


    Can anyone point me in the right direction for this please?

    I'm trying to find and replace multiple lines of text in a file and can't figure out how to do it with sed/awk.

    I'm trying to enable bash-completion for all users, so editing the /etc/bash.bashrc file on a Ubuntu system.

    I need to replace
    #if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    #    . /etc/bash_completion
    #fi
    
    with
    if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
        . /etc/bash_completion
    fi
    
    i.e. removing the hash character from the start of each of those lines.

    Thanks!
    Tagged:


Comments

  • Registered Users Posts: 6,762 ✭✭✭WizZard


    Managed to find an answer myself :) (via StackOverflow)

    For anyone else who wants to know:
    line=`awk '/enable bash completion/ {print NR}' /etc/bash.bashrc`
    sed -i "$((line+1)),$((line+3))s/^#//g" /etc/bash.bashrc
    


  • Registered Users Posts: 37,485 ✭✭✭✭Khannie


    On linux I would do that like this:

    sed --in-place -r 's/^#//' <filename>

    That should do it I think (unless I'm missing something).


  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    Khannie wrote: »
    On linux I would do that like this:

    sed --in-place -r 's/^#//' <filename>

    That should do it I think (unless I'm missing something).
    Would that not remove all comments?


  • Registered Users Posts: 3,745 ✭✭✭laugh


    You can do a multi line select with vim visual mode.


  • Registered Users Posts: 37,485 ✭✭✭✭Khannie


    croo wrote: »
    Would that not remove all comments?

    See I knew I was missing something. ;)

    There is a feature in sed that let's you select start and end points (I'll paste it in later) but I can't seem to manage to get a replace in with that too, it just spits it to stdout.


  • Advertisement
  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    yeah, it's the 3 lines that makes it tricky but I think WizZards own provided solution is the ticket.


Advertisement