Advanced `uniq` with “unique part regex”

前端 未结 3 1134
广开言路
广开言路 2021-01-20 02:16

uniq is a tool that enables once to filter lines in a file such that only unique lines are shown. uniq has some support to specify when two lines a

3条回答
  •  无人共我
    2021-01-20 02:29

    You can do this with just grep and sort

    DATAFILE=file.dat
    
    for match in $(grep -P '(!\w+!)' -o "$DATAFILE" | sort -u); do 
      grep -m1 "$match" "$DATAFILE";
    done
    

    Outputs:

    foo!bar!baz
    !baz!quix
    

提交回复
热议问题