Operating System - HP-UX
1846601 Members
2558 Online
110256 Solutions
New Discussion

Re: Need some AWK help ...

 
SOLVED
Go to solution
Robert Gamble
Respected Contributor

Need some AWK help ...

A new version of the software produced an extra field in it's output log, that is parsed for for reporting. The new field is not required or wanted to report. The tool used for parsing is a combination of ksh and awk in a spaghetti-like code monster. I intend to edit only one of the awk scripts that determines the version of the software used, and insert logic to remove the extra field. I don't want to introduce any more scripts.

Since we were able to determine that is only one new field, we would like to remove it, and the following OFS of that field. The FS and OFS is "," The record could have varying amount fields, but the one we want to remove is always 7th from the last.

I realize I could just: printf $(NF -7) = ""
but that still leaves the OFS for the value that was "zero'd" out.

I attached a working test example of what I have right now, and I am hoping someone can tell me an easier way to accomplish this task, and meet the requirements.

TIA !
7 REPLIES 7
A. Clay Stephenson
Acclaimed Contributor

Re: Need some AWK help ...

I wouldn't bother with OFS and IFS and simply use the split command to load an array using the comma as the separator.

{
n = split($0,arry,",")
if (n > 1)
{
i = 1
while (i < (n - 7))
{
printf("%s,",arry[i])
++i
}
++i;
while (i < n)
{
printf("%s,",arry[i])
++i
}
printf("%s\n",arry[i])
}
}
}

If I haven't made any typo's that should be your awk script.
If it ain't broke, I can fix that.
Juergen Tappe
Valued Contributor
Solution

Re: Need some AWK help ...

... or you could do it that way :

i=1
while (i <= NF)
{
if(i!=(NF-7)) printf "%s,",$i
i++
}
print ""

... for each line and NF-7 disarreared.
Working together
Nicolas Dumeige
Esteemed Contributor

Re: Need some AWK help ...

Hello,

To make field number n desappear :

awk '{ $n="" o } { print $0 }' filename

Cheers

Nicolas
All different, all Unix
Nicolas Dumeige
Esteemed Contributor

Re: Need some AWK help ...

[continued]

to get rid of the extra blank :

awk '{ ... }' | sed -e 's/\ \//g'

OR

I was wandering how to manage to affect the ascii delete caractere instead of a null string. That way, print would would display a blank, then got backward one caracter.

I use that trick to do a chrono in shell with printf "\\015$ELAPSED "

#-----CHRONO SHELL------------
#!/bin/ksh
tpssec() {
H=`date +'%H'` ; M=`date +'%M'` ; S=`date +'%S'`
echo "($H * 3660) + ($M * 60) + $S" | bc | read DATE
echo $DATE
}
elapsed() {
DATEREF=$1 ; NEWDATE=$2
[ $NEWDATE -lt $DATEREF ] && (( NEWDATE += 87780 ))
echo "$NEWDATE - $DATEREF" | bc | read DIFF
if [ $DIFF -ge 3660 ] ; then
echo "$DIFF / 3660" | bc -l | cut -c1-2 | sed s/"\."// | read HEURE
echo "$DIFF - ($HEURE * 3660)" | bc | read DIFF
else
HEURE=0
fi
if [ $DIFF -gt 60 ] ; then
echo "$DIFF / 60" | bc -l | cut -c1-2 | sed s/"\."// | read MINUTE
echo "$DIFF - ($MINUTE * 60)" | bc | read DIFF
else
MINUTE=0
fi
printf "Elapsed time %02d:%02d:%02d" $HEURE $MINUTE $DIFF
}
clock() {
tpssec | read DATEREF
while : ; do
tpssec | read NEWDATE
elapsed $DATEREF $NEWDATE | read ELAPSED
printf "\\015$ELAPSED "
sleep 1
done
}
clock
#-----END OF SHELL------------

Cheers

Nicolas
All different, all Unix
Robert Gamble
Respected Contributor

Re: Need some AWK help ...

Thanks for all the replies!

Special Thanks to Juergen! your example gave me the idea for what I plan to implement.

I have attached an example of my solution.
Juergen Tappe
Valued Contributor

Re: Need some AWK help ...

Another Idea coming to my mind:

If the number of records does not vary within a file but you have huge AWKs running on them, it might be easier to keep them as they are, but to modify the input for them so that it is as it was before the update i.e.:

current script:
awk '
{
# huge awk script
.
.
}' filename

future script:
NF=$(head -1 filename | awk '{print NF}')
cut -d"," -f1-$((NF-8)),$((NF-6))-$NF filename |
awk '
{
# same huge awk script as before
.
.
}' # no filename here

could be an even better solution. :)
Working together
Robert Gamble
Respected Contributor

Re: Need some AWK help ...

The number of fields, and the number of records vary greatly for each file.

The first 23 fields are constant, and the last 15 are constant, with the total number of fields varying between 47 and 72.

Based on a value of the 2nd field, it may remove the $(NF -7).

Thanks again for your assistance!

:)