8

Given the following substring given obtained from the command df /dev/sdb1 | tail -n 1 :

/dev/sdb1 1952971772 1441131032 511840740 74% /media/kaiman/ShareData

I'd like to extract 1952971772 and 74% from that string, to use it in a bash script.

I know I could use some cut commands or something, but it won't work anymore if a size changes, for example.

I also had thoughts about using a regex or something, but I'd like to have the most recommended approach.

Thanks in advance!

  • 2
    Are you asking about the general case, or about df in particular? If the latter, you can specify particular output fields explicitly e.g. df --output=pcent /dev/sdb1 – steeldriver Apr 30 '19 at 12:24
  • 2
    You are using cut on character position on line which makes it fail when line length changes with fluctuations in file size. I believe you can tell cut to select column 2 and column 5 delineated by spaces but I'm on phone and can't research and effectively post an answer. – WinEunuuchs2Unix Apr 30 '19 at 12:25
  • 1
    @WinEunuuchs2Unix Number of spaces may vary, so it's hard to tell which column-number is correct. cut will see one column after each delimiter. – mook765 Apr 30 '19 at 12:28
  • Two or more contiguous spaces still count as a single space for delineating columns I think – WinEunuuchs2Unix Apr 30 '19 at 12:30
  • @WinEunuuchs2Unix correct :) I the commands from a text file I have at hand :D – Rinzwind Apr 30 '19 at 12:32
6
$ df /dev/sdb1 | awk '{ print $5 }' | tail -n 1

for the percentage and

$ df /dev/sdb1 | awk '{ print $2 }' | tail -n 1

for the size.

Without the tail:

df /home | awk 'NR==2 { print $2 }'
df /home | awk 'NR==2 { print $5 }'

  • awk '{ print $2 }'
    gets the 2nd column where multiple separators count as 1. So the 2nd column is always the same for your system for the 1st part of the command.

  • NR gives you the total number of records being processed or the line number.

| improve this answer | |
  • 1
    Or if you need both in one line df /home | awk '{print $2; print $5;}'. – Videonauth Apr 30 '19 at 13:26
  • 3
    Awk keeps the last line in the END block so instead of using tail just use awk 'END { print $2,$5}' – Kevin Apr 30 '19 at 18:03
  • FYI I name dropped you in Meta :) meta.askubuntu.com/questions/18584/… – WinEunuuchs2Unix May 1 '19 at 1:50
8

Edited answer

As @kevin stated, you can use awk to select only the last line and avoid using tail at all by using END and also if you want to print two columns at once you can do this:

df /dev/sdb1 | awk 'END {print $2,$5}'
1952971772 74%

Original answer

You can select the 2nd columns only:

$ df /dev/sdb1 | tail -1 | awk '{print $2}' 
1952971772

Where $2 indicates the position of TAB separated columns. You can use cut because it handle only one occurrence of TAB and here there are multiples TABs to delimita a column.

| improve this answer | |
  • 3
    Awk keeps the last line in the END block so instead of using tail just use awk 'END { print $2,$5}' – Kevin Apr 30 '19 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.