Hi
I need to check if field separator I am using in awk statement is " : ", for example:
TIME=12:59
HOUR=`echo "$TIME" | awk '{FS=":"; print $1}'`
MINUTES=`echo "$TIME" | awk '{FS=":"; print $2}'`
Is there a way to check within the above awk statement ?
If you need split one string to the fields, the awk is huge tool for that. You can use builtin properties (ksh93, bash, ...).
oifs="$IFS" # save input field delemiter
IFS=":" # set input fld delim.
flds=($TIME) # parse input the array
fld1=${flds[0]} # first field index is 0
numberOfFlds=${#flds[@]}
for var in fld1 numberOfFlds
do
eval echo $var = \$$var
done
Or using read, this version works also with dash, ksh88, sh, ...
TIME=12:44
IFS=":" read fld1 fld2 xstr <<EOF
$TIME
EOF
for var in fld1 fld2
do
eval echo $var = \$$var
done