Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I need to manipulate an output file
C2K248MX - 3
C2K248MX - 3
C2K248MX - 3
C2K248NE - 1
C2K248NN - 2
C2K248NN - 2
C2K248OH - 2
C2K248OH - 2
C2K248OM - 1
C2K248PB - 1
C2K248PD - 1I only want to see 1 entry with a count rather than an entry for each count,would also like to see the highest first
Any help would be appreciated as Im not a Unix professional and only have limited knowledge... Many thanks

A Korn shell, break point report is one way of doing it. I'm assuming the file is already sorted and is 3 fixed length fields:
#!/bin/ksh# no error checking
rm -f new.file
buffer=""
sum=0
while read f1 f2 f3
do
if [[ -z $buffer ]]
then
buffer="$f1"
sum=$f3
continue
fi
if [[ "$f1" != "$buffer" ]]
then
echo "$buffer $sum"
sum=$f3
else
((sum=sum+$f3))
fi
buffer="$f1"
done < out.file > new.file
echo "$buffer $sum" >> new.file
# end scriptby default, the sort command sorts in descending order:
sort new.file

Many thanks for the prompt reply
Not sure if I can combine this with current script in use.
Current script:
#!/usr/bin/ksh
EVTLOG=evtlog.`date +"%Y-%m-%d"`
(Sets date back to previous day)if [ $# -ne 1 ]
then
# Assume using todays log
logfile=$LOGS/mq/$EVTLOG
else
logfile=$1
fiflipped=`cat $logfile | grep -i -c "Dual Site Indicator changed"`
print "Operating with file : $logfile"
print "Total Site Switches : $flipped "
for atmflip in $(cat $logfile | grep -i "Dual Site Indicator changed" | cut -c382-389 | sort)
do
atmflipcount=`cat $logfile | grep -i "Dual Site Indicator changed" | grep -c $atmflip`
print "$atmflip - $atmflipcount"
done.That last print statement produces the results you see at the begining need to arrange that to produce a list with entry and count only -
Many thanks for trying though

Myself, I'm very careful with changing production code so I try to limit changes when I can. You don't say how the output of your Current script is captured - a file perhaps?
If so, you could use that file as the input file of the script I provided. I called the file out.file.
Another way is to encorporate my changes into your current script. To minimize these changes, send the output of your last for loop to a temporary file:
for atmflip in $(cat $logfile | grep -i "Dual Site Indicator changed" | cut -c382-389 | sort)
do
atmflipcount=`cat $logfile | grep -i "Dual Site Indicator changed" | grep -c $atmflip`
print "$atmflip - $atmflipcount"
done > /tmp/out.file
# note sending output to /tmp/out.fileNow, add this code to the end of you Current script:
# untested
rm -f /tmp/new.file
buffer=""
sum=0
while read f1 f2 f3
do
if [[ -z $buffer ]]
then
buffer="$f1"
sum=$f3
continue
fi
if [[ "$f1" != "$buffer" ]]
then
echo "$buffer $sum"
sum=$f3
else
((sum=sum+$f3))
fi
buffer="$f1"
done < /tmp/out.file > /tmp/new.file
echo "$buffer $sum" >> /tmp/new.file
# end scriptIf the output of your Current script is presently captured in a file then you can eliminate redirecting to /tmp/new.file

Hi Robert,
Sorry for the late reply,
Not sure if my suggestion will be of any help.
Here is a one-liner for what u have asked for.
Your file is:-
C2K248MX - 3
C2K248MX - 3
C2K248MX - 3
C2K248NE - 1
C2K248NN - 2
C2K248NN - 2
C2K248OH - 2
C2K248OH - 2
C2K248OM - 1
C2K248PB - 1
C2K248PD - 1Now here is the oneline solution for your problem:-
bash-2.03# cut -d"-" -f1 list_2|sort|uniq -c|sort -nr
3 C2K248MX
2 C2K248OH
2 C2K248NN
1 C2K248PD
1 C2K248PB
1 C2K248OM
1 C2K248NEHope its of any use to u,
Regards,
Sujan Banerjee

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |