Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split string on non alphanumeric characters using awk

Tags:

regex

bash

unix

awk

I'm trying to get the last substring before a non alphanumeric character. For eg.

1) beta gamma foo

2) beta gamma%foo

3) beta gamma|---foo

In all of the above examples I'm trying to get "foo" as it is the last substring after splitting on non-alphanumeric characters

So far I've tried

cat file* | awk -F'[^:alnum:]' '{print $NF;}' | less

but this doesn't give the expected results

like image 909
rockstarjindal Avatar asked Sep 15 '26 18:09

rockstarjindal


1 Answers

Your POSIX regex is incorrect. Use this awk command:

awk -F '[^[:alnum:]]' '{print $NF}' file
foo
foo
foo
like image 110
anubhava Avatar answered Sep 19 '26 06:09

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!