问题
According to manual page for uniq
the -f option is for skipping fields
the -s option for skipping characters
Can someone explain with relevant examples, how actually these two options work?
回答1:
Vanilla uniq
:
/tmp$ cat > foo
foo
foo
bar
bar
bar
baz
baz
/tmp$ uniq foo
foo
bar
baz
uniq -s
to skip over the first character:
/tmp$ cat > bar
1foo
2foo
3bar
4bar
5bar
6baz
7baz
/tmp$ uniq -s1 bar
1foo
3bar
6baz
uniq -f
to skip over the first field of the input (here, hosts):
/tmp$ cat > baz
127.0.0.1 foo
192.168.1.1 foo
example.com bar
www.example.com bar
localhost bar
gateway1 baz
192.168.1.254 baz
/tmp$ uniq -f1 baz
127.0.0.1 foo
example.com bar
gateway1 baz
回答2:
It looks clear to me but here you go anyway.
-f
skips fields. So
(ol)noufal@sanitarium% echo "a b c\nd e c" | uniq -c
1 a b c
1 d e c
prints two separate lines but if you skip the first two fields (-f2) and compare only the last,
(ol)noufal@sanitarium% echo "a b c\nd e c" | uniq -c -f2
2 a b c
they're both the same.
Similarly,
(ol)noufal@sanitarium% echo "abc\ndec" | uniq -c
1 abc
1 dec
(ol)noufal@sanitarium% echo "abc\ndec" | uniq -c -s2
2 abc
We skip the first two characters here (rather than fields).
As for the definition of fields, the manual has that.
A field is a run of blanks (usually spaces and/or TABs), then non-blank characters.
来源:https://stackoverflow.com/questions/8078769/how-f-s-options-work-with-the-uniq-command