首页 > 解决方案 > 如何使用 Bash 获取仅包含一组字母的行?

问题描述

我试图找到只包含一组字母的行。该文件仅包含数字和小写字母。没有空间或任何东西。

好例子:

39568250269955376311912572precondition005426787530581443236416842014020466976603

不好的例子:

1895853531360579the3170095290529923mathematici2779805995331496368099837070an1084

标签: linuxbashunixawksed

解决方案


请您尝试以下操作。

awk 'gsub(/[a-z]+/,"&")==1' Input_file

说明:添加上述代码的解释,仅用于解释运行代码的目的,请使用上述代码本身。

awk '                    ##Starting awk program here.
gsub(/[a-z]+/,"&")==1    ##Using gsub function of awk to substitute all small letters occurrences with same values itself.
                         ##Then checking count of it,if it is equal to 1 then print current line.
                         ##awk works on method of condition and action, in above condition is mentioned but NO action so by default print of current line will happen.
' Input_file             ##mentioning Input_file name here, which is being passed to awk program.

推荐阅读