split string with awk code example
Example 1: bash split string with awk
awk '{split($column_to_split,array_name,"delimiter"); print a[index]}'
string_containing_CODE_to_pentagon
echo "string_containing_CODE_to_pentagon" | awk '{split($0,a,"_"); print a[3]}'
--> CODE
cat file.txt
--> code_1 12|ab_cd|34_ef
code_2 ef_56|gh_78_ij
awk '{split($2, a, "|"); split(a[2],b,"_"); print b[2]}' file.txt
--> cd
78
Example 2: awk define string as delimiter
Just use function split in awk command to split a line into an array 'a'
using a choosen string as delimiter as for example ", " in next use case:
echo "hi, bye, hey" | awk '{split($0,a,", "); print a[3],a[2],a[1]}'