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

Add field delimitor at one point ?

0 views
Skip to first unread message

Bordon

unread,
Sep 26, 2005, 1:20:14 PM9/26/05
to
Im not that familiar with awk. But I have a problem that I know awk is the
perfect program to solve.

I have a text database that Im trying to split in the middle of a field.

The delimiter is | pipe.

On the 56th field I want to split the data at the 255 character by adding |
the field delimiter. In other words I want to add a field delimiter to the
56 field between the 255 and 256 letters. It must be forgiving in that if
there is no character at the 255 letter it still must add the delimiter.

Can anyone give me the script for this.

Thanks.


John Savage

unread,
Sep 27, 2005, 7:41:48 PM9/27/05
to
"Bordon" <bor...@123.com> writes:
>I have a text database that Im trying to split in the middle of a field.
>
>The delimiter is | pipe.
>
>On the 56th field I want to split the data at the 255 character by adding |
>the field delimiter. In other words I want to add a field delimiter to the
>56 field between the 255 and 256 letters. It must be forgiving in that if
>there is no character at the 255 letter it still must add the delimiter.
>
>Can anyone give me the script for this.

Okay, here's a script to insert a | after the 3rd character in the 5th field.
Test it first, if it works then make the changes you need.

$cat data
one | two | three|four | five | six
une |deux|trois| quatre |cinq | six |sept | huit
1| 2|3| 4|5| 6| 7 | 8 | 9|10

$gawk306 --re-interval 'BEGIN{OFS=FS="|"} {sub(/.{3}/,"&|",$5)} {print}' data
one | two | three|four | fi|ve | six
une |deux|trois| quatre |cin|q | six |sept | huit
1| 2|3| 4|5| 6| 7 | 8 | 9|10

I'm not sure what "forgiving" means. If $56 has only 10 characters, are
you wanting the script to add 245 extra spaces there so that it does now
have 255 or what? But whatever you want, the above code doesn't do it.
Perhaps the third record below illustrates what you want:-

$gawk306 --re-interval 'BEGIN{OFS=FS="|"} !sub(/.{3}/,"&|",$5) {sub(/$/,
"&|",$5)} {print} ' data
one | two | three|four | fi|ve | six
une |deux|trois| quatre |cin|q | six |sept | huit
1| 2|3| 4|5|| 6| 7 | 8 | 9|10

The --re-interval argument is needed for my oldish version to understand
the /.{3}/ regex. Note: you may need to use \x7c instead of the literal
pipe character if trying to do this on DOS.

The group comp.lang.awk is the active awk group.
--
John Savage (my news address is not valid for email)

Bordon

unread,
Sep 28, 2005, 10:04:02 AM9/28/05
to

"John Savage" <rook...@suburbian.com.au> wrote in message
news:050928000093609.28Sep05$rook...@suburbian.com...

Thanks John, I appreciate the help.
As to your question;


> I'm not sure what "forgiving" means. If $56 has only 10 characters, are
> you wanting the script to add 245 extra spaces there so that it does now
> have 255 or what? But whatever you want, the above code doesn't do it.
> Perhaps the third record below illustrates what you want:-

No I don't need 245 extra spaces added but simply the | field delimiter at
the end 0-255 characters in the $56th field.
I believe that your last example is what I need thank you very much.

Regards


0 new messages