changing from command line to perl script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting changing from command line to perl script
# 1  
Old 09-14-2009
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!
# 2  
Old 09-15-2009
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

# 3  
Old 09-15-2009
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl command line option '-n','-p' and multiple files: can it know a file name of a printed line?

I am looking for help in processing of those options: '-n' or '-p' I understand what they do and how to use them. But, I would like to use them with more than one file (and without any shell-loop; loading the 'perl' once.) I did try it and -n works on 2 files. Question is: - is it possible to... (6 Replies)
Discussion started by: alex_5161
6 Replies

2. Shell Programming and Scripting

Concatenate small line with next line perl script

Hello to all, I'm new to perl, I have input file that contains the string below: 315350535ff450000014534130101ff4500ff45453779ff450ff45545f01ff45ff453245341ff4500000545000This string has as line separator "ff45". So, I want to print each line but the code below is not working. perl -pe '... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

3. Shell Programming and Scripting

perl :Changing script to only find the group

Hi scripting guru's I found this script on IBM's website and it seems to be really good only thing it gives off more info than i need. I was wondering if someone could help me modify it to only find a group instead of every user. (group is support) I believe i know how to add the line so it... (2 Replies)
Discussion started by: vpundit
2 Replies

4. Shell Programming and Scripting

perl script - command line parameter

i am a beginner, i want to make a program that takes any command line arguments... and print it out in reverse. ie. if the command line argument is "thanks for helping me" i want it to output "me helping for thanks" :D i have tried using the reverse command, but i cant get it working!! ... (3 Replies)
Discussion started by: bshell_1214
3 Replies

5. Shell Programming and Scripting

Run perl script, with command-line options

Hello everyone, I have a perl script which takes various command line options from user like : test.pl -i <input_file> -o <output_file> -d <value> -c <value> Now I have multiple input files in a directory: <input_file_1> <input_file_2> <input_file_3> <input_file_4> ..... .... ...... (6 Replies)
Discussion started by: ad23
6 Replies

6. Shell Programming and Scripting

perl/unix: script in command line works but not in perl

so in unix this command works works and shows me a list of directories find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt but when i try running a perl script to run this command my $query = 'find . -name \*.xls -exec dirname {} \; | sort -u | > list.txt';... (2 Replies)
Discussion started by: kpddong
2 Replies

7. Shell Programming and Scripting

perl script command line option driven script

could someone show me a sample command line option driven script? i want to see an easy way to write one and how i can execute it using command line options such as typing in read.pl -i <id> -c <cmds> -s <start> -e <end> would read out all the commands run by ID . from start time to... (7 Replies)
Discussion started by: kpddong
7 Replies

8. Shell Programming and Scripting

Perl script to search a line and copy it to another line

Hi I have a log file (say log.txt). I have to search for a line which has the string ( say ERROR) in the log file and copy 15 lines after this into another file (say error.txt). Can someone give me the code and this has to be in PERL Thanks in advance Ammu (3 Replies)
Discussion started by: ammu
3 Replies

9. Shell Programming and Scripting

[Perl] Accessing array elements within a sed command in Perl script

I am trying to use a script to replace the header of each file, whose filename are stored within the array $test, using the sed command within a Perl script as follows: $count = 0; while ( $count < $#test ) { `sed -e 's/BIOGRF 321/BIOGRF 332/g' ${test} > 0`; `cat 0 >... (2 Replies)
Discussion started by: userix
2 Replies

10. Shell Programming and Scripting

Perl script - changing passwords

Just wanted options of this - first 'real' Perl script and I'm not positive of all the quirks in Perl. Any suggestions are welcome. Especially since I'm messing with /etc/shadow! Running Solaris 2.6, Perl 5.005.03 #!/u/bin/perl # # Change the user's old password to the new in /etc/shadow ... (3 Replies)
Discussion started by: thehoghunter
3 Replies
Login or Register to Ask a Question