[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: awk not just using the Field separator as such. it is using the blank space as well ...



 Thank you! I noticed my mistake and yes, once again it was a hack
which I thought to be a typo. I had removed the pipe you had included
in the last part of the input string!: "${_PTH}|"

_PTH="83847547|2|dli.ernet.449320/449320-Seduction Of The Innocent_text.pdf"
IFS="|" read -ra _PTH_AR <<< "${_PTH}|"
_PTH_AR_L=${#_PTH_AR[@]}
echo "// __ \$_PTH_AR_L: |${_PTH_AR_L}|, \"${_PTH}\""
for(( IX=0; IX<${_PTH_AR_L}; ++IX )); do
 echo "// __ [$IX/$_PTH_AR_L): |${_PTH_AR[$IX]}|"
done

// __ $_PTH_AR_L: |3|, "83847547|2|dli.ernet.449320/449320-Seduction
Of The Innocent_text.pdf"
// __ [0/3): |83847547|
// __ [1/3): |2|
// __ [2/3): |dli.ernet.449320/449320-Seduction Of The Innocent_text.pdf|

 With awk I just to do such things like this:

_PTH_AR=($( echo "${_PTH}" | awk -F '|' '{for (i=1; i<=NF; i++) print $i;}' ))
echo "// __ \$_PTH_AR_L: |${_PTH_AR_L}|, \"${_PTH}\""
// __ $_PTH_AR_L: |1|, "83847547|2|dli.ernet.449320/449320-Seduction
Of The Innocent_text.pdf"

 However this would rightly split that line based on the pipe delimiter:

$ echo "${_PTH}" | awk -F '|' '{for (i=1; i<=NF; i++) print $i;}'
83847547
2
dli.ernet.449320/449320-Seduction Of The Innocent_text.pdf
$

 There should be a sane way ;-) to feed those three lines into a bash array.


Reply to: