Home > database >  How to check last 3 chars of a string are alphabets or not using awk?
How to check last 3 chars of a string are alphabets or not using awk?

Time:06-17

I want to check if the last 3 letters in column 1 are alphabets and print those rows. What am I doing wrong?

My code :-

awk -F '|' ' {print str=substr( $1 , length($1) - 2) } END{if ($str ~ /^[A-Za-z]/ ) print}' file

cat file

12300USD|0392
abc56eur|97834
238aed|23911
aabccde|38731
73716yen|19287
.*/|982376
0NRT0|928731

expected output :

12300USD|0392
abc56eur|97834
238aed|23911
aabccxx|38731
73716yen|19287

CodePudding user response:

$ awk -F'|' '$1 ~ /[[:alpha:]]{3}$/' file
12300USD|0392
abc56eur|97834
238aed|23911
aabccde|38731
73716yen|19287

Regarding what's wrong with your script:

  • You're doing the test for alphabetic characters in the END section for the final line read instead of once per input line.
  • You're trying to use shell variable syntax $str instead of awk str.
  • You're testing for literal character ranges in the bracket expression instead of using a character class so YMMV on which characters that includes depending on your locale.
  • You're testing for a string that starts with a letter instead of a string that ends with 3 letters.

CodePudding user response:

Use grep:

grep -P '^[^|]*[A-Za-z]{3}[|]' in_file > out_file

Here, GNU grep uses the following option:
-P : Use Perl regexes.

The regex means this:
^ : Start of the string.
[^|]* : Any non-pipe character, repeated 0 or more times.
[A-Za-z]{3} : 3 letters.
[|] : Literal pipe.

  • Related