|
|
|
|
||||||
| comp.unix.shell Using and programming the Unix shell. |
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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? |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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..... |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|