Trying to extract the first 5 characters (only number and alphabet) from a string in bash

I have a string like

1-a-BC-dxyz

I want to get 1-a-bc-d(first 5 characters, only number and alphabet)

thank

+4
source share
6 answers

With gawk:

   awk '{ for ( i=1;i<=length($0);i++) { if ( match(substr($0,i,1),/[[:alnum:]]/)) { cnt++;if ( cnt==5) { print substr($0,1,i) } } } }' <<< "1-a-bc-dxyz"

Read each character one at a time, and then if there is a pattern match for an alphanumeric character (using the match function), increase the cnt variable. When cnt gets the value 5, print the line we have seen so far (using the substr function)

Conclusion:

 1-a-bc-d
+4
source
a='1-a-bc-dxyz'
count=0
for ((i=0;i<${#a};i++)); do
    if [[ "${a:$i:1}" =~ [0-9]|[a-Z] ]] && [[ $((++count)) -eq 5 ]]; then
        echo "${a:0:$((i+1))}"
        exit
    fi  
done

You can also reduce this as:

a='1-a-bc-dxyz'
count=0
for ((i=0;i<${#a};i++)); do [[ "${a:$i:1}" =~ [0-9]|[a-Z] ]] && [[ $((++count)) -eq 5 ]] && echo "${a:0:$((i+1))}"; done
+1
source

GNU awk:

$ echo 1-a-bc-dxyz | \
awk -F '' '{b=i="";while(gsub(/[0-9a-z]/,"&",b)<5)b=b $(++i);print b}' 
1-a-bc-d

:

awk -F '' '{                        # separate each char to its own field
    b=i=""                          # if you have more than one record to process
    while(gsub(/[0-9a-z]/,"&",b)<5) # using gsub for counting (adjust regex if needed)
        b=b $(++i)                  # gather buffer
    print b                         # print buffer
}'
+1

GNU sed k- .

echo "1-a-bc-dxyz" | sed 's/[^a-zA-Z0-9]*[a-zA-Z0-9]//g6'
+1

sed AWK

echo 1-a-bc-dxyz | sed 's/[-*%$#@]//g' | awk -F '' {'print $1$2$3$4$5'}

.

0
echo '1-a-bc-dxyz' | grep -Eo '^[[:print:]](-*[[:print:]]){4}'

It is pretty simple. Neither sed nor awk.

0
source

Source: https://habr.com/ru/post/1682724/


All Articles