Unix Shell - Searching and replacing text using a delimeter

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2004 > Searching and replacing text using a delimeter





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 Searching and replacing text using a delimeter
Darren Stribling

2004-10-07, 7:49 am

Hi Group,

have a puzzeling problem.

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.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!

Many Thanks,

Darren
Stephane CHAZELAS

2004-10-07, 7:49 am

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
Dana French

2004-10-07, 11:21 am

darren_stribling@hotmail.com (Darren Stribling) wrote in message news:<f4072b3e.0410070213.1d947ae4@posting.google.com>...
> Hi Group,
>
> have a puzzeling problem.
>
> 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.
>

SNIP

A ksh93 solution:

Assumes the data file is as you represented with the field you want to
replace at the end of the record, followed by a delimiter and end of
line.
Also assumes the replacement number can be represented by a 2 digit
number (meaning 99 or less records in the data file).

#!/usr/bin/ksh93
typeset -Z2 CNT=0
while read -r -- LINE
do
(( ++CNT ))
print -r -- "${LINE/%\|23\|/|${CNT}|}"
done < data.in


--------------------------------------------------------
Dana French dfrench@mtxia.com
Mt Xia Technical Consulting Group http://www.mtxia.com
100% Spam Free Email http://www.ridmail.com
MicroEmacs http://uemacs.tripod.com
Korn Shell Web http://dfrench.tripod.com/kshweb.html
rakesh sharma

2004-10-08, 2:47 am

darren_stribling@hotmail.com (Darren Stribling) wrote in message news:

>
> have a puzzeling problem.
>
> 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.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!
>


perl -F'\|' -pale'$F[11]=$k++;$_=join"|",@F' yourfile
Darren Stribling

2004-10-08, 5:52 pm

Thanks for everyones help on this. Cracked it in the end with:

awk -F'|' -v 'X=1' -v 'OFS=|' -v 'CONVFMT=%.10g' ' {$2 = $2 + X;$3
= $3 + X;print $0;X++} ' p1process.unl > p1process_new.unl

Again, Thank You Everyone.

Darren

sharma__r@hotmail.com (rakesh sharma) wrote in message news:<ed24e4cf.0410072112.34fc0753@posting.google.com>...
> darren_stribling@hotmail.com (Darren Stribling) wrote in message news:
>
>
>
>
>
>
> PERL -F'\|' -pale'$F[11]=$k++;$_=join"|",@F' yourfile

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com