For a field format such as AAL1001_MD82, how do I select(and use in if statement) only the last four elements( in this case MD82) or the first three elements (in this case AAL)?
For instance, how do I do the following - if first three elements of $x == yyy, then ...
If you have bash: ${variable:start:length}
length is optional.
stringZ=abcABC123ABCabc
# 0123456789.....
# 0-based indexing.
echo ${stringZ:0} # abcABC123ABCabc
echo ${stringZ:1} # bcABC123ABCabc
echo ${stringZ:7} # 23ABCabc
echo ${stringZ:7:3} # 23A
# Three characters of substring.
pos=$(( ${#stringZ} - 3 ))
echo ${stringZ:$pos} # last three characters
use cut
[tpntvkc]:tpntvkc> echo abcdefgh | cut -c 1-4
abcd
This does not work while using awk. How do I do this in awk?
Thanks
Try this:
> echo AAL1001_MD82 | /usr/xpg4/bin/awk ' { print substr($0,length($0)-3,4) }'
MD82