Canzee
Canzee

Reputation: 77

Cut command not returning correct output

Scenario

I am trying to use cut command to get the 4th field from a file that has the redirected output of a df command. But it's only cutting and returning the 1st field for some reason.

df -kh -B 1g /ws/abc-Location03 > file1.txt
cut -d" " -f4 file1.txt

Output of my df command

Filesystem           1G-blocks      Used Available Use% Mounted on
abc5-xyz04c:/root_vdm_6/t2local24/abc-Location03
                       500       408        93  82% /ws/abc-Location03

Output of my cut command

abc5-xyz04c:/root_vdm_6/t2local24/abc-Location03

Basically, it's cutting just the 1st field and returning it, instead of the 4th field. I 'd like to know what it is I'm doing wrong.

Upvotes: 3

Views: 446

Answers (3)

arielf
arielf

Reputation: 5952

For this, and many other similar problems, the shortest solution might be to use cuts ("cut on steroids") which tries to fix most pain-points of cut.

Example:

$ df -kh -B 1g / | cuts 3
Used
25

http://arielf.github.io/cuts/

Upvotes: 0

whereswalden
whereswalden

Reputation: 4959

Because you're delimiting on whitespace and not repeated whitespace, your columns are messed up. Try this:

df -kh -B 1g | tr -s ' ' | cut -d ' ' -f 4

The tr turns all repeated spaces into single spaces, so you can cut them as you please.

Upvotes: 3

anubhava
anubhava

Reputation: 785156

You can use awk instead:

df -kh -B 1g /ws/abc-Location03 | awk '{print $4}'

Upvotes: 1

Related Questions