[GRLUG] Sed Limitations

zdennis zdennis at mktec.com
Fri Dec 8 14:46:01 EST 2006


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Another way to solve this...

- -------------
#!/usr/bin/env ruby

string_count = 3,3,6,6  # ,etc,....
strings = []
position = 0
data = STDIN.read
string_count.each do |c|
  strings << data[position ... position+=c]
end
puts strings.join( ',' )

- ---------------------------

Given your sample data it shows...
  $ cat YOUR_DATA | ruby parse.rb
  123,456,justin,denick

Zach



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.
> 
> Thanks
> 
> 
> 
> 
> -- 
> In vino veritas.
>         [In wine there is truth.]
>                 -- Pliny
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> grlug mailing list
> grlug at grlug.org
> http://shinobu.grlug.org/cgi-bin/mailman/listinfo/grlug

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFecD4Myx0fW1d8G0RAiS/AJ9RyocekocHh+5yG01suT5E0wmbKgCeOqgG
WwWeS7mYfSSDj484v1jpB1M=
=2ANy
-----END PGP SIGNATURE-----


More information about the grlug mailing list