![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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! |
|
||||
|
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).
|
|
|||||
|
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
$
$
|
|
||||
|
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 | ||
|
|
![]() |
| Bookmarks |
| Tags |
| perl, replace, search |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|