Results 1 to 5 of 5

Thread: sed / awk: how to delete lines that DON'T match a pattern?

  1. #1
    Join Date
    Oct 2004
    Beans
    Hidden!

    sed / awk: how to delete lines that DON'T match a pattern?

    I have a file that looks like this:

    Code:
    192.168.10.123 blah blah text
    192.168.10.123
    192.168.10.10 more text
    192.168.10.10
    192.168.10.12
    192.168.10.9 yet more text
    How can I delete the lines that are only IP addresses, and leave the lines that are IPs with the text after them?

  2. #2

    Re: sed / awk: how to delete lines that DON'T match a pattern?

    Quote Originally Posted by negativ View Post
    I have a file that looks like this:

    Code:
    192.168.10.123 blah blah text
    192.168.10.123
    192.168.10.10 more text
    192.168.10.10
    192.168.10.12
    192.168.10.9 yet more text
    How can I delete the lines that are only IP addresses, and leave the lines that are IPs with the text after them?
    Code:
    $ grep -v '^[\.[:digit:]]*$' <myfile
    That matches all lines that contain nothing but a string of dots and digits. Might not be 100 percent effective. For that matter, you could

    Code:
    $ grep ' ' <myfile
    which would pick out only lines with no spaces in them (the -v flag inverts the test).

  3. #3
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: sed / awk: how to delete lines that DON'T match a pattern?

    Hi,
    just to show some sed equivalents:

    print lines that don't match a pattern, or delete lines that do ... both achieving the same result

    Code:
    $ sed -n '/[0-9]$/!p' testfile
    192.168.10.123 blah blah text
    192.168.10.10 more text
    192.168.10.9 yet more text
    
    $ sed '/[0-9]$/d' testfile
    192.168.10.123 blah blah text
    192.168.10.10 more text
    192.168.10.9 yet more text

  4. #4
    Join Date
    Sep 2006
    Beans
    2,914

    Re: sed / awk: how to delete lines that DON'T match a pattern?

    Quote Originally Posted by negativ View Post
    I have a file that looks like this:

    Code:
    192.168.10.123 blah blah text
    192.168.10.123
    192.168.10.10 more text
    192.168.10.10
    192.168.10.12
    192.168.10.9 yet more text
    How can I delete the lines that are only IP addresses, and leave the lines that are IPs with the text after them?
    Code:
    $ grep -P -v '^\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}$' file
    192.168.10.123 blah blah text
    192.168.10.10 more text
    192.168.10.9 yet more text

  5. #5
    Join Date
    Sep 2006
    Beans
    2,914

    Re: sed / awk: how to delete lines that DON'T match a pattern?

    Quote Originally Posted by DaithiF View Post
    Hi,
    just to show some sed equivalents:

    print lines that don't match a pattern, or delete lines that do ... both achieving the same result

    Code:
    $ sed -n '/[0-9]$/!p' testfile
    192.168.10.123 blah blah text
    192.168.10.10 more text
    192.168.10.9 yet more text
    
    $ sed '/[0-9]$/d' testfile
    192.168.10.123 blah blah text
    192.168.10.10 more text
    192.168.10.9 yet more text

    that is, provided OP's data always have an IP address as its first field.

Tags for this Thread

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
  •