Input: df -k
Output:
Filesystem kbytes used avail capacity Mounted on
/dev/dsk/c0t0d0s0 10332220 443748 9785150 5% /
/devices 0 0 0 0% /devices
ctfs 0 0 0 0% /system/contract
proc 0 0 0 0% /proc
mnttab 0 0 0 0% /etc/mnttab
swap 45475864 1688 45474176 1% /etc/svc/volatile
objfs 0 0 0 0% /system/object
sharefs 0 0 0 0% /etc/dfs/sharetab
/dev/dsk/c0t0d0s3 10332220 3513927 6714971 35% /usr
I want to omit the 1st line Filesystem kbytes used avail capacity Mounted on from the output.
I used df -k | tail -n+2 in linux to get exactly what i wanted, but in SunOs I get
zenvo% df -k | tail -n+2
usage: tail [+/-[n][lbc][f]] [file]
tail [+/-[n][l][r|f]] [file]
How can i achieve the Required output:
/dev/dsk/c0t0d0s0 10332220 443748 9785150 5% /
/devices 0 0 0 0% /devices
ctfs 0 0 0 0% /system/contract
proc 0 0 0 0% /proc
mnttab 0 0 0 0% /etc/mnttab
swap 45475864 1688 45474176 1% /etc/svc/volatile
objfs 0 0 0 0% /system/object
sharefs 0 0 0 0% /etc/dfs/sharetab
/dev/dsk/c0t0d0s3 10332220 3513927 6714971 35% /usr
Note: No. of rows might change
I know it's an old thread, but the shortest and the clearest of all:
df -k | sed 1d
I haven't used SunOS but using sed you should be able to delete the first line like this:
df -k | sed -e /Filesystem/d
edit: But you would have to be careful that the word Filesystem doesn't show up elsewhere in the output. A better solution would be:
df -k | sed -e /^Filesystem/d
If you want to omit the first line of any result, you can use tail:
<command> | tail -n +2
So in your case:
df -k | tail -n +2
https://man7.org/linux/man-pages/man1/tail.1.html
What about:
df -k | tail -$((`df -k | wc -l`-1))
Related
I would like to add the date for each line of the df output.
I tried:
df -m | awk '{print `date +%Y-%m`";"$1";"$2";"$3 }'
... but it doesn't work.
How can I add the date?
Here is an alternative:
df -m | awk '{print strftime("%Y-%m"), $0}'
And here is the output from the command above:
$ df -m | awk '{print strftime("%Y-%m"), $0}'
2019-10 Filesystem 1M-blocks Used Available Use% Mounted on
2019-10 devtmpfs 9852 0 9852 0% /dev
2019-10 tmpfs 9871 132 9740 2% /dev/shm
2019-10 tmpfs 9871 2 9869 1% /run
2019-10 /dev/mapper/fedora_canvas-root 50141 14731 32834 31% /
2019-10 tmpfs 9871 1 9871 1% /tmp
2019-10 /dev/sda5 976 243 667 27% /boot
2019-10 /dev/mapper/fedora_canvas-home 1277155 217435 994777 18% /home
2019-10 tmpfs 1975 63 1912 4% /run/user/1000
$
And here is an alternative version, printing just the 3 columns you wanted on the OP:
df -m | awk '{print strftime("%Y-%m"), $1, $2, $3}' | column -t
And the corresponding output:
$ df -m | awk '{print strftime("%Y-%m"), $1, $2, $3}' | column -t
2019-10 Filesystem 1M-blocks Used
2019-10 devtmpfs 9852 0
2019-10 tmpfs 9871 132
2019-10 tmpfs 9871 2
2019-10 /dev/mapper/fedora_canvas-root 50141 14731
2019-10 tmpfs 9871 1
2019-10 /dev/sda5 976 243
2019-10 /dev/mapper/fedora_canvas-home 1277155 217435
2019-10 tmpfs 1975 63
$
You may use this way:
df -m | awk -v dt=$(date "+%Y-%m") '{print dt "::", $0}'
We use -v dt=$(date "+%Y-%m") to execute date command in shell and pass it to awk in an argument dt.
If you want only first 3 columns from df command output then use:
df -m | awk -v dt=$(date "+%Y-%m") '{print dt, $1, $2, $3}'
A Perl solution.
df -m | perl -MPOSIX=strftime -alpe '$_ = strftime("%Y-%M ", localtime) . "#F[0..2]"'
Command line options:
-M : Load thestrftime() function from the POSIX module
-a : Autosplit the input records on whitespace into #F
-l : Remove newlines from input and add them to output
-p : Put each input record into $_, execute code and then print $_
-e : Run this code for each input record
The code updates $_ by concatenating the date (strftime("%Y-%M ", localtime)) with the first three columns (#F[0 .. 2]) of the input record.
So at the beginning of my script, I am defining "threshold," which has a number that isn't going to exist in the next part, (2) and a number that will exist in the next part (6). I'm having the result of running df -h to a file called dffile. My question, is how do I get grep in line 7 to search all of the variable "threshold" for the number that will exist in the file? It works if I have the 6 before the 2 in the variable, so it seems as if it's only searching the first number in it. Thanks!
#!/bin/bash
threshold=("2%" "6%")
df -h > dffile
grep $threshold dffile >> thresh
cat thresh | awk '{print $6}' >> finding1
LINES=()
while IFS= read -r finding1
do
find $finding1 -xdev -size +40M -exec ls -lah {} \; | head -n 10
done < "finding1"
The output of df -h on my test server is:
root#tstd0001:~/scripts# df -h
Filesystem Size Used Avail Use% Mounted on
udev 481M 0 481M 0% /dev
tmpfs 99M 616K 98M 1% /run
/dev/vda1 25G 1.3G 23G 6% /
tmpfs 493M 0 493M 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/vda15 105M 3.4M 102M 4% /boot/efi
tmpfs 99M 0 99M 0% /run/user/0
As you can see above, "2" from my variable, does not exist, whereas "6" does. My goal is to make grep find any number that matches a number inside the variable.
Let's consider that cat output_df is the output of your df -h command:
$ cat output_df
Filesystem Size Used Avail Use% Mounted on
udev 481M 0 481M 2% /dev
tmpfs 99M 616K 98M 1% /run
/dev/vda1 25G 1.3G 23G 6% /
tmpfs 493M 0 493M 6% /dev/shm
tmpfs 5.0M 0 5.0M 2% /run/lock
tmpfs 493M 0 493M 0% /sys/fs/cgroup
/dev/vda15 105M 3.4M 102M 4% /boot/efi
tmpfs 99M 0 99M 0% /run/user/0
Then you change the first part of your script in the following way:
thresold="2%|6%"
cat output_df | awk -v VAR=$thresold '{if($5~VAR)print $6}'
of course you will have to replace cat output_df by df -h in the final script.
This will give the output:
/dev
/
/dev/shm
/run/lock
Explanations:
thresold="2%|6%" is a regex that matches 2% or 6% you can generalize it to "2%|6%|X%|Y%|...|Z%"
You pass it as a variable to awk via -v VAR=$thresold
Then you command awk to print the 6th field when the 5th field does match the regex that you have passed to it via '{if($5~VAR)print $6}'
Then you can regroup everything without using intermediate files:
thresold="2%|6%"
for f in `df -h | awk -v VAR=$thresold '{if($5~VAR)print $6}'`
do
find $f -xdev -size +40M -exec ls -lah {} \; 2>/dev/null | head -n10
done
On my box it gives the following output:
-rw-r--r-- 1 root root 47M 6月 1 06:33 /var/cache/apt/srcpkgcache.bin
-rw-r--r-- 1 root root 47M 6月 1 06:33 /var/cache/apt/pkgcache.bin
-rw-r--r-- 1 root root 62M 4月 28 02:27 /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar
-rw-r--r-- 1 root root 56M 2月 20 06:10 /usr/lib/libreoffice/program/libmergedlo.so
-rw-r--r-- 1 root root 73M 5月 22 19:31 /usr/lib/thunderbird/libxul.so
-rwxr-xr-x 1 root root 142M 5月 22 01:35 /usr/lib/chromium-browser/chromium-browser
-rw-r--r-- 1 root root 41M 5月 8 09:57 /usr/lib/x86_64-linux-gnu/libwebkit2gtk-4.0.so.37.28.2
-rw-r--r-- 1 root root 54M 11月 17 2017 /usr/lib/x86_64-linux-gnu/libLLVM-5.0.so.1
-rw-r--r-- 1 root root 99M 3月 18 2017 /usr/lib/x86_64-linux-gnu/libOxideQtCore.so.0
-rwxr-xr-x 1 root root 42M 5月 8 09:57 /usr/lib/x86_64-linux-gnu/webkit2gtk-4.0/WebKitPluginProcess2
...
Notes: 2>/dev/null this redirection is to remove all the permission errors by redirecting stderr to /dev/null (muting the stderr)
You initialize threshold as an indexed array,
threshold=("2%" "6%")
You then call grep with:
grep $threshold dffile
Since threshold is an array, to dereference all values in the array, you use the form:
${threshold[#]} ## which can be quoted to preserve whitespace in elements
When you deference the array as a normal variable, e.g. $threshold you return only the 1st element, e.g.
echo $threshold ## output '2%'
So before going further, you need to determine what you want to pass to grep, if you want to search for either 2% or 6%, then Allan has a nice explanation above. You can also construct the grep expression using printf -v, e.g.
printf -v gexp "%s\|%s" ${threshold[#]}
or to properly limit to the first 2-elements,
printf -v gexp "%s\|%s" "${threshold[0]}" "${threshold[1]}"
and then call grep with
grep "$gexp" dffile >> thresh
Let me know if you have further questions.
I'm looking for a command where only the highest disk space usage mount will be shown. So The maximum %usage mount will be shown.
Running df -h
Filesystem Size Used Avail Use% Mounted on
/dev/vx/dsk/appdg/boom-vol
1.0G 19M 943M 2% /opt/blah99
500G 349G 152G 70% /opt/blah/data
/dev/vx/dsk/isdg/boom-shared-vol
50G 1.6G 46G 4% /opt/blah99/product/shared
/dev/vx/dsk/isdg/boom-bc-vol
150G 64G 81G 45% /opt/blah99/product/a_01
/dev/vx/dsk/isdg/boom-bt-vol
150G 47G 98G 33% /opt/blah99/product/a_02
Output should be -
500G 349G 152G 70% /opt/blah/data
What you are looking for is:
df -h | grep -vw "^\/dev" | sort -k 5 -n | tail -n 2 | head -n 1
Output of df -h | grep -vw "^\/dev":
Filesystem Size Used Avail Use% Mounted on
1.0G 19M 943M 2% /opt/blah99
500G 349G 152G 70% /opt/blah/data
50G 1.6G 46G 4% /opt/blah99/product/shared
150G 64G 81G 45% /opt/blah99/product/a_01
150G 47G 98G 33% /opt/blah99/product/a_02
Sorting by column 5 in numeric order: df -h | grep -vw "^\/dev" | sort -k 5 -n:
50G 1.6G 46G 4% /opt/blah99/product/shared
1.0G 19M 943M 2% /opt/blah99
150G 47G 98G 33% /opt/blah99/product/a_02
150G 64G 81G 45% /opt/blah99/product/a_01
500G 349G 152G 70% /opt/blah/data
Filesystem Size Used Avail Use% Mounted on
Getting second row from the end: df -h | grep -vw "^\/dev" | sort -k 5 -n | tail -n 2 | head -n 1:
500G 349G 152G 70% /opt/blah/data
I have been spending a lot of time trying to create a quota check script and have not gotten the results I need.
I am using a for loop to iterate an awk command to search for a value greater than 3000000.
Base of command to output quota:
for i in `awk '{print $2}' /etc/userdomains | grep -v "nobody" | sort -u`
do
quota -v -u $i
done
Output per iteration:
Disk quotas for user exampleuser (uid 2599):
Filesystem blocks quota limit grace files quota limit grace
/dev/sda1 8 0 0 10 0 0
/dev/sdb1 0 0 0 0 0 0
/dev/sdc1 57792 0 0 2511 0 0
/dev/sdd1 0 0 0 0 0 0
/dev/sde1 0 0 0 0 0 0
I intend to pipe an awk command to print line 1; field 5 AND line equal or greater than 3; field 2 if field 2 is greater than 50000
So the wanted output would be:
exampleuser
57792
OR
exampleuser 57792
So far I cannot get these results using different methods in awk.
Here are my last two tries (based off value greater than 3000000):
for i in `awk '{print $2}' /etc/userdomains | grep -v "nobody" | sort -u`
do
quota -v -u $i | awk '{ if ($2 >= 3000000) print $0 ; else;}'
done
Output:
Disk quotas for user bforrest (uid 2108):
Filesystem blocks quota limit grace files quota limit grace
Disk quotas for user bible (uid 500):
Filesystem blocks quota limit grace files quota limit grace
/dev/sdc1 12230716 0 0 10168 0 0
Disk quotas for user bigbeau (uid 1608):
Filesystem blocks quota limit grace files quota limit grace
Disk quotas for user bilgem (uid 3299):
Filesystem blocks quota limit grace files quota limit grace
Disk quotas for user billbell (uid 2872):
Filesystem blocks quota limit grace files quota limit grace
Disk quotas for user biosalus (uid 3215):
Filesystem blocks quota limit grace files quota limit grace
Disk quotas for user bkeating (uid 1104):
Filesystem blocks quota limit grace files quota limit grace
/dev/sdc1 3106480 0 0 9636 0 0
Disk quotas for user blaaraba (uid 2931):
Filesystem blocks quota limit grace files quota limit grace
Disk quotas for user blackbird (uid 1666):
Filesystem blocks quota limit grace files quota limit grace
Another one:
for i in `awk '{print $2}' /etc/userdomains | grep -v "nobody" | sort -u`
do
quota -v -u $i \
| awk '{ if (NR >= 3 && $2 >= 3000000) print $0 ; else;}' \
| cut -d "*" -f1
done
Output:
/dev/sdc1 55948456 0 0 45806 0 0
/dev/sdd1 91428904 0 0 97739 0 0
/dev/sdd1 512000
/dev/sdc1 60275820 0 0 10594 0 0
/dev/sdb1 512460
/dev/sdb1 93819732 0 0 47951 0 0
/dev/sdd1 527613532 0 0 11935 0 0
/dev/sdd1 56922524 0 0 60761 0 0
/dev/sdc1 307664
/dev/sdb1 65851960 0 0 257999 0 0
Maybe my method is totally off. Any thoughts on this?
UPDATE:
Found a better command (repquota -a) to report quota. Much more consistent since it doesn't vary depending on where files are located:
for i in `awk '{print $2}' /etc/userdomains | grep -v "nobody" | sort -u`
do
repquota -a | awk {'print $1 " " $3'} | grep -w $i \
| awk '{if ($2 >= 5000000) print $0 ; else;}'
done
Output:
a4fundjs 55948456
actifeve 12535196
aepromo 13224160
For you original input
awk 'NR==1{print $5} NR>2 && $2>50000 {print $2}'
will print
exampleuser
57792
Problem: the output file "single_hits.txt" is blank:
cut -f10 genome_v_trans.pslx | sort | uniq -c | grep ' 1 ' | sed -e 's/ 1 /\\\</' -e 's/$/\\\>/' > single_hits.txt
I have downloaded the script from Linux to be used on Mac OSX 10.7.5. There are some changes that need to be made as it is not working. I have nine "contigs" of DNA data that need to be filtered to remove all but unique contigs. blat is used to compare two datasets and output a .pslx file with these contigs, which worked:
964 0 0 0 0 0 3 292 + m.1 1461 0 964 3592203 ...
501 0 0 0 0 0 3 468 - m.1 1461 960 1461 5269699 ...
1168 0 0 0 1 2 7 1232 - m.7292 1170 0 1170 5233270 ...
Then this script is supposed to remove identical contigs such as the top two (m.1)
This seems to work on the limited data you gave,
grep -v `awk '{print $10}' genome_v_trans.pslx | uniq -d` genome_v_trans.pslx
unless you want it to have <> in place of the duplicates, then you can sed substitute the duplicate entries then you can do something like:
IFS=$(echo -en "\n\b") && for a in $(awk '{print $10}' genome_v_trans.pslx | uniq -d); do sed -i "s/$a/<>/g" genome_v_trans.pslx; done && unset IFS
results in:
964 0 0 0 0 0 3 292 + <> 1461 0 964 3592203 ...
501 0 0 0 0 0 3 468 - <> 1461 960 1461 5269699 ...
1168 0 0 0 1 2 7 1232 - m.7292 1170 0 1170 5233270 ...
or if you wanted that in the singlehits file:
IFS=$(echo -en "\n\b") && for a in $(awk '{print $10}' dna.txt | uniq -d); do sed "s/$a/<>/g" dna.txt >> singlehits.txt; done && unset IFS
SINGLE_TMP=/tmp/_single_tmp_$$ && awk '{if ($10 == "<>") print}' singlehits.txt > "$SINGLE_TMP" && mv "$SINGLE_TMP" singlehits.txt && unset SINGLE_TMP
or more elegant: sed -ni '/<>/p' singlehits.txt
singlehits.txt:
964 0 0 0 0 0 3 292 + <> 1461 0 964 3592203 ...
501 0 0 0 0 0 3 468 - <> 1461 960 1461 5269699 ...