1754014 Members
7136 Online
108811 Solutions
New Discussion юеВ

merging lines

 
SOLVED
Go to solution
OFC_EDM
Respected Contributor

merging lines

Done this before but am drawing a blank.

I'm listing all the patches on 3 separate servers into 3 files. 1D 9D and 1A

For simplicity we'll say the files have the following content.

File 1D.out 9D.out 1A.out
------- -------- ---------
patchA patchA patchB
patchB patchC patchC

I want the output to be: (FS=| and each colums 2-4 represent if the patch was found in a source file.

patchA|yes|yes|no # Found in 1D,9D and not 1A
patchB|yes|no|yes
patchC|yes|no|no

Where "yes" and "no" represent if the patch was found in the source file.

I want to write this in POSIX / KSH and not Perl since the folks I work with aren't Perl programmers.

How can I merge these records to achieve this result?
The Devil is in the detail.
10 REPLIES 10
Rodney Hills
Honored Contributor

Re: merging lines

If not Perl, then how about awk?

Rod Hills
There be dragons...
Alan Meyer_4
Respected Contributor

Re: merging lines

#!/usr/bin/sh
cat 1D 9D 1A |sort |uniq |while read PATCH ;do
LINE="$PATCH"
if [ -n "`grep $PATCH 1D`" ] ;then
LINE="$LINE|yes"
else
LINE="$LINE|no"
fi
if [ -n "`grep $PATCH 9D`" ] ;then
LINE="$LINE|yes"
else
LINE="$LINE|no"
fi
if [ -n "`grep $PATCH 1A`" ] ;then
LINE="$LINE|yes"
else
LINE="$LINE|no"
fi
print $LINE
done
" I may not be certified, but I am certifiable... "
OFC_EDM
Respected Contributor

Re: merging lines

Thanks for the ideas. I'm going to try them out.
The Devil is in the detail.
Mel Burslan
Honored Contributor

Re: merging lines

RESULTSFILE=pick_a_filename_here
cat 1D.out 9D.out 1A.out | sort | uniq > patchlist.out

for p in `cat patchlist.out`
do
grep -q $p 1D.out; r1=${?}
grep -q $p 9D.out; r2=${?}
grep -q $p 1A.out; r3=${?}
r1D="no";r9D="no";r1A="no"

if [ $r1 -eq 0 ]
then
r1D="yes"
fi

if [ $r2 -eq 0 ]
then
r9D="yes"
fi

if [ $r3 -eq 0 ]
then
r1A="yes"
fi

echo $p"\t"$r1D"\t"$r9D"\t"$r1A >> $RESULTSFILE

done


________________________________
UNIX because I majored in cryptology...
RAC_1
Honored Contributor

Re: merging lines

#!/usr/bin/ksh
#Script for patch check on different hosts

patch_to_check=$1
stat=$?
echo ${patch_to_check} > /tmp/${patch_to_check}

for i in 1D 9D 1A
do
swlist ${patch_to_check} @ ${i}
if [[ ${stat} -eq 0 ]]
then
echo "yes" > /tmp/${i}
else
echo "no" > /tmp/${i}
done
fi

echo "$(cat /tmp/${patch_to_check})|$(cat /tmp/1D)|$(cat /tmp/9D)|$(cat /tmp/1A)"


Not tested, but should work.
There is no substitute to HARDWORK
Sandman!
Honored Contributor
Solution

Re: merging lines

Kevin,

Attached a shell script (POSIX) for merging lines in order to compare patches from the three different servers.

regards!!!
OFC_EDM
Respected Contributor

Re: merging lines

Thanks for the ideas.

I'm now using all your ideas to add some extra functionality.

I want to have all the hostnames in a source file.

That way I can just modify that file and create a report on a different set of servers.

I'll let you know when done and will then assign points.

If you have tips on how to adjust posted ideas to read servers from a file and then compare the patches that would be great too.
The Devil is in the detail.
Sandman!
Honored Contributor

Re: merging lines

Shouldn't be hard if you provide the command you're using for generating the patch list on the servers you are thinking about doing this on. Actually it will be a simple while loop at the top of whatever script you're using:

================================================
cat | while read Server
do

$Server.out>

done
.
.
================================================
cheers!!!
Mel Burslan
Honored Contributor

Re: merging lines

I am assuming, this day in age, you have deployed ssh and have one central, secure node, where you can ssh to the others without having to type a pasword or passphrase.

Another assumption here: whichever directory you are dumping these *.out files into, does not have any outher files with .out extension except the patch lists

Having said that and assuming you have such a node, from this secure management node:

-----Code Begin-----


RESULTSFILE=pick_a_filename_here
####################################
# prepare the header line
####################################
printf "PATCH\t" > $RESULTSFILE
for host in `cat myhostsfile`
do
printf "${host}\t" >> $RESULTSFILE
done
printf "\n" >> $RESULTSFILE


####################################
# collect patches lists from servers
####################################
for host in `cat myhostsfile`
do

ssh ${host} "swlist -l product | grep PH | awk {'print $1'}" > ${host}.out

done



####################################
# create the RESULTSFILE
####################################
for file in *.out
cat $file >> templist
done
sort templist|uniq > patchlist

for patch in `cat patchlist`
do
printf $patch > pline

for host in `cat myhostsfile`
do
grep -q $patch ${host}.out; r=${?}
if [ $r -eq 0 ]
then
printf "yes\t" >> pline
else
printf "no\t" >> pline
fi
done

cat pline >> $RESULTSFILE
done

-----Code End-----

This is the type of situation that I would love to have this fixed character spacing code segment insertion feature that we are supposed to have now (but seems like nobody is aware of how to use it yet) The code would have looked much cooler with indentation.
________________________________
UNIX because I majored in cryptology...