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.

Shell Script Question

  • 27-02-2004 02: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: 36,228 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,780 ✭✭✭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