Linux 中grep命令实现在没有匹配时返回特定值

 

001、

(base) [root@PC1 test]# ls
a.txt  idx.txt
(base) [root@PC1 test]# cat a.txt        ## 测试数据
01 02 03 04
05 06a 07 08
09 10 11 12
13 14b 15 16
17 18 19 20
(base) [root@PC1 test]# cat idx.txt
a
c
b
k
(base) [root@PC1 test]# grep "a" a.txt
05 06a 07 08
(base) [root@PC1 test]# grep "c" a.txt                    ## 没有匹配
(base) [root@PC1 test]# grep "c" a.txt || echo "na"       ## 没有匹配c时返回na
na

。 

 

posted @ 2025-01-15 11:54  小鲨鱼2018  阅读(16)  评论(0)    收藏  举报