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
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

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-14-2009
xchen89x xchen89x is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 7
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 (permalink)  
Old 09-15-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,715
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 (permalink)  
Old 09-15-2009
xchen89x xchen89x is offline
Registered User
  
 

Join Date: Sep 2009
Posts: 7
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
Reply

Bookmarks

Tags
file, perl, replace, script, 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 01:14 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