BASH question
Walter Dnes
waltdnes-SLHPyeZ9y/tg9hUCZPvPmw at public.gmane.org
Sun Dec 16 01:41:12 UTC 2007
Is your inbox fixed, my offline email to you is rejected.
On Sat, Dec 15, 2007 at 06:58:40AM -0500, Madison Kelly wrote
> David C. Chipman wrote:
>> Hi Madi, Could you do this:
>> -- Start code ---
>> for QRY_LINE in $(<some-mysql-cli-command>)
>> do
>> ROW_NUM=$(echo $QRY_LINE | awk '{ print $1 }")
>> VAR1=$(echo $QRY_LINE | awk '{ print $2 }')
>> VAR2=$(echo $QRY_LINE | awk '{ print $3 }')
>> VAR2=$(echo $QRY_LINE | awk '{ print $4 }')
>> DATE1=$(echo $QRY_LINE | awk '{ print $5 }')
>> TIME=$(echo $QRY_LINE | awk '{ print $6 }')
>> DATE2=$(echo $QRY_LINE | awk '{ print $7 }')
>> done
>> -- End code --
> Hi David,
>
> The problem I had when ever I tried to do that was ${QRY_LINE} would be
> the results split on any white space, so every word was a line, regardless
> of whether it was space, tab or newline seperated... a lot of the (real)
> data contained one of more words... I need something that would populate
> the array by splitting on newline and then split the containing data into
> variables on 'tab'.
This is default bash behaviour. If you want entire lines, use the
built-in shell variable REPLY, which picks up input by line. Try
something like...
./test.sh | ./parse_script
where parse_script is something like
#!/bin/bash
linecounter=0
while read
do
linecounter= $(( ${linecounter} + 1 ))
dataline=${REPLY}
echo "Line number ${linecounter} is... ${dataline}"
done
You can then proceed to pick apart the contents of dataline.
Excerpt from "man bash"...
read [-ers] [-u fd] [-t timeout] [-a aname] [-p prompt] [-n nchars] [-d
delim] [name ...]
One line is read from the standard input, or from the file
descriptor fd supplied as an argument to the -u option, and the
first word is assigned to the first name, the second word to the
second name, and so on, with leftover words and their interven-
ing separators assigned to the last name. If there are fewer
words read from the input stream than names, the remaining names
are assigned empty values. The characters in IFS are used to
split the line into words. The backslash character (\) may be
used to remove any special meaning for the next character read
and for line continuation. Options, if supplied, have the fol-
lowing meanings:
[...snip detailed explanation of options...]
If no names are supplied, the line read is assigned to the vari-
able REPLY. The return code is zero, unless end-of-file is
encountered, read times out, or an invalid file descriptor is
supplied as the argument to -u.
--
Walter Dnes <waltdnes-SLHPyeZ9y/tg9hUCZPvPmw at public.gmane.org>
I'm not repeating myself
I'm an X Window user... I'm an ex-Windows-user
--
The Toronto Linux Users Group. Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists
More information about the Legacy
mailing list