【问题标题】:how -f , -s options work with the uniq command?-f , -s 选项如何与 uniq 命令一起使用?
【发布时间】:2011-11-10 11:25:04
【问题描述】:

根据 uniq 的手册页

-f 选项用于跳过字段

用于跳过字符的 -s 选项

有人可以用相关的例子解释一下,这两个选项实际上是如何工作的吗?

【问题讨论】:

  • -1。似乎您正在列出所有 UNIX 命令,并提出问题来为您澄清手册页。除了google,我建议unix.stackexchange.com

标签: linux shell unix uniq


【解决方案1】:

香草uniq:

/tmp$ cat > foo
foo
foo
bar
bar
bar
baz
baz
/tmp$ uniq foo
foo
bar
baz

uniq -s 跳过第一个字符:

/tmp$ cat > bar
1foo
2foo
3bar
4bar
5bar
6baz
7baz
/tmp$ uniq -s1 bar
1foo
3bar
6baz

uniq -f 跳过输入的第一个字段(此处为主机):

/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】:

    对我来说看起来很清楚,但无论如何你都可以了。

    -f 跳过字段。所以

    (ol)noufal@sanitarium% echo "a b c\nd e c" | uniq -c
          1 a b c
          1 d e c
    

    打印两个单独的行,但如果您跳过前两个字段 (-f2) 并仅比较最后一个字段,

    (ol)noufal@sanitarium% echo "a b c\nd e c" | uniq -c -f2
          2 a b c
    

    它们都是一样的。

    同样,

    (ol)noufal@sanitarium% echo "abc\ndec" | uniq -c
          1 abc
          1 dec
    (ol)noufal@sanitarium% echo "abc\ndec" | uniq -c -s2
          2 abc
    

    我们在这里跳过前两个字符(而不是字段)。

    关于字段的定义,手册上有。

    字段是一系列空白(通常是空格和/或制表符),然后 非空白字符。

    【讨论】:

      猜你喜欢
      • 2013-02-08
      • 2021-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-02
      • 1970-01-01
      相关资源
      最近更新 更多