Howto concatenate variable number of fields in a line

  • Follow


I am parsing a comma delimited file where one of the fields has
imbedded commas. I know the field starts at the second field and ends
at the second to last field. I would like to concatenate those fields
into one field and print it.
How can I do this?

I thought something like

{ for( i = 1; i <= NF-2;i++){ $s = $s " " $i}}
this is obviously creates a string with duplicate substrings, this is
bad.
so how can I do this.

An example of what I want follows:

input:

123, one, two, three, four, five, 567, 890

output:

one two three four five

I can't use printf because the number of fields vary.

any help?

thanks

john

0
Reply tendengarci (112) 2/1/2007 7:24:25 PM

john coltrane wrote:
> I am parsing a comma delimited file where one of the fields has
> imbedded commas. I know the field starts at the second field and ends
> at the second to last field. I would like to concatenate those fields
> into one field and print it.
> How can I do this?

You need to start by saying the word "field" a few more times ;-).

> 
> I thought something like
> 
> { for( i = 1; i <= NF-2;i++){ $s = $s " " $i}}
> this is obviously creates a string with duplicate substrings, this is
> bad.

????

> so how can I do this.
> 
> An example of what I want follows:
> 
> input:
> 
> 123, one, two, three, four, five, 567, 890
> 
> output:
> 
> one two three four five

I thought you wanted to go to the second-last field (567), not the 
third-last (five).

> I can't use printf because the number of fields vary.

????

> any help?

I'm really not sure what you want, but maybe it's just:

$ echo "123, one, two, three, four, five, 567, 890" |
awk -F", " '{$1=$NF=$(NF-1)=""}1'
  one two three four five

If not, please clarify and tell us which awk you use. There's various 
ways to get rid of extra spaces if that's a problem.

	Ed.
0
Reply Ed 2/1/2007 7:52:47 PM


On Thu, 01 Feb 2007 13:52:47 -0600, Ed Morton wrote:

> john coltrane wrote:
>> I am parsing a comma delimited file where one of the fields has
>> imbedded commas. I know the field starts at the second field and ends
>> at the second to last field. I would like to concatenate those fields
>> into one field and print it.
>> How can I do this?
> 
> You need to start by saying the word "field" a few more times ;-).
> 
>> 
>> I thought something like
>> 
>> { for( i = 1; i <= NF-2;i++){ $s = $s " " $i}}
>> this is obviously creates a string with duplicate substrings, this is
>> bad.
> 
> ????
> 
>> so how can I do this.
>> 
>> An example of what I want follows:
>> 
>> input:
>> 
>> 123, one, two, three, four, five, 567, 890
>> 
>> output:
>> 
>> one two three four five
> 
....
> I'm really not sure what you want, but maybe it's just:
> 
> $ echo "123, one, two, three, four, five, 567, 890" |
> awk -F", " '{$1=$NF=$(NF-1)=""}1'
>   one two three four five

  Ed. I really like this !-)
  In a short while you've been exposing "different" solutions than you
used to `prone' "afore", I'd guess that's a side effect of the backlash
you may have had from working hard on the variations in
shootfoot-getline thesaurus you wrote recently ;D)
  Beware, your scripts are slightly becoming even more jokeful
than mine used to try to be ;-)

> 
> If not, please clarify and tell us which awk you use. There's various 
> ways to get rid of extra spaces if that's a problem.

  I understood the OP like it seems you did, that's probably bad
news for reality ...-)
  Now I'd just suggest a more classical and less
interesting resoultion than yours but it has some advantages in
ease to read while blind ;D)

$ awk '{for(i=2;i<=NF-2;i++){a=a$i}; print a;a=""}' FS=, 

$ echo "123, one, two, three, four, five, 567, 890
> 123, one, two, three, 567, 890
> 123, one, two, 567, 890
> 123, one, two, three, four, 567, 890
> " |  awk '{for(i=2;i<=NF-2;i++){a=a$i}; print a;a=""}' FS=,
 one two three four five
 one two three
 one two
 one two three four
0
Reply loki 2/1/2007 10:51:25 PM

2 Replies
504 Views

(page loaded in 0.212 seconds)

Similiar Articles:













7/25/2012 10:33:55 AM


Reply: