通过在一个字符串的每个字符Bash-循环来检查特定模式 [英] Bash- loop through each character in a string to check for specific pattern
问题描述
所以,我需要检查每一个角色在与字符串的输入 - 看它是否匹配/不匹配的模式。
So I need to check each character in a string- the input- to see if it matches/doesn't match a pattern.
在codelish:
我猜我将不得不使用一个循环?
I'm guessing I'll have to use a for loop?
在for循环中会有 -
inside for loop there would be-
for each character in string
do (the following)
if it doesn't contain one/more number
echo it doesn't contain contain one/more number
fi
[then other conditions]
done.
所以,我想要做的是分析串并显示错误味精每个字符(每个条件)作为输出。
So what I'm trying to do is analyze each character in string and display error msg (for each condition) as output.
任何帮助将大大AP preciated。
Any help would be greatly appreciated.
推荐答案
正如我前面所说,成炭遍历字符串char是一般的不的一个非常有效的策略。但这里的一个办法做到这一点,用awk。下面的程序决定了输入文件的每行的字符类每个字符。请参阅AWK手册的的确切定义字符类的。
As I said above, looping over a string char by char is generally not a very efficient strategy. But here's one way to do that, using awk. The program below determines the character class of each character in each line of the input file. Please see the awk manual for the exact definitions of the character classes.
CharTypes0.awk
#!/usr/bin/awk -f
# Print the character class of each character in each input line
# Written by PM 2Ring 2014.10.02
BEGIN{numtypes = split("lower upper digit punct blank", types); FS = ""}
{
for(i=1; i<=NF; i++)
for (j=1; j<=numtypes; j++)
{
type = types[j]
if ($i ~ "[[:" type ":]]")
{
printf "'%s': %s\n", $i, type
break
}
}
}
您可以运行这个程序是这样的:
You can run this program like this:
echo 'This is A $24 @test.' | awk -f CharTypes0.awk
输出
'T': upper
'h': lower
'i': lower
's': lower
' ': blank
'i': lower
's': lower
' ': blank
'A': upper
' ': blank
'$': punct
'2': digit
'4': digit
' ': blank
'@': punct
't': lower
'e': lower
's': lower
't': lower
'.': punct
或者你可以得到它命名在命令行上PROCESSS的一个或多个文本文件中的所有行,例如:
Or you can get it to processs all the lines of one or more text files by naming them on the command line, eg:
awk -f CharTypes0.awk test1.txt test2.txt
......
.....
该计划的一个,而更有效的版本,可以很容易地计算每个类型的字符数在整个单词或线一次,而不是遍历每个字符。
A rather more efficient version of this program could easily count the number of characters of each type in a whole word or line at once, rather than looping over each character.
修改
例如,
CountCharTypes0.awk
#!/usr/bin/awk -f
# Count number of characters in each input line that match various classes
# Written by PM 2Ring 2014.10.01
BEGIN{numclasses = split("lower upper alpha digit alnum punct blank", classes)}
{
printf "\nData:[%s] Length: %d\n", $0, length($0)
for (i=1; i<=numclasses; i++)
{
class = classes[i]
printf "%s %2d\n", class, gsub("[[:" class ":]]", "&")
}
}
CharTypes0.awk
CharTypes0.awk
编辑2
下面是CharTypes的纯bash的版本:
Here's a pure bash version of CharTypes:
#!/bin/bash
# Print the character class of each character in $1
# Written by PM 2Ring 2014.10.03
chartypes()
{
types=(lower upper digit punct blank)
string=$1
for((i=0; i<${#string}; i++))
do
ch=${string:i:1}
for t in ${types[@]}
do
[[ $ch =~ [[:${t}:]] ]] &&
{ echo "[$ch] $t"; break; }
done
done
}
chartypes 'This is A $24 @test.'
这篇关于通过在一个字符串的每个字符Bash-循环来检查特定模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!