Operating System - HP-UX
1832345 Members
2546 Online
110041 Solutions
New Discussion

need awk Help from experts

 
Cem Tugrul
Esteemed Contributor

need awk Help from experts

Hi Forum,
i have approx 500 files with .csv extension
and i want to produce a final report from these
csv files.ALL of theses files have such a kind of strings which i have to use on my report;

Computer Name,Fully Qualified DNS Name,,0,Computer Name,
Memory,,Physical Memory,0,Total,
CPU,,CPU Properties,0,CPU Type,

from the original file(csv file);

Computer Name,Fully Qualified DNS Name,,0,Computer Name,MNTRANKTPC00938

CPU,,CPU Properties,0,CPU Type,Unknown; 3000 MHz

Memory,,Physical Memory,0,Total,190 MB

so My report will be like;

computer_name cpu mem
--------------- ---- ------
MNTRANKTPC00938 3000 Mhz 190 Mb

how can i produce such a kind of report via
awk?

!!!Points start with 5!!!

Our greatest duty in this life is to help others. And please, if you can't
14 REPLIES 14
Muthukumar_5
Honored Contributor

Re: need awk Help from experts

From your example:

# awk -F "," 'BEGIN{print "Computer_name cpu memory";}{printf $NF" ";}END{printf "\n";}' test.csv
Computer_name cpu memory
MNTRANKTPC00938 Unknown; 3000 MHz 190 MB

Question:

1) CPU,,CPU Properties,0,CPU Type,Unknown; 3000 MHz -- Is this format correct?

2) Is every file contains only those 3 details?

3) do you want to generate a final report with computer_name ...* and get names from differenct csv files and make combined report?

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: need awk Help from experts

Can you get back the answer for the questions?

I am continuing with available format as,

( echo "computer_name cpu memory";
for file in `find -name "*.csv"`
do
awk -F "," '!/^$/{ if ( $NF ~ "Unknown") { split ($NF,a,"; ");printf a[2]" ";}else {printf $NF" ";}}END{printf "\n"}' $file
done ) > final.report

Change which you want.

hth.
Easy to suggest when don't know about the problem!
Cem Tugrul
Esteemed Contributor

Re: need awk Help from experts

Muthukumar,

1- yes.it is correct.This csv file is being
generated when the computer logon to domain
so normally there are more information in this file hardware-software information.so i
only selected the lines which i want to produce a report

2- No.
3- Each *.csv file named as like the computername.so for example if MNTRANKTPC00938 logon to domain then it generates MNTRANKTPC00938.csv so information
cpu-mem-computer name information included in this file so i have to read all these *.csv files and get these information onto my report.
Our greatest duty in this life is to help others. And please, if you can't
Cem Tugrul
Esteemed Contributor

Re: need awk Help from experts

an example of a *.csv file
see the attachment.
Our greatest duty in this life is to help others. And please, if you can't
Muthukumar_5
Honored Contributor

Re: need awk Help from experts

Try this solution:

#!/bin/ksh
#report.ksh

#USER Input - Needed
FINALREPORT_FILE=/tmp/final.report
CSV_DIRECTORY=/tmp/

if [[ ! -f ${FINALREPORT_FILE ]]
then
echo "Computer_Name CPU Memory" >| ${FINALREPORT_FILE}
fi

find ${CSV_DIRECTORY} -type f -name "*.csv" | while read file;
do

awk -F "," '!/^$/{ if ( $NF ~ "Unknown") { split ($NF,a,"; ");printf a[2]" ";}else {printf $NF" ";}}END{printf "\n"}' ${file}

done >> ${FINALREPORT_FILE}

# END
exit 0

################

Change USER Input part with your requirement.

# chmod u+x report.ksh
# ./report.ksh

hth.
Easy to suggest when don't know about the problem!
Muthukumar_5
Honored Contributor

Re: need awk Help from experts

With your example csv file:

Change script as,

#!/bin/ksh
#report.ksh

#USER Input - Needed
FINALREPORT_FILE=/tmp/final.report
CSV_DIRECTORY=/tmp/

if [[ ! -f ${FINALREPORT_FILE ]]
then
echo "Computer_Name CPU Memory" >| ${FINALREPORT_FILE}
fi

find ${CSV_DIRECTORY} -type f -name "*.csv" | while read file;
do

computer=$(grep "Fully Qualified DNS Name" $file | grep -v "Class" | head -1 | awk -F"," '{ print $NF }')
cpu=$(grep "CPU.*Unknown" $file | awk -F"; " '{ print $NF }')
memory=$(grep "Physical Memory.*Total" $file | awk -F"," '{ print $NF }')

echo "${computer} ${cpu} ${memory}

done >> ${FINALREPORT_FILE}

# END
exit 0

Use it and post the result of success / failure.

hth.
Easy to suggest when don't know about the problem!
Cem Tugrul
Esteemed Contributor

Re: need awk Help from experts

sh report.sh
report.sh[8]: Syntax error at line 10 : `>' is not expected.

#!/bin/ksh
#report.ksh

#USER Input - Needed
FINALREPORT_FILE=/users/cemt/final.report
CSV_DIRECTORY=/users/cemt

if [[ ! -f ${FINALREPORT_FILE ]]
then
echo "Computer_Name CPU Memory" > ${FINALREPORT_FILE}
fi

find ${CSV_DIRECTORY} -type f -name "*.csv" | while read file;
do

computer=$(grep "Fully Qualified DNS Name" $file | grep -v "Class" | head -1 | awk -F"," '{ print $NF }')
cpu=$(grep "CPU.*Unknown" $file | awk -F"; " '{ print $NF }')
memory=$(grep "Physical Memory.*Total" $file | awk -F"," '{ print $NF }')

echo "${computer} ${cpu} ${memory}

done >> ${FINALREPORT_FILE}

# END
exit 0
Our greatest duty in this life is to help others. And please, if you can't
Denver Osborn
Honored Contributor

Re: need awk Help from experts

not sure if it's the prob or not, but Muthukumar's example has ">|" and your cut/paste only shows ">".. either the forums stripped it off or you missed the pipe in your script.

-denver
Cem Tugrul
Esteemed Contributor

Re: need awk Help from experts

Hi Denver,
i fixed it up.but nothing changed!!!
Our greatest duty in this life is to help others. And please, if you can't
James R. Ferguson
Acclaimed Contributor

Re: need awk Help from experts

Hi:

There is a missing curly brace. The code should read:

if [[ ! -f ${FINALREPORT_FILE} ]]
then
echo "Computer_Name CPU Memory" > ${FINALREPORT_FILE}
fi

Note also that there is no pipe. He is saying if the file doesn't exist, then create a new one with a heading line.

Regards!

...JRF...
Cem Tugrul
Esteemed Contributor

Re: need awk Help from experts

Hi James,
You exactly right..
${FINALREPORT_FILE}
also i changed echo "${computer} ${cpu} ${memory}

echo "${computer} ${cpu} ${memory}"

Then script seems working and generated the report!!!!

PS:
Hey Muthukumar,
pls send a reply because 10 is still waiting for you..
Thank's again!!!!


Our greatest duty in this life is to help others. And please, if you can't
James R. Ferguson
Acclaimed Contributor

Re: need awk Help from experts

Hi (again) Cem:

The key to debugging code is to look *near* the offending line. Generally, you will find that the real cause of the error that doesn't make sense is a few lines ahead.

Regards!

...JRF...
Cem Tugrul
Esteemed Contributor

Re: need awk Help from experts

Hi James,
You (again) right...
i sometimes mixe "seeing" & "looking" but
i know there is a nuance so that's the point..

Greetings,

Our greatest duty in this life is to help others. And please, if you can't
Cem Tugrul
Esteemed Contributor

Re: need awk Help from experts

Muthukumar's solution is OK.
Our greatest duty in this life is to help others. And please, if you can't