Help - Search - Members - Calendar
Full Version: Can Someone Convert A Pearl Oneliner to Bash?
The Planet Forums > System Administration > General Support Questions
MrBT
I have this bit of perl

CODE


#!/usr/bin/perl

$output = ">/rootIPs";

open (INV, $input);

open (OUTFILE, $output);

open(FILEHANDLE,"/root/BigEvil/evil_sort");

while(<FILEHANDLE>){

   s/^[s]*//g; ($cnt,$ip) = split(/s/); if ($cnt > 9) { print OUTFILE "$cntt$ipn"; }

}




I'd like to do that withing a bash shell script, but I'm not sure how to do it. That file it's opening, rootIPs, has a list of IPs from a "uniq -c" so it looks something like this

CODE


12 123.34.456.677

13 123.46.789.123

11 123.456.789.910

9 123.456.789.111


It's just a list of IPs, and each IP has a # before it indicating the # of hits it's given to a server. I'd like to pull out only those IPs with more than 10 hits.

So my goal is to
open file with IPs
find all with a count of greater than 10
write them to another file

I can do it with that perl, but I'd like to do it using SH
nwilkens
You could do something like:
CODE
#!/bin/sh

INFILE=/path/to/infile      

OUTFILE=/path/to/outfile  



for i in `cat ${INFILE}|sed s/ /:/g`   # change spaces to : and save line to i

do

CNT=`echo ${i}|cut -d: -f1`  

IP=`echo ${i}|cut -d: -f2`



if [ $CNT -gt 9 ]

then

  echo "${CNT} ${IP}" >> $OUTFILE

fi

done

Quick hack. I am sure it could be done quite a few different ways, and probably much better icon_wink.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.