10-07-04 12:49 PM
2004-10-7, 03:13(-07), Darren Stribling:
[...]
> Have the following text:
>
>|x|x|y|y|...|23|<cr>
>
>|x|x|y|y|...|23|<cr>
> .
> .
> .
> EOF
>
> Where x,y can be any char / interger or empty.
>
> The problem I have is the file is an unload from an SQL database, and
> I want to replace, for example, '23' by '01'.
>
> The position of '23' is at delimiter position 12, say. So I have to
> count the delimiters, until i get to position 12, then replace it with
> '01'. I then have to go to the next record (after the carriage return)
> and increment this by 1.
I would bet that your records are line feed separated rather
than carriage return separated (\n is the default line
terminator on Unix, \r is the line terminator on Mac, \r\n is
the line terminator on MS systems generally).
>
> i.e.
>
>|x|x|y|y|...|01|<cr>
>
>|x|x|y|y|...|02|<cr>
>
>|x|x|y|y|...|03|<cr>
>
>|x|x|y|y|...|04|<cr>
>
> .
> .
> .
> EOF
>
> Is there a way to automate this as it has been driving me insane!
[...]
Maybe:
nawk '-F|' -v 'OFS=|' '$12 == 23, 0 {
$12 = sprintf("%2d", n++)} 1' < your-file
Note that in
|a|b|c|
c is in the 4th field ("" is in the first field), so you may
have to change $12 with $13.
--
Stephane
[ Post a follow-up to this message ]
|