Linux

具有多個條件的 grep 逗號分隔欄位

  • September 3, 2018

我在 Linux 伺服器上有一個大文本文件,其中包含以下內容,例如:

123456789012345,00,0000,0000
1234567890123450,00,0000,000
12345678901b111,0,0000,0000
1234567/89011111,00,0000,00000

輸出

12345678901b111,0,0000,0000       line# 3
1234567/8011111?,00,0000,00000    line# 4

所以我的目標是:

我想 grep 行是

not 15 or 16 digits only before first comma
not 2 digits only before second comma
not 3 or 4 digits only before third comma
not 3 or 4 digits only after third comma

**the line should cover ANY of the predefined conditions**

每行列印行號並保存到另一個文本。

**AWK**解決方案:

awk -F, '$1!~/[0-9]{15,16}/ || $2!~/[0-9]{2}/ || $3!~/[0-9]{3,4}/ || $4!~/[0-9]{3,4}/{ 
            printf "%-35s line# %s\n",$0,NR 
        }' file
  • -F,- 將逗號,視為欄位分隔符
  • printf "%-35s line# %s\n"- 對齊/排列的格式化輸出

輸出:

12345678901b111,0,0000,0000         line# 3
1234567/89011111,00,0000,00000      line# 4

引用自:https://unix.stackexchange.com/questions/404766