Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut command not returning correct output

Tags:

shell

cut

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.

like image 683
Canzee Avatar asked Feb 17 '26 10:02

Canzee


1 Answers

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.

like image 100
whereswalden Avatar answered Feb 19 '26 22:02

whereswalden