Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting data with if clauses and properly outputting? 1

Status
Not open for further replies.

boolean22

IS-IT--Management
Aug 14, 2009
3
US
Hi all,
First post!
Little-bit of awk experience, need some of the expert help on here. Browsed around here, got a little further, but I am still missing some pieces. Can you help me fill-in my missing awk cells?

Sample data file (leaving out ","'s):
Column 1 Column 2 Column 3 Column 4 etc.
apple banana ice cream 11:11 p/iz/za etc.
attack banana etc 12:34 l/em/on etc.
big tomato etc etc.
blowfish tomato etc etc.
cindy tomato etc


What I have got thus far (pseudo-code):
awk -F","
{
$1=var1
if var1 ~ '/a/' var1=$1-fruit; else
if var1 ~ '/b/' var1=$1-animal; else
etc.
print "this is called" $var "thank you "

$2=var2
print $var2

$3=var3

$4=hmmm need it to just output "pizza" from that format and set to var4

print var3 " " var4

$5=var5
$6=var6

if count ($)2 appears more then once; then
for each instance
{
print $5 $6
}


--
The columns may be alpha-numeric, so I would like to script it as such.

Thank you for any help.
 
Here is some clarification:
Input:
Code:
Col1	Col2	Col3	Col4	Col5	Col6	Col7	Col8	Col9
apple1-123856	banana12	shaws	fruit	food	4:20:00 PM 8/18/2008	type2	 12.122.122.122	00:00:00:00:00:00
apple1-123856	banana12	shaws	fruit	food	4:21:00 PM 8/18/2008	type3	 12.122.122.123	00:00:00:00:00:01
apple3-123656	banana34	shaws	fruit	food	4:24:00 PM 8/18/2008	type5	 12.122.122.125	00:00:00:00:00:09
apple3-123656	banana34	shaws	fruit	food	4:21:00 PM 8/18/2008	type6	 12.122.122.126	00:00:00:00:00:08
banana1-127456	banana77	shaws	fruit	food	4:23:00 PM 8/18/2008	type2	 12.122.122.122	00:00:00:00:00:10
banana2-133456	banana88	shaws	fruit	food	10:24:00 PM 8/18/2008	type2	 12.122.122.122	00:00:00:00:00:11

Output:
Code:
<name="apple1-123856.forapples" id="banana12" type="shaws" type1="fruit food" date="20080818">	
<subdata="type2" ip="12.122.122.122" mac="00:00:00:00:00:00" />
<subdata="type3" ip="12.122.122.123" mac="00:00:00:00:00:01" />
</name>
<name="apple3-123656.forapples" id="banana34" type="shaws" type1="fruit food" date="20080818">	
<subdata="type5" ip="12.122.122.125" mac="00:00:00:00:00:09" />
<subdata="type6" ip="12.122.122.126" mac="00:00:00:00:00:08" />
</name>
<name="banana1-127456.forbananas" id="banana77" type="shaws" type1="fruit food" date="20080818">	
<subdata="type2" ip="12.122.122.122" mac="00:00:00:00:00:10" />
</name>
<name="banana2-133456.forbananas" id="banana88" type="shaws" type1="fruit food" date="20080818">	
<subdata="type2" ip="12.122.122.122" mac="00:00:00:00:00:11" />
</name>

extra notes:
-if apple = .forapples
-if banana = .forbananas
-if id's are the same, the subdata (last 3 columns) are used
 
This example should give you all you need to complete it:

Code:
awk '
        NR > 1 {
                name=$1
                ip=$10
                # add IP to ip array
                ipcount[name]++
                iparray[name,ipcount[name]]=ip
        }
        END {
                # for every name used in the ipcount array...
                for (name in ipcount) {
                        ext=""
                        if (name ~ /^apple/) ext = ".forapples"
                        if (name ~ /^banana/) ext = ".forapples"
                        print "<name=\"" name ext "\">"
                        # for every IP address recorded for this name
                        for (i=1; i<=ipcount[name]; i++) {
                                print "<ip=" iparray[name,i] ">"
                        }
                        print "</name=>"
                }
        }
' inputfile

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top