How to delete lines from a file start with word unable - Unix

This is a discussion on How to delete lines from a file start with word unable - Unix ; Hi All, I have a file around 700MB size and I want to remove all the lines which start with word "unable" and also the line above that line too. Please help me. Bhargava....

+ Reply to Thread
Results 1 to 2 of 2

Thread: How to delete lines from a file start with word unable

  1. How to delete lines from a file start with word unable

    Hi All,
    I have a file around 700MB size and I want to remove all the lines
    which start with word "unable" and also the line above that line too.
    Please help me.

    Bhargava.

  2. Re: How to delete lines from a file start with word unable

    2008-10-25, 10:13(-07), Bhargava S Akula:
    > I have a file around 700MB size and I want to remove all the lines
    > which start with word "unable" and also the line above that line too.
    > Please help me.

    [...]

    awk '
    NR > 1 && !/^unable/ && prev !~ /^unable/ {print prev}
    {prev = $0}
    END {if (!/^unable/) print}'

    or optimised a little bit:

    awk '
    /^unable/ {skip=1; next}
    !skip && NR > 1 {print prev}
    {prev=$0; skip=0}
    END {if (!/^unable/) print}'

    As you're only removing lines, you can work in place, as in:

    awk '...' < file 1<> file

    but that won't truncate the file.

    you could do:

    {
    awk '...' < file
    perl -e 'truncate STDOUT, tell STDOUT'
    } 1<> file

    but then, you might as well do everything in perl.

    --
    Stéphane

+ Reply to Thread