Linux - Shell - cut: low with awk

  1. Outline
    1. Brief shell command line tool cut
  2. background
    1. Occasionally need to use awk to filter specific columns
      1. awk is a very powerful
      2. But behind the powerful, but along with the complex
        1. In fact, the same function, awk is not much complicated
    2. If it is a simple task, cut tool is completely capable of
      1. Specific locations within the cutting line
      2. Specific field within the cutting line
      3. Description may not be accurate, the following will be an example

1. Prepare

  1. the
    1. centos7
  2. file
    1. cutdemo01

      1:2:3:4:5
      1:2:3:4:5
      1:2:3:4:5
    2. cutdemo02

      1   2   3   4   5
      1   2   3   4   5
      1   2   3   4   5

2. Scene 1: specific locations within the cutting line

  1. Outline
    1. I want specific characters within the cut line
  2. command

    1. Command 1: Cutting a single character

      # -c 表示切割行内的 特定字符
      # 下标从 1 开始
      # 如果超出范围, 会返回 空内容
      > cut -c1 cutdemo01
      1
      1
      1
    2. Command 2: Cutting consecutive characters

      # 下标从 1 开始, 3 结束
      > cut -c1-3 cutdemo01
      1:2
      1:2
      1:2
    3. Command 3: Cutting discontinuous character

      # 下标从 1 开始, 3 结束, 外加第 5 个字符
      > cut -c1-3,5 cutdemo01
      1:23
      1:23
      1:23

3. Scenario 2: endo Branch specific field

  1. Outline
    1. Similar cutting mode of awk
  2. command
    1. Under certain field delimiter cut: Command 1

      -d 指定分隔符
      -f 指定字段
      > cut -d':' -f 1 cutdemo01
      1
      1
      1
    2. Cutting the continuous fields specific delimiter: Command 2

      # -f 类似 之前的 -c
      # 结果中, 每个字段, 会用 -d 指定的分隔符隔开
      > cut -d':' -f 1-3 cutdemo01
      1:2:3
      1:2:3
      1:2:3
    3. Discontinuous fields under certain cutting delimiter: Command 3

      > cut -d':' -f1-3,5 cutdemo01
      1:2:3:5
      1:2:3:5
      1:2:3:5
  3. doubt
    1. If you use the tab breakdown, the command-line play no tab, use \ t escape not so that, how to do
      1. You can take a look at the man command
        1. Without -d, the default is to use the tab to points
  4. pit
    1. cut in only one character as a delimiter
      1. So there may be two possible Xiahuikeng
        1. Continuous need more characters, as delimiters
        2. Simultaneous use of multiple characters, as delimiters
      2. solve
        1. Use awk

ps

  1. ref
    1. Linux Shell Scripting --cut command
      1. It very simple
    2. The difference between the cut and awk
      1. To the point of inspiration, made me realize the power of awk
  2. Recently restless, I do not know what to write
    1. Before things get to stopgap
      1. Charge a small one ...

Guess you like

Origin www.cnblogs.com/xy14/p/12024067.html