Searching and replacing text using a delimeter
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > Searching and replacing text using a delimeter




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    Searching and replacing text using a delimeter  
Darren Stribling


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-07-04 12:49 PM

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





[ Post a follow-up to this message ]



    Re: Searching and replacing text using a delimeter  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
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 ]



    Re: Searching and replacing text using a delimeter  
Dana French


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-07-04 04:21 PM

darren_stribling@hotmail.com (Darren Stribling) wrote in message news:<f4072b3e.0410070213.1
d947ae4@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





[ Post a follow-up to this message ]



    Re: Searching and replacing text using a delimeter  
rakesh sharma


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-08-04 07: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





[ Post a follow-up to this message ]



    Re: Searching and replacing text using a delimeter  
Darren Stribling


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
10-08-04 10: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@po
sting.google.com>...
> darren_stribling@hotmail.com (Darren Stribling) wrote in message news:
> 
> 
> 
> 
> 
>
>    PERL -F'\|' -pale'$F[11]=$k++;$_=join"|",@F' yourfile





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:23 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register