The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Sed Replace a repeating character insania Shell Programming and Scripting 2 06-23-2009 05:30 PM
Random Numbers - Perl repinementer Shell Programming and Scripting 3 03-26-2009 04:19 AM
How to replace many numbers with one number in a file vpandey AIX 2 02-27-2008 08:43 AM
to replace one character by numbers in a file cdfd123 Shell Programming and Scripting 7 10-07-2007 10:25 PM
Replace Perl Module name in all Perl scripts rahulrathod Shell Programming and Scripting 2 12-02-2005 01:00 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-12-2009
xchen89x xchen89x is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 7
Perl - replace repeating numbers with 0

I have a text file that looks like this:

Line1) 2000001 12 34 42.5 122 204 2000001 -2000001 15
Line2) 2000001 14 2000001 38.3 2000001 88 2000001
Line3) 45 2000001 446 2000001 88 2000001
Line4) 2000001 2000001 65 883 2000001 34 2000001 5000 2000001
.
.
.

What I want to do is to scan each line of the file and if there are more than 3 repeats of 2000001 on that specific line (ex. Line2 and Line4), I want to leave that line alone. If there are less than 3 (Line1 and Line3) then I want to replace all the 2000001 values with one 0.

I was wondering if someone could help me with this problem using Perl or at least point me in the right direction. Thanks in advanced!
  #2 (permalink)  
Old 09-12-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 517
Quote:
Originally Posted by xchen89x View Post
...
...
if there are more than 3 repeats of 2000001 on that specific line (ex. Line2 and Line4), I want to leave that line alone.

If there are less than 3 (Line1 and Line3) then I want to replace all the 2000001 values with one 0.
...
And what do you want to do if there are exactly 3 occurrences of "2000001" ?

Quote:
...
If there are less than 3 (Line1 and Line3) then I want to replace all the 2000001 values with one 0.
...
Do you mean all or each ?
That is, for line no. 1 which has 2 occurrences (less than 3):

Code:
Line1) 2000001 12 34 42.5 122 204 2000001 -2000001 15
do you want each occurence to be replaced by a 0 ?

Code:
Line1) 0 12 34 42.5 122 204 0 -2000001 15
If you really mean all, then where should the 0 be placed (since the two occurrences of 2000001 are not consecutive) ?

tyler_durden
  #3 (permalink)  
Old 09-12-2009
xchen89x xchen89x is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 7
If there are 3 occurrences, then I want all 3 of them to be replaced by one 0 and preferably have the 0 at the beginning of the line. If you cannot replace them with a 0, it is also okay to completely remove those values (replace them with nothing).
  #4 (permalink)  
Old 09-12-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 517
Code:
$ 
$ cat f1
Line1) 2000001 12 34 42.5 122 204 2000001 -2000001 15
Line2) 2000001 14 2000001 38.3 2000001 88 2000001
Line3) 45 2000001 446 2000001 88 2000001
Line4) 2000001 2000001 65 883 2000001 34 2000001 5000 2000001
$ 
$ 
$ ##
$ perl -lne 'chomp; while(/ 2000001( |$)/g){$i++};
>            if ($i<=3) {s/ 2000001( |$)/$1/g; printf("%d ",0)} print; $i=0' f1
0 Line1) 12 34 42.5 122 204 -2000001 15
Line2) 2000001 14 2000001 38.3 2000001 88 2000001
0 Line3) 45 446 88
Line4) 2000001 2000001 65 883 2000001 34 2000001 5000 2000001
$ 
$
tyler_durden
  #5 (permalink)  
Old 09-13-2009
xchen89x xchen89x is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 7
thanks a lot!

---------- Post updated at 12:41 AM ---------- Previous update was at 12:27 AM ----------

My text file contains hundreds of those kinds of lines, I feel like doing it as you've shown is quite difficult. Is there a way to accommodate this new requirement?

---------- Post updated at 12:53 AM ---------- Previous update was at 12:41 AM ----------

Sorry, I misinterpreted your code, it's fine.

Thanks again!

---------- Post updated at 09:56 PM ---------- Previous update was at 12:53 AM ----------

Could you help me create a perl script for this command? I have many files of this type, each with hundreds of lines and it's quite difficult to run it on the command line. I would really appreciate it if you could help me put it into a script. I would also like to be able to create an output file to store the results. Thanks again!
Sponsored Links
Reply

Bookmarks

Tags
perl, replace, search

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0