Bash- loop through each character in a string to check for specific pattern

China☆狼群 提交于 2019-12-08 13:41:26

问题


So I need to check each character in a string- the input- to see if it matches/doesn't match a pattern.

In Codelish:

I'm guessing I'll have to use a for loop?

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.

Any help would be greatly appreciated.


回答1:


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

Output

'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

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.


Edit

For example,

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


Edit 2

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.'


来源:https://stackoverflow.com/questions/26075341/bash-loop-through-each-character-in-a-string-to-check-for-specific-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!