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.

grep a number of lines in Solaris

  • 17-04-2007 04:20PM
    #1
    Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭


    Hi all,
    I need to find the line before a line in output on a Solaris box, now with gnu grep I can
    grep -1<pattern> <file>|head -1
    Unfortunately this has to work in other environments so I can't just install GNU grep :(. how would I achieve the same effect on a vanilla Solaris box?


Comments

  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    Probably not the most efficient solution, by lots but I found a way to do it
    ...|sed -n '1,/pattern/ p'|tail -2|head -1


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


    You'll more than likely have perl.
    Wouldn't that give you more options?

    You will have 'ex'.
    I had to use this for some record munging on an archaeological AIX install once.

    Try this:
    ex -s -R -c '/<pattern>/-1|q' <filename>


  • Registered Users, Registered Users 2 Posts: 1,110 ✭✭✭Skrynesaver


    Hi Niall,

    vi proves it's worth again ;)

    While perl is a given on these boxes, I was trying to keep it a shell script, it's sort of grown from a 5 line one off solution to a generic solution (as they do). however it's for parsing and modifying an ldap directory so if I was to use perl I'd have a system(ldapsearch ....) in a loop or I'd have to use Net::LDAP and insist on them installing the module.


Advertisement