removing duplicate lines from file with grep

6,723

Solution 1

This might do what you want:

sort -t '|' -k 2,2 -u  foo.dat

However this sorts the input according to your field, which you may not want. If you really only want to remove duplicates, your best option is Perl:

perl -ne '$a=(split "\\|")[1]; next if $h{$a}++; print;' foo.dat

Solution 2

awk -F \| '{if ($2 != 05408736032) print}'

Solution 3

Pure Bash:

oldIFS=$IFS
while read line
do
    IFS=$'|'
    testline=($line)  # make an array split according to $IFS
    IFS=$oldIFS       # put it back as soon as you can or you'll be sooOOoorry
    if [[ ${testline[1]} != "05408736032" ]]
    then
        echo $line
    fi
done < datafile
Share:
6,723

Related videos on Youtube

user5967
Author by

user5967

Updated on September 17, 2022

Comments

  • user5967
    user5967 almost 2 years

    I want to remove all lines where the second column is 05408736032.

    0009300|05408736032|89|01|001|0|0|0|1|NNNNNNYNNNNNNNNN|asdf|
    0009367|05408736032|89|01|001|0|0|0|1|NNNNNNYNNNNNNNNN|adff|
    
  • Dennis Williamson
    Dennis Williamson almost 15 years
    You can leave out the "if" and the "print": awk -F \| '$2 != "05408736032"'