awk in solaris


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk in solaris
# 8  
Old 10-01-2009
Friends, I have a file with fileds in the following order
Code:
sda 4.80 114.12 128.69 978424 1103384
sdb 0.03 0.40 0.00 3431 0

sda 1.00 0.00 88.00 0 176
sdb 0.00 0.00 0.00 0 0

sda 1.00 0.00 88.00 0 176
sdb 0.00 0.00 0.00 0 0

Now what i want is to sum up the values in 2nd column 2 rows at a time like first sum will be 4.80 + .03. 2nd will be 1.00 + 0.00. and so on. Pls can u advice me how i can acheive this. I am running against time on this. My system is a solaris 10 SPARC system.
# 9  
Old 10-01-2009
Code:
/usr/xpg4/bin/awk '/^$/ {next} {C++; T+=$2;} !(C % 2) { printf( "%.2f\n", T ); T=0 }' file1

4.83
1.00
1.00

Interestingly using just "awk":
Code:
awk '/^$/ {next} {C++; T+=$2;} !(C % 2) { printf( "%.2f\n", T ); T=0 }' file1
awk: syntax error near line 1
awk: bailing out near line 1

# 10  
Old 10-01-2009
Quote:
Originally Posted by achak01
Friends, I have a file with fileds in the following order
Code:
sda 4.80 114.12 128.69 978424 1103384
sdb 0.03 0.40 0.00 3431 0

sda 1.00 0.00 88.00 0 176
sdb 0.00 0.00 0.00 0 0

sda 1.00 0.00 88.00 0 176
sdb 0.00 0.00 0.00 0 0

Now what i want is to sum up the values in 2nd column 2 rows at a time like first sum will be 4.80 + .03. 2nd will be 1.00 + 0.00. and so on. Pls can u advice me how i can acheive this. I am running against time on this. My system is a solaris 10 SPARC system.
My possible solution (not tested) assumes that your sample output is the result of the command :
Code:
sar -d 3 3 | awk 'NR>4 { if ($0 !~ /:/ && $1 !~ /,.+/) print}' | sed -n '/Average/!p;/Average/q;' | grep -v "nfs[0-9]"

Try :
Code:
sar -d 3 3 | \
awk '
   /Average/ { 
      exit;
   } 
   NR>4 && ! /:|nfs[0-9]/ && $1 !~ /,.+/ {
      if (NF) {
         sum += $2;
      } else {
         print sum;
         sum = 0;
      }
   }
   END { 
      if (NF) print sum;
   }
'

Jean-Pierre.
# 11  
Old 10-01-2009
This is what my script has boiled down to
Code:
#!/bin/sh

sar -d 3 3 | awk 'NR>4 { if ($0 !~ /:/ && $1 !~ /,.+/) print}' | sed -n  '/Average/!p;/Average/q;'| grep -v "nfs[0-9]" | /usr
/xpg4/bin/awk '/^$/   {next} {C++; T+=$2;} !(C % 2) { printf( "%.2f\n", T ); T=0 }'

. Please let me know if this script can be improved any more.
# 12  
Old 10-01-2009
In a word. Yes.

But I have a question.

Are you low on disk space? A carriage return and some indentation would only use up a few bytes.

It seems that your latest incarnation is an amalgamation of everyone's code!

You said that your original "code" worked fine in Linux but not in Solaris. You used iostat on Linux and Sar on Solaris?

It's one of those threads that... well, where were you when Bobby Ewing stepped out of the shower?!
# 13  
Old 10-05-2009
Actually for some tool this is what i have to calculate in solaris using my script
Code:
sar -d (%busy) sum /tot # disk

. Fot this this is what my script looks like at present.

Code:
#!/bin/sh
Disk=`iostat -xtc |  egrep -v "nfs[0-9]|device" | awk '{print $1}'| wc -l`
sar -d 3 3 | awk 'NR>4 { if ($0 !~ /:/ && $1 !~ /,.+/) print}' | sed -n  '/Average/!p;/Average/q;'| grep -v "nfs[0-9]" | /usr
/xpg4/bin/awk '/^$/   {next} {C++; T+=$2/"$Disk";} !(C % 2)  { printf( "%f\n", T" ); T=0 }'

. Any suggestions to improve the code. My script will only take into account the physical hard disks, hence i am neglecting nfs output as thats the assignment for me. pls need ur suggestions.
# 14  
Old 10-05-2009
Why don't you want to group all your pipes into a single awk command ?

Code:
# Disk=`iostat -xtc |  egrep -v "nfs[0-9]|device" | awk '{print $1}'| wc -l`

Disk=`iostat -xtc | awk '! /nfs[0-9]|device/ { c++ } END { print c }`
if [ -z "$Disk" ]
then
   echo "NO disk found !"
   exit 1
fi

# sar -d 3 3 |
# awk 'NR>4 { if ($0 !~ /:/ && $1 !~ /,.+/) print}' |
# sed -n  '/Average/!p;/Average/q;'|
# grep -v "nfs[0-9]" | /usr
# /xpg4/bin/awk '/^$/   {next} {C++; T+=$2/"$Disk";} !(C % 2)  { printf( "%f\n", T" ); T=0 }'

sar -d 3 3 | \
/usr/xpg4/bin/awk -v disk_count=$Disk '
   NR<=4 || /:/ || $1 ~ /,.+/ || /nfs[0-9]/ || ! NF {
      next
   }
   /Average/ {
      exit;
   }
   {
     C++;
     T += $2 / disk_count;
     if (! (C % 2)) {
        printf("%f\n", T);
        T = 0;
     }
   }
'

Jean-Pierre.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

awk command error in Solaris

I have the following code that works perfectly in Cygwin $ awk -F, ' BEGIN {months ="AP01"; months ="AP02"; months ="AP03"; months ="AP04"; months ="AP05"; months ="AP06"; months ="AP07"; months ="AP08"; months ="AP09"; months ="AP10"; months ="AP11"; months... (3 Replies)
Discussion started by: Raul_Rodriguez
3 Replies

2. UNIX for Dummies Questions & Answers

Converting HP-UX awk to Solaris

Hi, I am using awk in HP-UX to enter an encrypted entry of the password into /etc/passwd with success, this is the command I am using and it is working great. cat /tmp/passwd.gal.before|awk -F: -v gal_passwd="encrypted_password" '{OFS=":" ; print $1,gal_passwd,$3,$4,$5,$6,$7}' >... (3 Replies)
Discussion started by: galuzan
3 Replies

3. UNIX for Advanced & Expert Users

awk -v issue in Sun Solaris

Hi, awk -v is having issue in sun solaris, however the same works fine in HP Superdome. Pls advise what to do, while executing below command in SunOS. echo "this is saurabh"|awk -v a="SAURABH" '{ print a }' (2 Replies)
Discussion started by: sbaisakh
2 Replies

4. Shell Programming and Scripting

awk -F works on Linux, but not on Solaris

Hello, I found this command works on Linux: $ echo `uptime` | awk -F "load average: " '{ print $2 }' 1.60, 1.53, 1.46 but got error on Solaris: $ echo `uptime` | awk -F "load average: " '{ print $2 }' awk: syntax error near line 1 awk: bailing out near line 1 $ which awk... (2 Replies)
Discussion started by: seafan
2 Replies

5. Shell Programming and Scripting

awk script Error in Sun Solaris

Hi Friends, the below script has been through the errors in sun solaris. awk '/%s/{f=0 ;n++; print >(file="OUT_" n); close("OUT_" n-1)} f{ print > file}; /%s/{f=1}' $BASE_DIR/concat.TXT awk: syntax error near line 1 awk: bailing out near line 1 And ls $MERGE_DIR/*.R | awk ' { ... (2 Replies)
Discussion started by: krbala1985
2 Replies

6. Shell Programming and Scripting

System call using awk on Solaris

I'm brand new to awk and need your help. I want to be able to shut down my workstations when they become to hot (because we've lost our cooling). I found a really neat way to monitor the temperature of the system and a short, simple awk scipt to use with it: /usr/sbin/prtpicl -v -c... (2 Replies)
Discussion started by: natural
2 Replies

7. Shell Programming and Scripting

awk problem (Solaris vs Linux)

My overall goal is to parse a large log by date range - here's my code: awk '$0>=" This works fine with Linux but I get the following error with Solaris: awk: syntax error near line 1 awk: bailing out near line 1 Is there a known workaround for this problem? (1 Reply)
Discussion started by: bwatlington
1 Replies

8. Shell Programming and Scripting

awk and nawk on Solaris

Why do they do two different things? Like on one version of UNIX you can use awk, but tehn if you move to Solaris then awk becomes something crap and you need to use nawk instead! whY!?!?!?! (4 Replies)
Discussion started by: linuxkid
4 Replies

9. Shell Programming and Scripting

awk syntax for Solaris

Hi, Pass variable in SUN SOLARIS awk I have a file call text server1 10.0.0.2 When i use this awk command in Mac OS and Linux , everything works as expected. export HOSTNAME=server1 awk -v HOSTNAME=$HOSTNAME ' $1 ~ HOSTNAME { print $2 ; } ' text1 But when i entered the... (7 Replies)
Discussion started by: phamp008
7 Replies

10. Shell Programming and Scripting

awk on Solaris

Hi, we have a shell script like this .. # make sure all parameters are dealt with in upper case. for option in ${1} ${2} ${3} ${4} ${5} ${6} ${7} ${8} ${9} do option_name=`echo ${option} | awk -F \= '{print $1}'` if then SID=`echo ${option} | awk -F \= '{print $2}'` else ... (2 Replies)
Discussion started by: talashil
2 Replies
Login or Register to Ask a Question