如何获得仅包含一组字母的行? -重击

问题描述 投票:0回答:4

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

很好的例子:

39568250269955376311912572precondition005426787530581443236416842014020466976603

错误示例:

1895853531360579the3170095290529923mathematici2779805995331496368099837070an1084
linux bash unix awk sed
4个回答
1
投票

您能不能尝试以下操作。

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.

1
投票

sed的正则表达式

sed -n '/^[0-9]*[a-z]\{1,\}[0-9]*$/p'

或带有grep

 grep -x -E '[0-9]*[a-z]+[0-9]*'

或带有awk

 awk '$0 ~ "^[0-9]*[a-z]+[0-9]*$"'

两者的含义相同-仅过滤以零或多个数字开头的行,然后过滤一个或多个小写字母,然后仅以零或多个数字开头的行。


1
投票

用于此类任务的常规工具是grep,例如e:g:

grep -E -x '[^[:alpha:]]*[[:alpha:]]+[^[:alpha:]]*' file

0
投票

将所有数字音译为换行符,然后以字母计数行数:

echo 39568250269955376311912572precondition005426787530581443236416842014020466976603 | tr '[0-9]' '\n'| grep -c [:alpha:]
1

echo  1895853531360579the3170095290529923mathematici2779805995331496368099837070an1084 | tr '[0-9]' '\n'| grep -c [:alpha:]
3
© www.soinside.com 2019 - 2024. All rights reserved.