1748019 Members
4799 Online
108757 Solutions
New Discussion юеВ

Re: shell script

 
SOLVED
Go to solution
Mark McDonald_2
Trusted Contributor

shell script

Hi all

I have a tarball of files per machine for about 1500 machines. I need a script to read each tarball and extract certain info from files within the tarball. I need this info to be put in a comma separated flat file so it can be read in to Excel.

so far I have
cd /export/home/Explorers
ls *.gz | while read FILE
do
cd /export/home/Explorers
# copy file to temp area
cp $FILE ./temporary
cd temporary

# extract the files
gunzip $FILE
tar xf *.tar
cd explorer.*
#extract the info
grep Hostname README
grep Hostid README
grep Release README
grep Company README
grep Kernel README
grep architecture README
grep Date README
grep Uptime README
#more info to be added
#clean up - remove contents of temp before next explorer
cd ..
rm -r /export/home/SUN/Explorers/temporary/*

done

The grep statements bring out the lines I require, but for each grep I need the data after the colon to be added to a csv file, and a new line to be started for each separate tar ball.

example output of the greps:
Hostname: ipcsoiwa
Hostid: 8075ac8c
Release: 5.6
Kernel architecture: sun4d
Kernel version: SunOS 5.6 Generic 105181-06
Kernel architecture: sun4d
Application architecture: sparc
Date: 2005.05.30.04.54
Uptime: 6:02am up 43 min(s), 2 users, load average: 1.34, 1.16, 0.66

Thanks

Mark
5 REPLIES 5
Patrick Wallek
Honored Contributor
Solution

Re: shell script

Well, this is probably ugly, but it should work:

HOST=$(grep Hostname README | awk -F : '{ print $2}')
HOSTID=$(grep Hostid README | awk -F : '{ print $2}')
RELEASE=$(grep Release README | awk -F : '{ print $2}')
COMPANY=$(grep Company README | awk -F : '{ print $2}')
KERNEL=$(grep Kernel README | awk -F : '{ print $2}')
ARCH=$(grep architecture README | awk -F : '{ print $2}')
DATE=$(grep Date README | awk -F : '{ print $2}')
UPTIME=$(grep Uptime README | awk -F : '{ print $2}')

echo "${HOSTNAME},${HOSTID},${RELEASE},${KERNEL},${ARCH},${DATE},${UPTIME}" >> /dir/afile

Another slight improvement is you can combine your gunzip and tar commands into a single line.

Instead of:

gunzip $FILE
tar xf *.tar

Try this:

gzcat $FILE | tar xf -

(Note there is a space between the 'xf' and the '-')
Patrick Wallek
Honored Contributor

Re: shell script

I just noticed one thing that could lead to some difficulty.

You have this grep statement:

grep Kernel README

which will return 2 lines of data:

Kernel architecture: sun4d
Kernel version: SunOS 5.6 Generic 105181-06

Then you also grep for architecture. Which will return the Kernel architecture line again. For those you should probably do this:

KERNEL=$(grep "Kernel version" README | awk -F : '{ print $2}')
ARCH=$(grep "Kernel architecture" README | awk -F : '{ print $2}')

This way you should only get one line per grep.
Mark McDonald_2
Trusted Contributor

Re: shell script

Thanks

Peter Nikitka
Honored Contributor

Re: shell script

Hi,

it's time for awk, IMHO. I see no info for 'Company', so I do not anchor that string and assume the requested value is the last field (like for the other pattern).

awk '/^Hostname:/ {hname=$NF}
/^Hostid:/ {hid=$NF}
/^Release/ {rel=$NF}
/Company/ {com=$NF}
/^Kernel architecture:/ {arch=$NF}
/^Kernel version:/ {ver=$3;for(i=4;i<=NF;i++) ver=ver" "$i }
/^Application architecture:/ {mach=$NF}
/^Date:/ {date=$NF}
/^Uptime:/ {upt=$2;for(i=3;i<=NF;i++) upt=upt" "$i }
END { printf("%s;%s;%s;%s;%s;%s;%s;%s;%s\n",hname,hid,rel,com,arch,ver,mach,date,upt)}' README

I would add some error checking as well, like instead
>>
gunzip $FILE
tar xf *.tar
cd explorer.*
<<
use in KSH
if ! gunzip<$file | tar xf -
then exit 1
fi
cd explorer.*

The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Mark McDonald_2
Trusted Contributor

Re: shell script

.