Issue with awk command between Linux and Solaris


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with awk command between Linux and Solaris
# 1  
Old 06-05-2017
Hammer & Screwdriver Issue with awk command between Linux and Solaris

Hi,

Here is the output using bash profile on Linux

Code:
 uptime
 04:59:14 up 16 days,  4:48,  2 users,  load average: 1.00, 1.00, 1.20

Here is the output using bash profile on Solaris

Code:
uptime
  4:00am  up 84 day(s), 22:21,  3 users,  load average: 0.09, 0.10, 0.12

Now, I m looking for a generic command to extract the average CPU Load which is the third column highlighted in BOLD RED

I tried the below command

Code:
uptime | awk -F" ,"  '{print $(NF-2)}' | cut -d ',' -f1

This works fine on Solaris but fails on Linux with the below error

Code:
awk: cmd. line:1: (FILENAME=- FNR=1) fatal: attempt to access field -1

Can you please help with a solution ?
# 2  
Old 06-05-2017
Hello mohtashims,

Obviously it will come because you have set field separator as [[:space:]],(writing like [[:space:]] as with comma space is not coming into iCode tags here) and it couldn't find any field as NF-2 so thus giving an error because it is taking it as a single field.

Could you please try following and let me know if this helps you.
Code:
uptime | awk -F'[ ,]'  '{print $(NF-4)}'

If you need to know how fields are getting acquired then you could run following command too.
Code:
uptime | awk -F'[ ,]'  '{for(i=1;i<=NF;i++){print i,$i}}'

on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk.

Thanks,
R. Singh

Last edited by RavinderSingh13; 06-05-2017 at 07:55 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 06-05-2017
Hi.

Here is one solution that works in common:
Code:
OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.7 (jessie) 

awk GNU Awk 4.1.1, API: 1.1 (GNU MPFR 3.1.2-p3, GNU MP 6.0.0)
$ uptime | awk -F, '{split($(NF-2),a,": ");print a[2]}'
0.67

and on
Code:
OS, ker|rel, machine: SunOS, 5.11, i86pc
Distribution        : Solaris 11.3 X86

awk - ( /usr/bin/awk, 2016-04-10 )
$ uptime | awk -F, '{split($(NF-2),a,": ");print a[2]}'
 0.02

Solaris awk is old, and can take only a single character -Fc:
Code:
       -Fc            Uses the character c as the field separator (FS) charac-
                      ter.  See the discussion of FS below.

excerpt from Solaris man awk, q.v.

There is a little matter of a leading space in Solaris output, easily fixed by setting to a variable and echoing without quotes, among other methods.

Both would also work with:
Code:
uptime | gawk -F, '{split($(NF-2),a,": ");print a[2]}'

OS, ker|rel, machine: SunOS, 5.11, i86pc
Distribution        : Solaris 11.3 X86
gawk GNU Awk 3.1.8

Best wishes ... cheers, drl

Last edited by drl; 06-05-2017 at 08:46 AM..
This User Gave Thanks to drl For This Post:
# 4  
Old 06-05-2017
ResolvedSmilie
# 5  
Old 06-05-2017
On linux, with the field separator set to ", " (instead non-existent " ,"), this is the result of post#1's awk script:
Code:
uptime | awk -F", "  '{print $(NF-2)}' | cut -d ',' -f1
 load average: 0

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Simple sed command not working; could be a Mac/Linux vs. PC/Linux issue

Hello, I am on a Mac and trying to clean up some monthly files with a very simple SED: sed '3,10d;/<ACROSS>/,$d' input.txt > output.txt (from the input, delete lines 3 - 10; then delete from the line containing <ACROSS> to the end of the file) then output to output.txt Even when I try... (2 Replies)
Discussion started by: verbatim
2 Replies

2. Shell Programming and Scripting

Issue executing grep command on Solaris

more jdbc.xml <name>Fast_ds/DataSource</name> <property> <name>user</name> <value>COL_USER</value> </property>Command 1: grep -A1 '<name>user</name>' jdbc.xml|grep -v '<name>user</name>'|sed 's/\(<value>\|<\/value>\)//g'| sed -e 's/^*//'Output: Command 2: grep... (5 Replies)
Discussion started by: mohtashims
5 Replies

3. Red Hat

Issue in tmadmin command in Linux

Hi , I am trying to execute the below command, i got error msg.. Please advice me how to solve this See, When i raised psr cmd., then i got error like above , Please help on this THanks Mani (0 Replies)
Discussion started by: Mani_apr08
0 Replies

4. Shell Programming and Scripting

Variable value substitution issue with awk command issue

Hi All, I am using the below script which has awk command, but it is not returing the expected result. can some pls help me to correct the command. The below script sample.ksh should give the result if the value of last 4 digits in the variable NM matches with the variable value DAT. The... (7 Replies)
Discussion started by: G.K.K
7 Replies

5. Cybersecurity

SSH password-less login issue between linux and solaris

Hello Gurus, I am trying to set up bidirectional password-less login between a linux and a Solaris. The way I am doing is very simple, which is creating pub/priv key pairs on each host and add the pub key to each other's authorized_keys file: ssh-keygen -t rsa (I tried dsa, and it didn't work... (4 Replies)
Discussion started by: error_lee
4 Replies

6. Red Hat

Issue in Linux Command

Hi, :wall: I am not able to run any utility command in Redat, Please see the below example and suggest me how I shall run this type of command. $ su Password: # ifconfig bash: ifconfig: command not found # which ifconfig /usr/bin/which: no ifconfig in... (3 Replies)
Discussion started by: pradipta_pks
3 Replies

7. 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

8. Shell Programming and Scripting

Sed command issue in linux

I ran one the script in debug mode in linux and have a problem ret='$prmAttunityUser=ais' Now i need to remove $ from this '$prmAttunityUser=ais' so i had added a sed command like this sed 's/$//g' but its not working could you all please help me with an alternate command I want the output... (3 Replies)
Discussion started by: vee_789
3 Replies

9. Shell Programming and Scripting

Issue with Find command on Linux

Hi, I am issuing find command below mentioned ways but it givs different count. I don't understand the behaviour. Could any one have any clue? $ find . -mtime -5 -maxdepth 1 -exec ls -lrt {} \; | wc -l 169 $ find . -mtime -5 -maxdepth 1 | wc -l 47 $ find . -mtime -5 -maxdepth 1 | wc -l... (2 Replies)
Discussion started by: siba.s.nayak
2 Replies

10. Linux

Space bar issue in Vi editor on Linux - Solaris

Hi I have a process generating a file in Solaris. Now we have migrated the process to Linux. When we open the file in vi on solaris and hit space bar, it stops after reaching the end of line. But in linux it continues to go on the next line. So I want to know whether the difference is between the... (4 Replies)
Discussion started by: sudhamacs
4 Replies
Login or Register to Ask a Question