Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multisplitting in AWK

Tags:

linux

bash

awk

I would like to execute 2 splits using AWK (i have 2 fields separator), the String of data i'm working on would look like something like so:

data;digit&int&string&int&digit;data;digit&int&string&int&digit

As you can see the outer field separator is a semicolon, and the nested one is an ampersand. What i'm doing with awk is (suppose that the String would be in a variable named test)

echo ${test} | awk '{FS=";"} {print $2}' | awk '{FS="&"} {print $3}'

This should catch the "String" word, but for some reason this is not working. It seems like the second pipe its not being applied, as i see only the result of the first awk function Any advice?

like image 568
JBoy Avatar asked Mar 19 '26 13:03

JBoy


2 Answers

use awk arrays

echo $test | awk -F';' '{split($2, arr, "&"); print(arr[3])}'
like image 162
iruvar Avatar answered Mar 21 '26 03:03

iruvar


Try that :

echo "$test" | awk -F'[;&]' '{print $4}'

I specify a multiple separator in -F'[;&]'

like image 41
Gilles Quenot Avatar answered Mar 21 '26 05:03

Gilles Quenot



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!