正则表达式匹配列的第一个字符

时间:2017-10-04 18:31:39

标签: regex linux awk grep

我目前有一个名为computers.txt的文件

  ST 314,Fox,PC,Unix
  ST 13,Newman,Mac
  ST 212,Frank,Linux,PC
  BP 311,Jones,PC
  ST 404,Walden,PC,Linux
  ST 303,Noblitt,PC
  ST 102,Christensen,Mac
  ST 301,Ward,PC
  SB 444,Smith,Mac
  ST 202,Kirby,Mac,PC
  ST 361,Lab,Linux
  ST 333,Hu,Unix,PC

如果成员名称以字母F,G,H,I,J或K开头,我需要使用正则表达式显示该行。有什么帮助?

3 个答案:

答案 0 :(得分:1)

使用 grep

grep '^[^,]*, *[FGHIJK]' file

输出:

ST 314, Fox, PC, Unix
ST 212, Frank, Linux, PC
BP 311, Jones, PC
ST 202, Kirby, Mac, PC
ST 333, Hu, Unix, PC

答案 1 :(得分:0)

awk -F', *' '$2 ~ /^[FGHIJK]/' file

答案 2 :(得分:0)

cat jake
ST 314, Fox, PC, Unix
ST 13, Newman, Mac
ST 212, Frank, Linux, PC
BP 311, Jones, PC
ST 404, Walden, PC, Linux
ST 303, Noblitt, PC
ST 102, Christensen, Mac
ST 301, Ward, PC
SB 444, Smith, Mac
ST 202, Kirby, Mac, PC
ST 361, Lab, Linux
ST 333, Hu, Unix, PC

然后

awk -F, '$2~/^[FGHIJK]/' jake
ST 314, Fox, PC, Unix
ST 212, Frank, Linux, PC
BP 311, Jones, PC
ST 202, Kirby, Mac, PC
ST 333, Hu, Unix, PC