Home > Archive > AWK > February 2007 > Howto concatenate variable number of fields in a line
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Howto concatenate variable number of fields in a line
|
|
| john coltrane 2007-02-01, 9:56 pm |
| 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
| |
| Ed Morton 2007-02-01, 9:56 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.
| |
| loki harfagr 2007-02-01, 9:56 pm |
| On Thu, 01 Feb 2007 13:52:47 -0600, Ed Morton wrote:
> john coltrane wrote:
>
> You need to start by saying the word "field" a few more times ;-).
>
>
> ????
>
>
....
> 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
|
|
|
|
|