Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Linux scripting
#1

Simple, yet I'm trying to figure out awk. Maybe someone can help me out here.

 



Code:
angry:/home/v702623# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/hda3             8.7G  940M  7.3G  12% /




 

now if I wanted to just display the /dev/hda3 8.7G 940M 7.3G 12% /

how would I go about doing that?

 



Code:
angry:/home/v702623# df -h | grep /dev/hda3
/dev/hda3             8.7G  940M  7.3G  12% /




 

However, what if I wanted to take it a step further? Something more like where it displays the output like this.

 

Filesystem: [/dev/hda3] Size: [8.7G] Used: [940M] Free: [7.3G] %Used: [12%]

 

I'd like to learn how to do that.

Reply
#2
df -h |grep /dev |awk '{print "Filesystem: " $1 " Size: " $2 " Used: " $3 " Free: " $4 " Use: " $5}'
Reply
#3

Thanks a bunch Grep420. Works like a champ. I added the []'s

 



Code:
df -h |grep /dev |awk '{print "Filesystem: [" $1 "] Size: [" $2 "] Used: [" $3 "] Free: [" $4 "] Use: [" $5 "]"}'




 

This prints:

 

Filesystem: [/dev/hda3] Size: [8.7G] Used: [978M] Free: [7.3G] Use: [12%]

:)

Reply
#4
glad to help
Reply
#5

or you can create an executable file in case any of those ', &, ", or $ get in the way.

 



Code:
#!/bin/bash
df -h |grep /dev |awk '{print "Filesystem: [" $1 "] Size: [" $2 "] Used: [" $3 "] Free: [" $4 "] Use: [" $5 "]"}'




Reply
#6

tell us how Ritter !

 

i'd like to know :)

 

being the noob that i am and all that ;)

 

cheers

 

anyweb

Reply
#7

Another one to work with.

 



Code:
df -h | grep hda3 | sed 's/\(\/dev\/hda3\)\([ ]\+\)\([0-9.GM]\+\)\([0-9.GM]\+\)\(.*\)/Filesystem: \[\1\] Size: \[\3\] Used: \[\4\]/'
Filesystem: [/dev/hda3] Size: [8.7] Used: [G]




Reply
#8

Quote:tell us how Ritter ! 

i'd like to know

 

being the noob that i am and all that
 

All you have to do is

 



Code:
angry:/# vi hddinfo.pl




 

Edit the file in VI with the following:

 



Code:
#!/bin/bash
df -h |grep /dev |awk '{print "Filesystem: [" $1 "] Size: [" $2 "] Used: [" $3 "] Free: [" $4 "] Use: [" $5
"]"}'
~
~
~




 

What I did for irssi is then add it to the directory ~/.irssi/alias/hddinfo.pl

Then created the alias in irssi like:

 

/alias hdd /exec -o perl /home/v702623/.irssi/alias/hddinfo.pl

 

That will display "Filesystem: [/dev/hda3] Size: [8.7G] Used: [978M] Free: [7.3G] Use: [12%]" Being that it is my hdd.

 

Else without putting it into a perl script, the $1 $2 $3 are alias characters in irssi causing them to be stripped when executing the script itself.

 

Anyway, thanks for your help guys.

Reply
#9



Code:
./exec -o df -h |grep /dev/hd |awk '{print "\00314 Filesystem: [\0034\002" $1 "\002\00314] Size: [\0034\002" $2 "\002\00314] Used: [\0034\002" $3 "\002\00314] Free: [\0034\002" $4 "\002\00314] Use: [\0034\002" $5 "\00314\002]"}'




 

That adds color to the output. :]

Reply
#10

I did this:

 

[user@box user]$ cat >> diskfree

#!/bin/bash

df -h |grep /dev |awk '{print "Filesystem: [" $1 "] Size: [" $2 "] Used: [" $3 "] Free: [" $4 "] Use: [" $5 "]"}'

 

Then hit ctrl d to save it.

 

Then do:

 

[user@box user]$ chmod u+x diskfree

 

Then:

 

[user@box user]$ ./diskfree

Filesystem: [/dev/hdb2] Size: [8.8G] Used: [4.0G] Free: [4.3G] Use: [49%]

Filesystem: [/dev/hdb1] Size: [101M] Used: [14M] Free: [81M] Use: [15%]

Filesystem: [none] Size: [125M] Used: [0] Free: [125M] Use: [0%]

Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)