Advanced `uniq` with “unique part regex”

前端 未结 3 1132
广开言路
广开言路 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:28

    Not using uniq but using gnu-awk you can get the results you want:

    awk -v re='![[:alnum:]]+!' 'match($0, re, a) && !(a[0] in p) {p[a[0]]; print}' file
    foo!bar!baz
    !baz!quix
    
    • Passing required regex using a command line variable -v re=...
    • match function matches regex for each line and returns matched text in [a]
    • Every time match succeeds we store matched text in an associative array p and print
    • Thus effectively getting uniq function with regex support

提交回复
热议问题