Print separated datas

Hi People !!

I need your help.

I have a a txt file "example" with it datas.

[Hour]  [Data]   [IP]     [ASN]  [Country] [Domain ASN Responsable]

11:35 10/12/2003  10.10.10.1    God.com  5  country   Responsable of ASN
11:37 12/12/2003  10.10.10.1    FATHER.COM  5  country   Responsable of ASN
11:40 14/12/2003  10.10.10.3    www.mother  6  country   Responsable of ASN
11:45 14/12/2003  10.10.10.3    www.daugher  6  country   Responsable of ASN
12:50 18/12/2003  100.100.10.1  son.com  8  country   Responsable of ASN
12:55 18/12/2003  100.100.10.1   Cousin Corporation of America  8  country   Responsable of ASN

I want to print just ASN that are about my responsibility, this example only that with ASN , 5 and 6.

Example: I am using this command

egrep '5|6' example

This is a output of my command.

11:35 10/12/2003  10.10.10.1    God.com  5  country   Responsable of ASN
11:37 12/12/2003  10.10.10.1    FATHER.COM  5  country   Responsable of ASN
11:40 14/12/2003  10.10.10.3    www.mother  6  country   Responsable of ASN
11:45 14/12/2003  10.10.10.3    www.daugher  6  country   Responsable of ASN

But I wanted that my output wil be this:

ASN5=11:35 10/12/2003  10.10.10.1  God.com  5  country  Responsable of ASN
 11:37 12/12/2003  10.10.10.1    FATHER.COM  5  country   Responsable of ASN

ASN6=11:40 14/12/2003  10.10.10.3    www.mother  6  country  Responsable of ASN
 11:45 14/12/2003  10.10.10.3    www.daugher  6  country   Responsable of ASN

Because after i wil need to send e-mail to ASN5 and ASN6 separated.

Thanks all

Hi,

Using grep as you do will not work as it will capture 5 and 6 anywhere in the line. If you have a date or time that has a 5 or 6 in it, grep will return the line.

Have a go with this awk snippet:

awk '$5==5||$5==6{a[$5]=a[$5] (a[$5]?"\n":"") $0}END{for(i in a) print "ASN"i"=",a"\n"}' input.file

What about

awk '$(NF-4)~/^[5\|6]$/{print > ("ASN"$(NF-4)".result")}' in.file

Or simply [56] for the regex pattern :wink:

[LEFT]Hi good person.I hope that help me

Then When I try to do with these commands . I m with problem.

My file is so.

# Global Variables
DIR="/myhome"
WHOIS="whois.pl"
TEMPLATE="$DIR/mytemplate"
EMAIL="$DIR/email.$$"
TO="myemail"

I wanted to put result of command in variable with the example:

ASNS=`awk '$5==5||$5==6{a[$5]=a[$5] (a[$5]?"\n":"") $0}END input.file `

Because I will need other data in ASNS.
I want to do "Whois" in the field IP inside the ASNS variable

I wanted that for each line of my variable ASNS . The variable IP catch the value of field IP that is inside the $ASNS and to execute command whois, after this his send e-mail to me and starting again with second data of variable ASNS.And finally replacing field <IP> and field <data> set in my template.

I tried so , but isn't work. My for is wrong.

for I in `echo $ASNS`; do               
       IP= $ASNS | awk ' { print $3 } '  "is it field of my ip in my input.file"
       for j in  `echo $IP`; do
            # Information of WHOIS
             echo " **** WHOIS **** " > $EMAIL
             $WHOIS $I >> $EMAIL
            echo " *************** " >> $EMAIL
            echo "  " >> $EMAIL

           # Conteudo do email
            cat $TEMPLATE >> $EMAIL
            sed -i s/"<IP>"/$I/ $EMAIL
            DATA=`echo $ASNS | sed 's/ / /g'`
             sed -i s#"<data>"#"$DATA"# $EMAIL
           done
done

Thanks all[/LEFT]

[list]
[*]Please use

 tags when you post data or code sample.
[*]Please remove useless UUOC
[*]State your environment/shell

Base on your second post you need something like:
# eval "$(awk '$5==5||$5==6{a[$5]=((a[$5])?a[$5]"\n":"ASN"$5"=\"")$0}END{for(i in a)print a"\""}' file)"
# echo "$ASN6"
11:40 14/12/2003 10.10.10.3 www.mother 6 country Responsable of ASN
11:45 14/12/2003 10.10.10.3 www.daugher 6 country Responsable of ASN
# echo "$ASN5"
11:35 10/12/2003 10.10.10.1 God.com 5 country Responsable of ASN
11:37 12/12/2003 10.10.10.1 www.father.com 5 country Responsable of ASN

Wait a second , you need only the IP, lets try

# eval "$(awk '$5==5||$5==6{a[$5]=((a[$5])?a[$5]"\n":"ASN"$5"=\"")$3}END{for(i in a)print a"\""}' file)"
# echo "$ASN5"
10.10.10.1
10.10.10.1
# echo "$ASN6"
10.10.10.3
10.10.10.3

and

# while read IP;do echo $IP;done <<< "$ASN5"
10.10.10.1
10.10.10.1

I hope you can fix your script from here.
Success,

Hi danmero.

I think that I didn't explained correctly my question.
My english is poor then I think it's a problem.
If you can help me ,, it will be good.

I need to print all ASN that are about my responsability.

In this case ASN5 and ASN6 together so.

I m put this in variable , Im using it.

#FirstVariable=`egrep '5|6|' myfile  | awk '{print $3}' | sort | uniq `

11:35 10/12/2003 10.10.10.1 God.com 5 country Responsable of ASN
11:37 12/12/2003 10.10.10.1 FATHER.COM 5 country Responsable of ASN
11:40 14/12/2003 10.10.10.3 www.mother 6 country Responsable of ASN
11:45 14/12/2003 10.10.10.3 www.daugher 6 country Responsable of ASN

Now I need to get field "IP" inside the variable First .
Because I need to do "WHOIS" in each "IP"

#SecondVariable=10.10.10.1 and 10.10.10.10.3

After this I need to do command " WHOIS" , i have a script to do "WHOIS".
Then I think that could be make so.

#for I in `echo $IP`; do 
#	echo "  WHOIS  " > $EMAIL
#	$WHOIS $I >> $EMAIL
#	echo " -- " >> $EMAIL
#	echo "  " >> $EMAIL

When I do this "command for" i want to print my line(s) that are related with my ip , so.

First Time print this

#11:35 10/12/2003 10.10.10.1 God.com 5 country Responsable of ASN
#11:37 12/12/2003 10.10.10.1 FATHER.COM 5 country Responsable of ASN

And send e-mail .
I did it.

Second Time print this

11:40 14/12/2003 10.10.10.3 www.mother 6 country Responsable of ASN
11:45 14/12/2003 10.10.10.3 www.daugher 6 country Responsable of ASN

and send another e-mail.

Thanks a lot people