You are not logged in.

#1 2012-05-23 05:29:05

pshevtsov
Member
From: Novosibirsk, Russia
Registered: 2011-01-20
Posts: 52

[SOLVED][bash] read and tab character

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

#2 2012-05-23 05:43:15

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: [SOLVED][bash] read and tab character

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

#3 2012-05-23 07:10:02

pshevtsov
Member
From: Novosibirsk, Russia
Registered: 2011-01-20
Posts: 52

Re: [SOLVED][bash] read and tab character

/dev/zero wrote:

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

#4 2012-05-23 07:18:52

/dev/zero
Member
From: Melbourne, Australia
Registered: 2011-10-20
Posts: 1,247

Re: [SOLVED][bash] read and tab character

pshevtsov wrote:

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

#5 2012-05-23 08:50:48

portix
Member
Registered: 2009-01-13
Posts: 757

Re: [SOLVED][bash] read and tab character

pshevtsov wrote:

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

#6 2012-05-23 09:41:31

pshevtsov
Member
From: Novosibirsk, Russia
Registered: 2011-01-20
Posts: 52

Re: [SOLVED][bash] read and tab character

portix wrote:

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

#7 2012-05-24 03:46:48

thisoldman
Member
From: Pittsburgh
Registered: 2009-04-25
Posts: 1,172

Re: [SOLVED][bash] read and tab character

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."

Offline

#8 2012-05-24 03:54:39

pshevtsov
Member
From: Novosibirsk, Russia
Registered: 2011-01-20
Posts: 52

Re: [SOLVED][bash] read and tab character

thisoldman wrote:
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

Board footer

Powered by FluxBB