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. Unable to Remove Previous MSDN Installation
    By regix in forum Windows - General Topics
    Replies: 0
    Last Post: 01-01-2005, 09:54 PM
  2. Real Player -- Firefox 0.9.3: No Matching Stream
    By shebang in forum Linux - Software, Applications & Programming
    Replies: 2
    Last Post: 10-01-2004, 12:19 PM
  3. Java and Patern Matching
    By Ashcrow in forum Linux - Software, Applications & Programming
    Replies: 2
    Last Post: 08-18-2003, 04:12 PM
  4. PERL: get lines between 2 markers
    By Blaqb0x in forum Linux - Software, Applications & Programming
    Replies: 4
    Last Post: 11-15-2002, 09:35 AM
  5. Geek Pickup lines
    By in forum General Chat
    Replies: 29
    Last Post: 12-22-2001, 07:36 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
  •