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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Shell Script Question

  • 27-02-2004 1:49pm
    #1
    Registered Users, Registered Users 2 Posts: 95 ✭✭


    This could equally go in Programming I suppose... Bah?!

    Anyways Shell script problem..

    Lets say I want to parse the output of a command and split it up according to newlines?

    EG:
    ip route show gives output:
    default via 10.0.0.1
    10.4.12.33 via 10.4.12.32
    10.1.2.3 via 10.1.2.1

    now lets say I wanted to insert "
    " instead of each newline.. ? Output would be:
    default via 10.0.0.1
    10.4.12.33 via 10.4.12.32
    10.1.2.3 via 10.1.2.1

    I've tried stuff like:
    ip route show | sed -e 's/\n/
    /g'

    but it doesnt seem to pick up on newlines.. If at all possible I'd like to use sed or some other utility you'll find on pretty much any machine..


Comments

  • Moderators, Arts Moderators Posts: 35,731 Mod ✭✭✭✭pickarooney


    Wild shot in the dark...
    opstring=""
    ip route show|while read nextline
    do 
      opstring=opstring+"-----------"+$nextline
    done  
    


  • Registered Users, Registered Users 2 Posts: 95 ✭✭fractal


    Well fair f*&^s to you..

    Cheers!

    Few tweaks and it worked. :)

    opstring=""
    ip route show | while read nextline
    do
    opstring="$opstring
    $nextline"
    done


  • Registered Users, Registered Users 2 Posts: 2,755 ✭✭✭niallb


    Nice one P,
    I'll stick that construction in my toolbox for the future.

    If you want to do it with sed, I think your problem is that
    sed is treating the newline as a record seperator.
    As such, it is not part of the record (opinions?)

    If instead you substitute on the end of line as in

    ip route show | sed -e 's/$/
    /g' you'll get more mileage.

    Oh, to stick them onto one line with a tool that exists on most systems?

    ip route show | sed -e 's/$/

    /g' | paste -s

    I think the other way is cheaper in resources though.

    NiallB


Advertisement