i have this variable:
varT="1--2--3--5"
i want to use awk to print field 3 from this variable. i dont want to do the "echo $varT".
but here's my awk code:
awk -v valA="$varT" "BEGIN {print valA}"
this prints the entire line. i feel like i'm so close to getting what i want. i basically just want to fork another awk process or any other command.
i was thinking this would work, but it didn't:
awk -v valA="$varT" "BEGIN {print valA,$3}"
If you want to do it that way you could try:
awk -v valA="$varT" 'BEGIN{split(valA,F,/--/);print F[3]}'
1 Like
this worked perfectly. can i print the last field using this command? i tried:
awk -v valA="$varT" 'BEGIN{split(valA,F,/--/);print F[NF]}'
but i didn't get anything.
Try:
awk -v valA="$varT" 'BEGIN {n=split(valA,F,/--/); print F[n]}'
1 Like
Yoda
5
AWK split function returns the number of elements created:
awk -v valA="$varT" 'BEGIN{n=split(valA,F,/--/);print F[n]}'
1 Like