Help with pulling / filtering data from a .csv

Good day Gurus,

I have a csv file that contains an inventory of active servers. This csv file contains a well over a hundred systems (IBM, SUN, HP). It also contains those systems details. See below for an example

hostA,invver,1.02,20100430
hostA,date,08/30/2010,06:18
hostA,use,"Unknown Server Use"
hostA,os,"5.2.0.0","5200-10-04-0750","IBM,7029-6C3","0004DBAC4C00"
hostA,obp,Sat Aug 28 20:34:22 2010
hostA,mem,2048 MB
hostA,platform,IBM,7029-6C3
hostA,serial,"104DBAC"
hostA,hostid,"0xcfc0213b"
hostB,invver,1.05,20100526
hostB,date,08/30/2010,06:18
hostB,use,"unknown-server"
hostB,os,"5.10","142900-12","sun4u","SUNW,SPARC-Enterprise"
hostB,obp,"4.24.11"
hostB,mem,65536
hostB,platform,"Sun Microsystems sun4u Sun SPARC Enterprise M5000 Server"
hostB,serial,"BEF0949C7D"
hostB,hostid,"854574e2"

All I really need is the data from hostname, server platform, serial, and hostid fields. I have a script that will do that.

#!/usr/bin/bash

nawk -F'[,]' '/platform/ {print $1":", $3}; /hostid/ {print $0};
                 /serial/ {print $0}
        ' inventory.csv | sed -e 's/\"//g' > host.txt

The output from my code:

hostA: IBM
hostA,serial,104DBAC
hostA,hostid,0xcfc0213b
hostB: Sun Microsystems sun4u Sun SPARC Enterprise M5000 Server
hostB,serial,BEF0949C7D
hostB,hostid,854574e2

This code works great. But what I stumped on is, how to pull the data only on the SUN inventory? If anyone have any ideas let me know. I really don't care code I use.

Thanks,
Luffy :smiley:

As you have the host as a key, you can:

# cat TestFile.txt
hostA: IBM
hostA,serial,104DBAC
hostA,hostid,0xcfc0213b
hostB: Sun Microsystems sun4u Sun SPARC Enterprise M5000 Server
hostB,serial,BEF0949C7D
hostB,hostid,854574e2

# cat HostInv.sh
hostInvFile="TestFile.txt"
egrep -i 'sun' "${hostInvFile}" | awk -F":" '{print $1}' | \
while read hostKey
do
	egrep '^'"${hostKey}"'' "${hostInvFile}"
done

Of course, there are several ways to do the same thing!

I hope it helps.

Regads.

---------- Post updated at 13:38 ---------- Previous update was at 13:34 ----------

Maybe remove the egrep by changing the awk:

awk -F":" '/[S|s]un/ {print $1}' "${hostInvFile}"
$
$
$ cat f12
hostA,invver,1.02,20100430
hostA,date,08/30/2010,06:18
hostA,use,"Unknown Server Use"
hostA,os,"5.2.0.0","5200-10-04-0750","IBM,7029-6C3","0004DBAC4C00"
hostA,obp,Sat Aug 28 20:34:22 2010
hostA,mem,2048 MB
hostA,platform,IBM,7029-6C3
hostA,serial,"104DBAC"
hostA,hostid,"0xcfc0213b"
hostB,invver,1.05,20100526
hostB,date,08/30/2010,06:18
hostB,use,"unknown-server"
hostB,os,"5.10","142900-12","sun4u","SUNW,SPARC-Enterprise"
hostB,obp,"4.24.11"
hostB,mem,65536
hostB,platform,"Sun Microsystems sun4u Sun SPARC Enterprise M5000 Server"
hostB,serial,"BEF0949C7D"
hostB,hostid,"854574e2"
hostC,platform,"Solaris Server"
hostC,serial,"XXX9999Y9Z"
hostC,hostid,"123456x7"
hostD,platform,"sun microsystems server"
hostD,serial,"YYY8888Y8Z"
hostD,hostid,"555555x5"
$
$ # I've made the assumption that "platform", "serial" and "hostid" occur in that order (in different lines) for Sun servers in your file.
$ # They do not have to be on consecutive lines for this script to work.
$
$ awk -F, '/platform.*[Ss]un/{print $1":", $3; x=1}; /serial/ && x==1{print $0}; /hostid/ && x==1{print $0; x=0}' f12
hostB: "Sun Microsystems sun4u Sun SPARC Enterprise M5000 Server"
hostB,serial,"BEF0949C7D"
hostB,hostid,"854574e2"
hostD: "sun microsystems server"
hostD,serial,"YYY8888Y8Z"
hostD,hostid,"555555x5"
$
$

tyler_durden

Another approach:

awk -F, '/platform/ && /[sS]un/{$0=$1 ":" $3;c=3}c-->0{gsub("\"","");print}' file

Great thanks guys all of your solutions work. Thanks for the time and effort...