I am reading file utilization on the server with below command.
How can I add the hostname in my output as a first column?
Thanks in advance
df -h | grep % | awk '{OFS="\t";print $6,$5}'
Output:
/apps/inf9b2b 43%
/apps/dbclients 13%
/apps/inf9 77%
This is a simple application of How do I use shell variables in an awk script?
df -h | awk -v hostname="$(hostname)" '/%/ {OFS="\t"; print hostname, $6, $5}'
Note that there's no need for an external grep -- just make your pattern match a condition of the awk statement.
You can do df -h | grep % | awk '{OFS="\t";print "hostname\t" $6,$5}'
Related
I'm trying to write a bash command that will sort all volumes by the amount of data they have used and tried using
df | awk '{print $1 | "sort -r -k3 -n"}'
Output:
map
devfs
Filesystem
/dev/disk1s5
/dev/disk1s2
/dev/disk1s1
But this also shows the header called Filesystem.
How do I remove that?
For your specific case, i.e. using awk, #codeforester answer (using awk NR (Number of Records) variable) is the best.
In a more general case, in order to remove the first line of any output, you can use the tail -n +N option in order to output starting with line N:
df | tail -n +2 | other_command
This will remove the first line in df output.
Skip the first line, like this:
df | awk 'NR>1 {print $1 | "sort -r -k3 -n"}'
I normally use one of these options, if I have no reason to use awk:
df | sed 1d
The 1d option to sed says delete the first line, then print everything else.
df | tail -n+2
the -n+2 option to tail say start looking at line 2 and print everything until End-of-Input.
I suspect sed is faster than awk or tail, but I can't prove it.
EDIT
If you want to use awk, this will print every line except the first:
df | awk '{if (FNR>1) print}'
FNR is the File Record Number. It is the line number of the input. If it is greater than 1, print the input line.
Count the lines from the output of df with wc and then substract one line to output a headerless df with tail ...
LINES=$(df|wc -l)
LINES=$((${LINES}-1))
df | tail -n ${LINES}
OK - I see oneliner - Here is mine ...
DF_HEADERLESS=$(LINES=$(df|wc -l); LINES=$((${LINES}-1));df | tail -n ${LINES})
And for formated output lets printf loop over it...
printf "%s\t%s\t%s\t%s\t%s\t%s\n" ${DF_HEADERLESS} | awk '{print $1 | "sort -r -k3 -n"}'
This might help with GNU df and GNU sort:
df -P | awk 'NR>1{$1=$1; print}' | sort -r -k3 -n | awk '{print $1}'
With GNU df and GNU awk:
df -P | awk 'NR>1{array[$3]=$1} END{PROCINFO["sorted_in"]="#ind_num_desc"; for(i in array){print array[i]}}'
Documentation: 8.1.6 Using Predefined Array Scanning Orders with gawk
Removing something from a command output can be done very simply, using grep -v, so in your case:
df | grep -v "Filesystem" | ...
(You can do your awk at the ...)
When you're not sure about caps, small caps, you might add -i:
df | grep -i -v "FiLeSyStEm" | ...
(The switching caps/small caps are meant as a clarification joke :-) )
I am using 'df -h' command to get disk space details in my directory and it gives me response as below :
Now I want to be able to do this check automatically through some batch or script - so I am wondering, if I will be able to check disk space only for specific folders which I care about, as shown in image - I am only supposed to check for /nas/home that it does not go above 75%.
How can I achieve this ? Any help ?
My work till now:
I am using
df -h > DiskData.txt
... this outputs to a text file
grep "/nas/home" "DiskData.txt"
... which gives me the output:
*500G 254G 247G 51% /nas/home*
Now I want to be able to search for the number previous or right nearby '%' sign (51 in this case) to achieve what I want.
This command will give you percentage of /nas/home directory
df /nas/home | awk '{ print $4 }' | tail -n 1| cut -d'%' -f1
So basically you can use store as value in some variable and then apply if else condition.
var=`df /nas/home | awk '{ print $4 }' | tail -n 1| cut -d'%' -f1`
if(var>75){
#send email
}
another variant:
df --output=pcent /nas/home | tail -n 1 | tr -d '[:space:]|%'
output=pcent - show only percent value (for coreutils => 8.21 )
A more concise way without extensive piping could be:
df -h /nas/home | perl -ane 'print substr $F[3],0,-1 if $.==2'
Returns: 51 for your example.
When I run
df -hl | grep '/dev/disk1' | awk '{sub(/%/, \"\");print $5}'
I'm getting the following error:
awk: syntax error at source line 1
context is
{sub(/%/, >>> \ <<< "\");}
awk: illegal statement at source line 1
I can't seem to find any documentation on awk sub.
df -hl | grep '/dev/disk1'
returns
/dev/disk1 112Gi 94Gi 18Gi 85% 24672655 4649071 84% /
As I understand, it should return the percentage of disk space used.
It should return 85 from the input
/dev/disk1 112Gi 94Gi 18Gi 85% 24699942 4621784 84% /
This will fix the command as you supplied it.
df -hl | grep '/dev/disk1' | awk '{sub( /%/, ""); print $5 }'
No need to escape the double quotes.
Of course you don't need to use grep here either.
df -hl | awk '/disk1/ { sub( /%/, "", $5); print $5}'
Notice that you can supply the target for the substitution as a third argument to sub.
The sub command is described in the gawk manual on this page.
Perhaps you can reduce it down to just df and awk with:
df --output=pcent /dev/disk1 | awk '/ /{printf("%d\n", $1)}'
I have a simple bash script which is getting the load average using uptime and awk, for example
LOAD_5M=$(uptime | awk -F'load averages:' '{ print $2}' | awk '{print $2}')
However this includes a ',' at the end of the load average
e.g.
0.51,
So I have then replaced the comma with a string replace like so:
LOAD_5M=${LOAD_5M/,/}
I'm not an awk or bash wizzkid so while this gives me the result I want, I am wondering if there is a succinct way of writing this, either by:
Using awk to get the load average without the comma, or
Stripping the comma in a single line
You can do that in same awk command:
uptime | awk -F 'load averages?: *' '{split($2, a, ",? "); print a[2]}'
1.32
The 5 min load is available in /proc/loadavg. You can simply use cut:
cut -d' ' -f2 /proc/loadavg
With awk you can issue:
awk '{print $2}' /proc/loadavg
If you are not working on Linux the file /proc/loadavg will not being present. In this case I would suggest to use sed, like this:
uptime | sed 's/.*, \(.*\),.*,.*/\1/'
uptime | awk -F'load average:' '{ print $2}' | awk -F, '{print $2}'
0.38
(My uptime output has 'load average:' singular)
The load average numbers are always the last 3 fields in the 'uptime' output so:
IFS=' ,' read -a uptime_fields <<<"$(uptime)"
LOAD_5M=${uptime_fields[#]: -2:1}
What would be the correct CL sequence to execute a df -h and only print out the mount name and used space (percentage)? I'm trying to do a scripted report for our servers.
I tried
df -h | awk '{print $1 $4}'
which spits out
$df -h | awk '{print $1 $4}'
FilesystemAvail
/dev/sda164G
udev3.9G
tmpfs1.6G
none5.0M
none3.9G
none100M
/home/richard/.Private64G
How would you change this to add spacing? Am I selecting the right columns?
Try this:
df -h | awk '{if ($1 != "Filesystem") print $1 " " $5}'
Or just
df -h | awk '{print $1 " " $5}'
if you want to keep the headers.
You are almost there:
df -h | awk 'NR>1{print $1, $5}'
The issues with your code are what input to process, and how to format the output.
As an example, this awk selects records that have a % symbol at the end of field five, and put a space between the two output fields.
df -h | awk '$5 ~ /\%$/ {print $1 " " $5 }'
Everything else is just refining those two things.