![]() |
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 |
| Perl script to search a line and copy it to another line | ammu | Shell Programming and Scripting | 3 | 12-29-2008 05:12 PM |
| [Perl] Accessing array elements within a sed command in Perl script | userix | Shell Programming and Scripting | 2 | 10-03-2008 12:05 PM |
| One line perl command | rsg00usa | Shell Programming and Scripting | 2 | 12-14-2005 05:45 PM |
| perl on the command line.. | moxxx68 | Shell Programming and Scripting | 2 | 03-30-2005 01:31 PM |
| Perl script - changing passwords | thehoghunter | Shell Programming and Scripting | 3 | 05-03-2002 10:11 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
changing from command line to perl script
I had posted previously about this problem I had.
I have multiple text files with hundreds of lines of the following type: 2000001 34 54 234 2000001 32 545 2000001 -2000001 77 2000001 44 2000001 998 2000001 77 32 2000001 45 23 111 89 98 75 23 34 999 . . . etc... What I wanted was for each line, if 2000001 appears 3 or less times (<= 3) then I wanted to replace all instances of this value with a 0 at the beginning of the line. If there were more than 3 of these values, then I would not do anything to that line (keep all instances as they were). Previously, someone gave me this perl command to be used on the command line: 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 $ $ --------- However, with multiple files and hundreds of lines on each file of this sort, it is quite difficult to continually executing this on the command line. So I would prefer a perl script where I can change a couple variables and be able to run it over and over again. What I want is to basically open one file, read in one line at a time and if there are 3 or less occurrences of 2000001 on that line, then replace ALL of them with a 0 at the beginning. Do this for all the lines in the file and then output the results into a new file (results.txt for example). I am quite new to Perl and anything I have tried has not succeeded, so I am hoping for some help or at least a point in the right direction. Thanks for all your help in advanced! |
|
||||
|
one way:
Assuming you want to keep perl just embed the command in a script: Code:
perl_it()
{
perl -lne 'chomp; while(/ 2000001( |$)/g){$i++};
> if ($i<=3) {s/ 2000001( |$)/$1/g; printf("%d ",0)} print; $i=0' $1
}
for fname in /path/to/files/*
do
perl_it $fname > tmp
mv tmp $fname
done
|
|
||||
|
Is it possible to not embed the command in a script?
I want a perl script (using perl notation) that does the exact same thing. Thanks |
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Tags |
| file, perl, replace, script, search |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|