我正在尝试打印只有一个t的行,或者只有一个t的行,其他的都可以。即没有没有没有t的线,没有带有2个或更多t的线,也没有含有1t和1t的线。
我在努力:

egrep '[tT]{1,1}$' filename

这是下面几行:
     nopqrstuvwxyz
     letters    (this line is the one that should not be here)
 The price is *$2*
      one two three (this line should not be here either)
    ONE TWO
 THREE

这些都是文件中有t或t的行。我该怎么办?

最佳答案

$ cat ip.txt
foobaz
nopqrstuvwxyz
letters
The price is *$2*
one two three
ONE TWO
THREE
1234

$ grep -ix '[^t]*t[^t]*' ip.txt
nopqrstuvwxyz
The price is *$2*
ONE TWO
THREE

-i忽略案例
-x仅匹配整行
默认情况下,grep匹配行中的任何位置
如果没有-x,则需要grep -i '^[^t]*t[^t]*$'
[^t]*t之外的任何字符(由于-i选项,T也将不匹配)
您也可以在这里使用awk
$ awk -F'[tT]' 'NF==2' ip.txt
nopqrstuvwxyz
The price is *$2*
ONE TWO
THREE

-F'[tT]'指定tT作为字段分隔符
NF==2如果行包含两个字段,即该行是否有一个tT

关于linux - egrep打印一条只有一吨的打印线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53625287/

10-14 19:53