我正在尝试打印只有一个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]'
指定t
或T
作为字段分隔符NF==2
如果行包含两个字段,即该行是否有一个t
或T
关于linux - egrep打印一条只有一吨的打印线,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53625287/