Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Is there a scriptiong answer to this question

Status
Not open for further replies.

fsanchez13

Technical User
May 9, 2003
35
US
I have data that looks sorta like this, only the lines are much longer, Is it possible to make a script that will combine the two lines in only certain columns?

Time TG INS OOS USG
01:39:42 50 25 10 1
01:59:42 50 25 10 10
02:39:42 50 25 10 2
02:59:42 50 25 10 10

I want my data to look like this:

Time TG INS OOS USG
01:00:00 50 25 10 11
02:00:00 50 25 10 12
 
something like that should get you started:

nawk -f sum.awk myFile.txt

#-------------------------- sum.awk
BEGIN {
SEPtime=":"
SUBSEP=" "
}

FNR==1 { print;next}

{
hour=substr($1, 1, index($1,SEPtime)-1);
$1=hour SEPtime "00" SEPtime "00"
arr[$1,$2,$3,$4]+=$5
}

END{
for (i in arr)
print i, arr;
}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Or if you prefer in shell...


#!/bin/ksh
integer TotUsg=usg=0
while read -- line
do
[[ -z $line ]] &&
continue
set -- $line
[[ $1 = Time ]] &&
{
print $line
continue
}
hour=${1%%:*}
usg=$5
: ${savehour:=$hour}
[[ $hour = $savehour ]] ||
{
print &quot;${savehour}:00:00&quot; $2 $3 $4 $TotUsg
TotUsg=$usg
savehour=$hour
continue
}
(( TotUsg=TotUsg+usg ))
done < Myfile
print &quot;${hour}:00:00&quot; $2 $3 $4 $TotUsg
 
I am having a problem with the shell version. My text file has time up to 22:39:42 and 22:59:42 and the script is only showing up to 21:00:00
 
Works for me...

Time TG INS OOS USG
01:39:42 50 25 10 1
01:59:42 50 25 10 10
02:39:42 50 25 10 2
02:59:42 50 25 10 10
22:39:42 50 25 10 15
23:39:42 50 25 10 50
23:59:42 50 25 10 22
00:39:42 50 25 10 12
00:59:42 50 25 10 2


Time TG INS OOS USG
01:00:00 50 25 10 11
02:00:00 50 25 10 12
22:00:00 50 25 10 15
23:00:00 50 25 10 72
00:00:00 50 25 10 14

Nothing in the logic would cause your error.

Check the input file.
 
Ok, I messed up by not puting an acurate description of my data... The actual data is 19 columns not inlcuding the time stamp. after the time stamp the is 2 tabs and 11 spaces. I don't know if this matters. the actual header I add in later. Sorry for not descibing it better at first.
 
Ok... I made the following changes to the shell version and it works up till I get to the lines in red. Or at least this is the line(s) I added and then it stopped all other added lines work perfect up till I added this one.

I get the following error:

testscript[52]: 00:29:420: syntax error

#!/bin/ksh
integer TotIpc=ipc=0
integer TotOpc=opc=0
integer TotOfl=ofl=0
integer TotMtu=mtu=0
integer TotUsg=usg=0
integer TotItusg=itusg=0
integer TotOtusg=otusg=0
integer Totctt=ctt=0
while read -- line
do
[[ -z $line ]] &&
continue
set -- $line
[[ $1 = Time ]] &&
{
print $line
continue
}
hour=${1%%:*}
ipc=$3
opc=$4
ofl=$5
mtu=$6
usg=$7
itusg=$8
otusg=$9
ctt=$10

: ${savehour:=$hour}
[[ $hour = $savehour ]] ||
{
print &quot;${savehour}:00:00&quot; $2 $TotIpc $TotOpc $TotOfl $TotMtu $TotUsg $TotItusg $TotOtusg $Totctt
TotIpc=$ipc
TotOpc=$opc
TotOfl=$ofl
TotMtu=$mtu
TotUsg=$usg
TotItusg=$itusg
TotOtusg=$otusg
Totctt=$ctt
savehour=$hour
continue
}
(( TotIpc=TotIpc+ipc ))
(( TotOpc=TotOpc+opc ))
(( TotOfl=TotOfl+ofl ))
(( TotMtu=TotMtu+mtu ))
(( TotUsg=TotUsg+usg ))
(( TotItusg=TotItusg+itusg ))
(( TotOtusg=TotOtusg+otusg ))
(( Totctt=Totctt+ctt ))
done < test2.txt
print &quot;${hour}:00:00&quot; $2 $TotIpc $TotOpc $TotOfl $TotMtu $TotUsg $TotItusg $TotOtusg $Totctt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top