You are not logged in.
Hello archers!
I'm trying to write simple script for mysql query results parsing. And I got that `read` command "eats" tab-characters:
$ echo -e "foo\tbar\nbaz\tquuix\nfin" | while read L; do echo -e $L; done
foo bar
baz quuix
fin
i.e. tab characters were replaced with spaces.
So, I can't perform the following:
mysql -e "SELECT title,summary,body,added,modified FROM news" \
| while IFS=\t read TITLE SUMMARY BODY ADDED MODIFIED
do
echo $TITLE
done
How can I prevent such behavior?
Thanks!
Last edited by pshevtsov (2012-05-23 09:42:13)
Offline
I'm not a mysql expert, so maybe I'm missing something. Still, wouldn't it work to pipe the results through awk instead of a while loop?
mysql -e "SELECT title,summary,body,added,modified FROM news" \
| awk -F"\t" '{print $1}'
... or the like?
Offline
I'm not a mysql expert, so maybe I'm missing something. Still, wouldn't it work to pipe the results through awk instead of a while loop?
mysql -e "SELECT title,summary,body,added,modified FROM news" \ | awk -F"\t" '{print $1}'
... or the like?
It won't work if query result is more that one line.
Offline
It won't work if query result is more that one line.
To convince me this is true, could you give some example output from the mysql statement, and an example of how you want the output to look after it's been filtered?
Offline
So, I can't perform the following:
mysql -e "SELECT title,summary,body,added,modified FROM news" \ | while IFS=\t read TITLE SUMMARY BODY ADDED MODIFIED do echo $TITLE done
How can I prevent such behavior?
Thanks!
If you want to use \t as IFS you have to quote it
mysql -e "SELECT title,summary,body,added,modified FROM news" \
| while IFS=$'\t' read TITLE SUMMARY BODY ADDED MODIFIED
do
echo $TITLE
done
Offline
If you want to use \t as IFS you have to quote it
mysql -e "SELECT title,summary,body,added,modified FROM news" \ | while IFS=$'\t' read TITLE SUMMARY BODY ADDED MODIFIED do echo $TITLE done
Thanks! It seems to work!
BTW, why it is written in this way -- $'\t'? Where can I get more information on this topic?
Offline
BTW, why it is written in this way -- $'\t'? Where can I get more information on this topic?
http://mywiki.wooledge.org/Quotes Third bullet under "Types of quoting."
Offline
pshevtsov wrote:BTW, why it is written in this way -- $'\t'? Where can I get more information on this topic?
http://mywiki.wooledge.org/Quotes Third bullet under "Types of quoting."
Thanks!
Offline