Perl - replace repeating numbers with 0


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl - replace repeating numbers with 0
# 1  
Old 09-12-2009
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  
Old 09-13-2009
Quote:
Originally Posted by xchen89x
...
...
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  
Old 09-13-2009
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  
Old 09-13-2009
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  
Old 09-13-2009
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!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed to replace / between the two numbers

i Have a file as following view pz19a0c0/1000T_J_3MoDw9DSLh1ZsCubdua-LKOQmbtiVgkIsiMbSiwF467?sessionId=15451401994597121249 view pz19a0c0/100086X67pR0MwzWnhhSO6sAEoxeFMyhh-IIbUCCdxicaQM4FC9?sessionId=154514019945971212494898 view/cart ... (5 Replies)
Discussion started by: Raghuram717
5 Replies

2. UNIX for Dummies Questions & Answers

How can I replace the lines that start with a star and replace it with numbers start from 1?

I need to replace the (*) in the fist of a list with numbers using sed for example > this file contain a list * linux * computers * labs * questions to >>>> this file contain a list 1. linux 2. computers 3. labs 4. questions (7 Replies)
Discussion started by: aalbazie
7 Replies

3. Shell Programming and Scripting

Sed replace using same pattern repeating multiple times in a line

Sed replace using same pattern repeating multiple times in a line I have text like below in a file: I am trying to replace the above line to following How can I acheive this? I am able to do it if the occurrence is for 1 time: But If I try like below I am getting like this: I have to... (4 Replies)
Discussion started by: sol_nov
4 Replies

4. Shell Programming and Scripting

replace numbers in records

hello every one I have file with following records begin ASX120016719 ASX190006729 ASX153406729 ASX190406759 ASX180006739 end for each record there is ASX word then 9 digits after it (NO spaces included) what i want is to : 1- skip ASX 2-skip first 2 digits after ASX word... (16 Replies)
Discussion started by: neemoze
16 Replies

5. Shell Programming and Scripting

How to replace multiple numbers?

hello everyone i searched the net and i could not find script for this request. i believe sed command will do it but i'm not sure about how. my file contains thousands of records, the following is sample: BEGIN ASX15001 BEGIN ASX15000000500020101230 ASX18001020070002010123... (10 Replies)
Discussion started by: neemoze
10 Replies

6. Shell Programming and Scripting

Sed Replace repeating pattern

Hi, I have an sqlplus output file using the character ';' as a delimiter and I would like to replace the fields without datas (i.e delimited by ';;') by ';0;' Example: my sqlplus output: 11;22;33;44;;;77;; What I would like to have: 11;22;33;44;0;0;77;0; Thanks in advance for your... (2 Replies)
Discussion started by: popesk
2 Replies

7. UNIX for Dummies Questions & Answers

Replace US numbers with European numbers

hey, I have a file with numbers in US notation (1,000,000.00) as well as european notation (1.000.000,00) i want all the numbers to be in european notation. the numbers are in a text file, so to prevent that the regex also changes the commas in a sentence/text i thought of: sed 's/,/\./'... (2 Replies)
Discussion started by: FOBoy
2 Replies

8. Shell Programming and Scripting

Script to replace numbers by string

Hi! I need the following script: - All numbers in a filename (0-9) have to be replace by a String ("Zero"-"Nine") - The script has to go through all the files in the current directory and has to replace the numbers as described above... I have no idea how to do this... Thanks! Michael (5 Replies)
Discussion started by: Michi21609
5 Replies

9. Shell Programming and Scripting

Replace a random string of numbers

Hi Can someone help me with this one? I have string.. (PROC_PROC_ID == 12183) <--PID is dynamic and i want to replace the PID number with whatever PID from /opt/hpws/apache32_2/logs/httpd.pid file. i'm having problem since the PID on the string is dynamic. It may be 2-5 digits or more. ... (5 Replies)
Discussion started by: ryandegreat25
5 Replies

10. Shell Programming and Scripting

Sed Replace a repeating character

I have a text file and every line ends in |^ |^^ |^^^ |^^^^ I need to use sed to make all lines end it |^ regardless of the amount of carrots. The code i was using is: cat FILE | sed 's/\^\^\^/\^/g' But then they threw that curveball at me. Also is there a way to... (2 Replies)
Discussion started by: insania
2 Replies
Login or Register to Ask a Question