PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Forums Hébergement > Forum Serveur - Sécurité et techniques > comp.unix.shell > "exploding" a line
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
comp.unix.shell Using and programming the Unix shell.

"exploding" a line

Réponse
 
LinkBack Outils de la discussion
Vieux 17/07/2007, 16h41   #1
Ben
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut "exploding" a line

In shell, e.g. ksh,
I have input data of the form

a b c,x,c,d,
b c x,y

I'd need to process the 3rd column, how do I "explode" line by line?

  Réponse avec citation
Vieux 17/07/2007, 16h57   #2
Chris F.A. Johnson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "exploding" a line

On 2007-07-17, Ben wrote:
> In shell, e.g. ksh,
> I have input data of the form
>
> a b c,x,c,d,
> b c x,y
>
> I'd need to process the 3rd column, how do I "explode" line by line?


while read -r field1 field2 field3
do
: do whatever with "$field3"
done < FILENAME

If it is a large file, it may be more efficient to use sed or awk
rather than a shell loop.

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
  Réponse avec citation
Vieux 17/07/2007, 16h58   #3
Janis Papanagnou
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "exploding" a line

Ben wrote:
> In shell, e.g. ksh,
> I have input data of the form
>
> a b c,x,c,d,
> b c x,y
>
> I'd need to process the 3rd column, how do I "explode" line by line?
>


You mean "explore" the line?

while read -r dummy dummy field3 ; do : anything with $field3 ; done <input


Janis
  Réponse avec citation
Vieux 17/07/2007, 17h18   #4
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "exploding" a line

2007-07-17, 08:41(-07), Ben:
> In shell, e.g. ksh,
> I have input data of the form
>
> a b c,x,c,d,
> b c x,y
>
> I'd need to process the 3rd column, how do I "explode" line by line?


You use a tool that process input as fields. awk comes to mind.

awk '{whatever you want with $3}' < input-data

--
Stéphane
  Réponse avec citation
Vieux 17/07/2007, 17h21   #5
Stephane CHAZELAS
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "exploding" a line

2007-07-17, 11:57(-04), Chris F.A. Johnson:
> On 2007-07-17, Ben wrote:
>> In shell, e.g. ksh,
>> I have input data of the form
>>
>> a b c,x,c,d,
>> b c x,y
>>
>> I'd need to process the 3rd column, how do I "explode" line by line?

>
> while read -r field1 field2 field3


that's
while read -r field1 field2 field3_and_above

use
while read -r field1 field2 field3 rest

if you want field3 only.

> do
> : do whatever with "$field3"


as long as "whatever" doesn't read from its stdin.

> done < FILENAME
>
> If it is a large file, it may be more efficient to use sed or awk
> rather than a shell loop.


That's a very good piece of advice

--
Stéphane
  Réponse avec citation
Vieux 17/07/2007, 21h33   #6
Glenn Jackman
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "exploding" a line

At 2007-07-17 11:41AM, "Ben" wrote:
> In shell, e.g. ksh,
> I have input data of the form
>
> a b c,x,c,d,
> b c x,y
>
> I'd need to process the 3rd column, how do I "explode" line by line?


If, by "explode", you mean "how do I iterate over the comma-delimited
values in the 3rd column?", then use awk:

awk '{
print;
split($3, arr, /,/);
for (x in arr) {
print "line " NR ", value[" x "]=" arr[x]
}
}' inputdata.txt



--
Glenn Jackman
You can only be young once. But you can always be immature." -- Dave Barry
  Réponse avec citation
Vieux 18/07/2007, 22h00   #7
johngnub
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "exploding" a line

On Jul 17, 8:41 am, Ben <chinese.cent...@googlemail.com> wrote:
> In shell, e.g. ksh,
> I have input data of the form
>
> a b c,x,c,d,
> b c x,y
>
> I'd need to process the 3rd column, how do I "explode" line by line?


The 3rd col is the "c" or the 2nd "c", comma is your col delimit? Ok.

My examples are in bash. A few ideas: Try to flip the IFS var, to what
you want.
That is a single quote then comma. Add in a counter to track the col
count, etc.
foo="a b c,e,f,g,1"
echo $foo
a b c,e,f,g,1
IFS=','
for x in $foo
> do
> echo "$x"
> done

a b c
e
f
g
1
Just for fun, once the IFS is changed, try echo $foo,
echo $foo
a b c e f g 1
The commas are gone,

Or Just use cut -d,
echo $foo
a b c,e,f,g,1
echo $foo|cut -d ',' -f3
f

Just for fun to see the IFS var,
echo $IFS|od -c
0000000 \n
0000001
Or on some OS's,
echo $IFS|od -ta
0000000 nl
0000001

2 cents.....

  Réponse avec citation
Vieux 19/07/2007, 09h02   #8
Ben
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "exploding" a line

Thank you. I used to think "explode" is a common term, it's a term for
PHP, maybe not in the UNIX world then.. thanks for all the response.

On Jul 17, 9:33 pm, Glenn Jackman <gle...@ncf.ca> wrote:
> At 2007-07-17 11:41AM, "Ben" wrote:
>
> > In shell, e.g. ksh,
> > I have input data of the form

>
> > a b c,x,c,d,
> > b c x,y

>
> > I'd need to process the 3rd column, how do I "explode" line by line?

>
> If, by "explode", you mean "how do I iterate over the comma-delimited
> values in the 3rd column?", then use awk:
>
> awk '{
> print;
> split($3, arr, /,/);
> for (x in arr) {
> print "line " NR ", value[" x "]=" arr[x]
> }
> }' inputdata.txt
>
> --
> Glenn Jackman
> You can only be young once. But you can always be immature." -- Dave Barry



  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 05h44.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12891 seconds with 16 queries