Discussion: case
Afficher un message
Vieux 03/11/2006, 11h51   #4
Michal Nazarewicz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: case

donghuang.de@googlemail.com writes:

> hello folks,
>
> I get a problem according to the "case"
>
> i am trying to the parse lots of .txt files into a .csv file.
>
> the txt file looks like this
>
> 2006-05-17 00:01:08 [INFO] "Name: joe"
> 2006-05-17 00:01:08 [INFO] "Sex: m"
> 2006-05-17 00:01:08 [INFO] "Co. Nr.: ece222"
>
> the csv file
> name sex age co. Nr.
> joe m 21 ece222
> may f 20 csi091
> ...
>
> for FILENAME in `find ./ -name *.txt`


That's a very bad way to read file names.

> do
>
> unset tmpline # output buffer
> IFS='
> '


You don't need to modify IFS here.

> while read new_line
> do
> KEY_WORD=`awk '{print $4}' | sed -e s/\"// | sed -e s/\://`
> case "$KEYWORD" in
> "Name" ) echo "hello" ;;
> "Sex" ) echo "sex" ;;
> * ) echo "byebye" ;;
> esac
>
> done < $FILENAME
> done


#v+
COUNTER=0
find . -name '*.txt' -exec cat -- {} \; | while IFS= read value; do
value=${value#*"}
value=${value%"*}
field=${value%%:*}
value=${value#*: }

# now you can do whatever you want with $field and $value

case "$field" in
(Name) NAME=$value ;;
(Sex) SEX=$value ;;
(Co. Nr.)
if [ -n "$NAME" ] && [ -n "$SEX" ]; then
COUNTER=$(( $COUNTER + 1 ))
printf '%-10s %-3s %3s %3d %-6s\n' "$NAME" "$SEX" \
'' $COUNTER "$value"
unset NAME SEX
else
# misformatted
fi
esac
done
unset COUNTER
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
  Réponse avec citation
 
Page generated in 0,05617 seconds with 9 queries