我在 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**
打印每行的行号并保存到另一个文本。
答案1
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