Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

question on bash for loop

3 views
Skip to first unread message

PigInACage

unread,
Nov 27, 2009, 5:30:47 AM11/27/09
to
Hello all.

I've got a file CSV with 3 column
name,surname,group

if one of the column has got a space like
Davide,Super Dooper,Group
I cannot use it in a for loop

for i in `cat list.csv` ; do echo $i ; done
the result is
Davide,Super
Dooper,Group

how can I have all inone line?
Really thanks.

Jan Gerrit Kootstra

unread,
Nov 27, 2009, 6:06:15 PM11/27/09
to
PigInACage schreef:
PigInACage,


You should look at awk for this, for awk can handle differ column
separators


Kind regards,


Jan Gerrit

Moe Trin

unread,
Nov 27, 2009, 8:24:16 PM11/27/09
to
On Fri, 27 Nov 2009, in the Usenet newsgroup linux.redhat, in article
<1d88929d-1203-4887...@m26g2000yqb.googlegroups.com>,
PigInACage wrote:

NOTE: Posting from groups.google.com (or some web-forums) dramatically
reduces the chance of your post being seen. Find a real news server.

>I've got a file CSV with 3 column
>name,surname,group

Sounds like a homework problem. Clue: comma separated.

>if one of the column has got a space like
>Davide,Super Dooper,Group
>I cannot use it in a for loop

and you didn't read the 'bash' man page - about shell grammar.

The list of words following in is expanded, generating a list of
items. The variable name is set to each element of this list in
turn, and list is executed each time.

There's more to that explanation.

>how can I have all inone line?

What did your instructor suggest? If this isn't a homework problem,
start by reading the bash man page (not the most helpful document),
the HOWTO:

-rw-rw-r-- 1 gferg ldp 31540 Jul 27 2000 Bash-Prog-Intro-HOWTO

and two marvelous documents from the Linux Documentation Project at
http://tldp.org/guides.html

* Bash Guide for Beginners

* Advanced Bash-Scripting Guide

A key to programming is to know what the material you are working with
actually looks like. For your example, I'd probably use 'cut'

[compton ~]$ whatis cut
cut (1) - remove sections from each line of files
[compton ~]$

directly, and not in a loop. 'awk' and 'sed' might also be useful,
but it depends on what you are actually trying to do.

[compton ~]$ whatis awk sed
awk (1) - pattern scanning and processing language
sed (1) - a Stream EDitor
[compton ~]$

Old guy

Reynolds McClatchey

unread,
Dec 1, 2009, 9:19:47 AM12/1/09
to

Check out the environment variable $IFS
(Internal Field Separator) in the bash man.

0 new messages