Operating System - HP-UX
1838571 Members
3280 Online
110128 Solutions
New Discussion

Re: A filtering script help

 
SOLVED
Go to solution
Jose Mosquera
Honored Contributor

A filtering script help

Hi Pals,

I'm trying to split the attached file in several files, the split criteria must be by third number of IP address. I was tried using grep command, but dots are complicating this filter. Please consider that the file in real life doesn't contain only addresses, it doesn't begin with them and has more information inside. But is enough for the filtering example that I need!

Regards,
10 REPLIES 10
Jean-Louis Phelix
Honored Contributor

Re: A filtering script help

Hi,

With this script, you will get 1 file (file.X) for each X (third field of IP address) :

#!/usr/bin/sh
IFS=".
"
cat b | while read LINE
do
set -- $LINE
echo $LINE >> file.$3
done
It works for me (© Bill McNAMARA ...)
Dave La Mar
Honored Contributor

Re: A filtering script help

Jose -
Not sure I am reading this correctly but to obtain separate files for every three lines of your list of ip address (and other date -

#!/bin/ksh

grep -n '154.' your_file > lined_file
cut -d: -f1 lined_file > line_numbers
tail -1 line_numbers | read last_line
cat line_numbers | wc -l | read end


i=0
for x in `cat line_numbers`
do
echo $x | read var
value[i]=$var
let i="$i+1"
done

i=0
let end="$end-3"
fnum=0

while [ $i -lt $end ]
do
let a="${value[$i]}"
let i="$i+3"
let b="${value[$i]}-1"
let fnum="$fnum+1"
sed -n "$a","$b"p your_file > out_file.$fnum
done
let fnum="$fnum+1"
let b="$b+1"
sed -n "$b","$last_line"p your_file > out_file.$fnum
rm lined_file
rm line_numbers

Even if the number of lines in the file is not divisable by three all files will contain three unique entries. The last file will contain 1-3 lines dependent upon what was left.

Best of luck.

Regards,

dl
"I'm not dumb. I just have a command of thoroughly useless information."
Chris Vail
Honored Contributor

Re: A filtering script help

I wrote the attached script 4-5 years ago for a Sun system. This reads through a DNS forward file, and creates reverse lookup files based on the content of the 3rd octet. It also re-writes the named.boot file with all the names of the reverse files, and restarts the named daemon.
You may be able to modify this to fit your own needs.



Chris
V.Tamilvanan
Honored Contributor
Solution

Re: A filtering script help

Hi,
Try this awk script.

#cat inputfile|awk -F "." '{fname = "fn."$3} {print $0 >> fname}'

HTH
vasundhara
Frequent Advisor

Re: A filtering script help

Hi,

Try this simple csh script.

fileno=1;
linenum=0;
cat "filename"| while read LINE
do
echo $LINE >> file$fileno;
linenum=`expr $linenum + 1`
if [ `expr $linenum % 3` = 0 ]
then
fileno=`expr $fileno + 1`
echo "Linenum: $linenum Fileno: $fileno\n"
fi
done

Regards
VJ.
vasundhara
Frequent Advisor

Re: A filtering script help

Hi,

Try this simple csh script.

fileno=1;
linenum=0;
cat "filename"| while read LINE
do
echo $LINE >> file$fileno;
linenum=`expr $linenum + 1`
if [ `expr $linenum % 3` = 0 ]
then
fileno=`expr $fileno + 1`
echo "Linenum: $linenum Fileno: $fileno\n"
fi
done

Regards
VJ.
vasundhara
Frequent Advisor

Re: A filtering script help

Hi,

Try this simple csh script.

fileno=1;
linenum=0;
cat "filename"| while read LINE
do
echo $LINE >> file$fileno;
linenum=`expr $linenum + 1`
if [ `expr $linenum % 3` = 0 ]
then
fileno=`expr $fileno + 1`
echo "Linenum: $linenum Fileno: $fileno\n"
fi
done

Regards
VJ.
Donny Jekels
Respected Contributor

Re: A filtering script help

dudette,

excuse for the lack of undrstanding your question.

a single line will take care of your issue, this is if I understnad it correctly.

cat | awk -F. '{print $3}' | tee -a newfile

peace
donny
"Vision, is the art of seeing the invisible"
Jose Antonio Orozco
Frequent Advisor

Re: A filtering script help

you will need first "clean" the info for process and validate the record.
cut the columns where the IPs are expected, and assure not include garbage.

i think in two scripts to obtain a file for each net (third number)

cat filewithips | cut -c1-17 | grep -e"." | xargs -i scriptnet.sh {}

where scriptnet.sh has

net=`echo $1 | cut -f3 -d.`
echo $1 > net.$net

What is not backed up, it not exists
Jordan Bean
Honored Contributor

Re: A filtering script help


If you like working with PERL, attached are two examples that sort a list of IPs to separate files by the third octet.