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 but this could also be done with sed. Btw: To make sed to use POSIX.2 regular expressions simply use the option -r as the first option of sed and you're fine. sed -r -e '/^X-Mozilla-Status:/{ s/1019/1011/ s/1009/1001/ s/001b/0013/ s/0019/0011/ s/000b/0003/ s/0009/0001/ }' < ~/.thunderbird/2k12pnl0.default/Mail/pop.suddenlinkmail.com/openSuSE.sav > ${NEWFILE} Werner -- "Having a smoking section in a restaurant is like having a peeing section in a swimming pool." -- Edward Burr -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org