Results 1 to 4 of 4

Thread: deleting previous 'n' lines of matching the pattern

  1. #1
    Associate
    Join Date
    Apr 2007
    Posts
    18

    deleting previous 'n' lines of matching the pattern

    Hi all,

    how can I delete previous n number of lines of matching pattern..

    I know how to delete the lines after the matching pattern using sed command

    sed -e '/pattern1/,/pattern2/d' filename

    but I am searching for deleting previous 3 lines of matching pattern.....

    I am doing this because I want to delete all the lease entries matching MAC address....

    Please help....

  2. #2
    Administrator Advisor peter's Avatar
    Join Date
    Apr 2004
    Posts
    882
    Try using "grep -n -B <lines> <string>". That will give the number of lines before a matching string and the line numbers.

    You could then keep the line numbers in an array and then delete each line afterwards.

  3. #3
    Associate
    Join Date
    Apr 2007
    Posts
    18

    How can I use sed command to delete line using line no.

    Thanks!

    Now I've gotta find out how to delete lines if we know the line number... probably I have to use sed script... if you know then please let me know how can I do it......


    Thanks for your reply...

  4. #4
    Moderator
    Advisor
    redhead's Avatar
    Join Date
    Jun 2001
    Location
    Copenhagen, Denmark
    Posts
    811
    I am doing this because I want to delete all the lease entries matching MAC address....
    What is wrong with something simple, like:
    Code:
     egrep -v '(DHCPREQUEST | DHCPACK | DHCPDISCOVER | DHCPOFFER)' -e TH:EM:AC:AD:DR:AS /var/log/messages
    Don't worry Ma'am. We're university students, - We know what We're doing.
    'Ruiat coelum, fiat voluntas tua.'
    Datalogi - en livsstil; Intet liv, ingen stil.

Similar Threads

  1. GL not working ati 7000
    By Mip in forum Linux - Software, Applications & Programming
    Replies: 19
    Last Post: 10-28-2004, 09:29 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •