1756768 Members
2600 Online
108852 Solutions
New Discussion юеВ

More help with awk

 
SOLVED
Go to solution
Don Spare
Regular Advisor

More help with awk

In my previous post http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x127e38dfa974d711abdc0090277a778c,00.html I received lots of good help with my awk script. I have extended that script and now I need more help.

I have added local variables that get set with values and printed at the END. The single line output is intended for use with Oracle SQLLDR to save this info in a database table for later retrieval and analysis. Anyway, I am able to get all the local variables except 'srvr' to print on the single line. It seems to have something to do with the position of that variable on the line as it prints just fine when it is the last item. Here's the revised script:

awk -v ptnum=$portnum '($2 == "is" && $3 == "up,") {
print "Port# " ptnum " Status: " $3 " Protocol: " $7
pnum=ptnum
pstat=$3
protstat=$7
next}
($1 == "Description:") {
print " Server: " substr($0,16,30)
srvr=substr($0,16,30)
next}
($2 == "packets" && $3 == "input,") {
print " Pkt In: " $1 " Byte In: " $4
inp_pkt=$1
inp_byt=$4
next}
($2 == "input" && $3 == "errors,") {
print " IN Errs: " $1 " CRC: " $4
inp_err=$1
inp_crc=$4
next}
($2 == "packets" && $3 == "output,") {
print " Pkt Out: " $1 " Byte Out: " $4
out_pkt=$1
out_byt=$4
next}
($2 == "output" && $3 == "errors,") {
print " OUT Errs: " $1 " Collisions: " $4
out_err=$1
colls=$4
next}
END {
print srvr "," ptnum pstat "," protstat "," inp_pkt "," inp_byt "," inp_err "," inp_crc "," out_pkt "," out
_byt "," out_err "," colls "," }
' $pfile >> gpi.awk_out

That last 'print' statement is what I'm after.
4 REPLIES 4
Don Spare
Regular Advisor

Re: More help with awk

Boy, did I get this wrong. The problem is that the value of 'srvr' contains an embedded ^M. So what I really need to do is remove the ^M from the file before I send it through awk. I've tried sed but can't seem to get the right combinations.

What is a good way to strip ^M characters from a file?
John Poff
Honored Contributor

Re: More help with awk

Hi,

There are a couple of ways to strip the ^M characters out of the file. One way is to use the dos2ux command. Another way is to use sed like this:

sed 's/.$//'

JP


James R. Ferguson
Acclaimed Contributor

Re: More help with awk

Hi:

'sed' can correct this. You will need to press the CONTROL key, and while holding down the CONTROL key, press a lowercase "v". Continue to hold down the CONTROL key and press "M". This sequence generates a carriage return. This sequence is documented in 'ascii(5)'. When done properly, it will look like:

# echo "This is^M botched with CRs^M and needs to be fixed"|sed 's/^M//g'

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: More help with awk

My weapon of choice for this would be "tr"

tr -d "[\015]" < infile | awk

Octal 015 is your ^M.
If it ain't broke, I can fix that.