Dr. Werner Fink wrote:
On Thu, Sep 04, 2008 at 01:10:16AM -0500, David C. Rankin wrote:
Listmates,
I stumbled on a problem trying to read a file (email mailbox) line-by-line in bash. Using the built-in read, it strips the leading whitespace from each line making the subsequent write impossible. I was using a while loop as follows:
{ while read XTAG VALUE LINE; do if [[ ${XTAG} == "X-Mozilla-Status:" ]]; then case ${VALUE} in 1019 ) NEWVAL=1011;; 1009 ) NEWVAL=1001;; 001b ) NEWVAL=0013;; 0019 ) NEWVAL=0011;; 000b ) NEWVAL=0003;; 0009 ) NEWVAL=0001;; * ) NEWVAL=${VALUE};; esac echo -e "${XTAG} ${NEWVAL} ${LINE}" >> ${NEWFILE} else echo -e "${XTAG} ${VALUE} ${LINE}" >> ${NEWFILE} fi XTAG=''; VALUE=''; LINE=''; NEWVAL='' done } < ~/.thunderbird/2k12pnl0.default/Mail/pop.suddenlinkmail.com/openSuSE.sav
All of the lines in the mailbox with leading whitespace were written without the leading whitespace like:
original file:
Received: from edge03.suddenlink.net ([195.135.221.135]) by imta03.suddenlink.net
newfile:
Received: from edge03.suddenlink.net ([195.135.221.135]) by imta03.suddenlink.net
Just use the IFS variable.
while IFS=$'\n' read line ; do if [[ $line =~ ^X-Mozilla-Status: ]] ; then value=${line#*:} value=${value%%[[:blank:]]*} case $value in 1019 ) line=${line/1019/1011} ;; 1009 ) line=${line/1009/1001} ;; 001b ) line=${line/001b/0013} ;; 0019 ) line=${line/0019/0011} ;; 000b ) line=${line/000b/0003} ;; 0009 ) line=${line/0009/0001} ;; esac fi echo "$line" >> ${NEWFILE} done < ~/.thunderbird/2k12pnl0.default/Mail/pop.suddenlinkmail.com/openSuSE.sav
Werner, I have a question. Is the value=${value%%[[:blank:]]*} right? It looks like it is deleting everything in the parameter $value. Testing yields: if [[ $line =~ ^X-Mozilla-Status: ]] ; then echo -e "line = $line" value=${line#*:} echo -e "\tvalue=\${line#*:} = '$value'" value=${value%%[[:blank:]]*} echo -e "\t\${value%%[[:blank:]]*} = '$value'\n" line = X-Mozilla-Status: 0009 value=${line#*:} = ' 0009' ${value%%[[:blank:]]*} = '' Shouldn't it be value=${value##[[:blank:]]} ? Doing this way yields the intended result: line = X-Mozilla-Status: 0009 value=${line#*:} = ' 0009' ${value%%[[:blank:]]*} = '0009' Also, couldn't we eliminate the second value expression altogether by including the [[:blank:]] in the first value expression like this: value=${line#*:[[:blank:]]} which yields: line = X-Mozilla-Status: 0009 value=${line#*:[[:blank:]]} = '0009' Are there some gotchas in there? Just curious. Perhaps it was just slyly meant for additional learning.... -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org