[GRLUG] Sed Limitations

Roberto Villarreal rvillarreal at mktec.com
Fri Dec 8 13:37:35 EST 2006


On Friday 08 December 2006 12:53, Justin Denick wrote:
> I have a file with records of 530 characters (space counts)
> What I want to do is organize the records by inserting a comma.
>
> So if I have a file "DATA" with a record like
>
> 123456justindenick{...}
>
> I want to do somethine like
>
> sed 's/^\(.\{3\}\)\(.\{3\}\)\(.\{6\}\)\(.\{6\}\)/\1,\2,\3,\4,/g' DATA
>
> I get
>
> 123,456,justin,denick,
>
> The problem comes when I need to replace anything after the 9th field.
> Sed cannot (in this circumstance) understand 10
>
> Instead, sed decides to enter the value of the first pattern matched,
> "123", and appends a "0" to it.
> When it gets to 11 it, enters the value of the first pattern matched and
> appends a "1" to it.
>
> So how should I tackle this because there are 81 fields I need to define in
> each record. Each field is defined by a specific amount of chars.

If you are committed to finding the answer in straight sed, I can't help you, 
but awk would solve your problems much easier and cleaner (imho).  Instead of 
the mess with capturing, you can go:

awk 'BEGIN{FIELDWIDTHS="3 3 6 6 .......";OFS=","};{print $1 $2 $3 $4....}' 
DATA

where FIELDWIDTHS is a space-seperated list of the widths of your fields, OFS 
is the output field separator (allowing to use the 'print' syntax above), and 
$1, $2, etc. are the resulting fields (analogous to your backreferences 
above).

HTH,
Roberto


More information about the grlug mailing list