LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   simple awk script question (https://www.linuxquestions.org/questions/programming-9/simple-awk-script-question-580943/)

adam_blackice 08-30-2007 07:59 AM

simple awk script question
 
hello all

while i was reading in the bash beginners guide in the awk section i found this script that prints the critical partitions

Code:

df -h | sort -rnk 5 | head -3 | \awk '{ print "Partition " $6 "\t: " $5 " full!" }'
and i want to understand what is the use of "sort -rnk 5" ,, head -3 ? and i will be thankful


thanks

nx5000 08-30-2007 08:06 AM

sort -rnk 5
is equivalent to sort -r -n -k 5
* -k 5 means sort by the 5th field (column)

* -n means numerical sort . Take 4,74,8,99
A non numerical will sort like this: 4 74 8 99
A numerical one will sort like this: 4 8 74 99

* -r means reverse
A numerical reversed : 99 74 8 4

head -3
will print the first three line of the input

In this case, it will print the 3 biggest values: 99 74 8

adam_blackice 08-30-2007 08:13 AM

thanks for you help :)

and i want to know what is the use of "\" backward slash and "/" forward slash inside the awk command ? .

Calvera 08-30-2007 08:17 AM

Quote:

Originally Posted by adam_blackice (Post 2875674)
thanks for you help :)

and i want to know what is the use of "\" backward slash and "/" forward slash inside the awk command ? .

The '\t' is giving you a tab character. Can't see a '/' in the awk command?!

D.

pwc101 08-30-2007 08:24 AM

The \ at the start of the awk command makes the shell ignore any aliases set on the awk command, and forces it to use the version of awk found in the PATH variable. This way, when writing shell scripts, you can be sure that the command you indend to run is that which is executed. This is often used in conjunction with rm to make sure files get deleted:
Code:

\rm -f files

jschiwal 08-30-2007 08:30 AM

I would highly recommend downloading the coreutils manual from the GNU website. The coreutils package contains many of the most common programs and their options.

http://www.gnu.org/software/coreutil.../coreutils.pdf

adam_blackice 08-30-2007 09:07 AM

many thanks for all\\

the df command just print the partitions like 1.)mounted patitions and 2.) the / partitions

if i want to make the script reads the whole file system like /etc /var /opt .....

any ideas

and thanks again

pwc101 08-30-2007 09:47 AM

Quote:

Originally Posted by adam_blackice (Post 2875733)
if i want to make the script reads the whole file system like /etc /var /opt .....

any ideas

Look into replacing the df command with the du command; it will do what you're looking for...

jschiwal 09-03-2007 07:24 AM

This will show you percentage of use of each mounted filesystem.

Code:

mount | grep '^/dev/' | cut -d' ' -f1 | xargs df
Filesystem          1K-blocks      Used Available Use% Mounted on
/dev/hda5            64665412  41686320  19694204  68% /
/dev/sdb2            837569504 264709116 530314248  34% /media/lbigdisk
/dev/hda1            30408696  30185872    222824 100% /mnt/windows
/dev/sdb1            122887832  1947900 120939932  2% /media/My Book
/dev/sda1            244992000 202144672  42847328  83% /media/NetDisk


adam_blackice 09-03-2007 02:23 PM

Thanks Guru


All times are GMT -5. The time now is 10:09 AM.