tags:

views:

122

answers:

7

Hi,

take the example:

$ du -h file_size.txt
112K file_size.txt

How would i remove the filename from the output of du -h

I have tried to use sed to search for a string (the filename) and replace it with nothing, but it hasnt worked (command below)

du -h file_size.txt | sed 's/ 'file_size.txt'//'

Could someone please point out why this wont work, or perhaps a better way to do it?

Regards

Paul

+3  A: 
du -h file_size.txt | cut -f1
Tomalak
+5  A: 

You have some bad quoting in that command line. The simplest is probably:

du -h file_size.txt | cut -f -1

To fix your command line:

du -h file_size.txt | sed 's/file_size.txt//'

Since your post has an awk tag:

du -h file_size.txt | awk '{ print $1 }'
Carl Norum
or `du -h file_size.txt | sed 's/\s+.*//'`
Tomalak
@Tomalak has the right idea for using `sed` - sure beats typing the filename twice.
Carl Norum
Woops, the single quotes where a left over of my have a $VARIABLE in there earlier. Thanks for the help as always
paultop6
@paultop6: If you use a variable, instead of opening, closing and re-opening and closing single quotes just use double quotes like this: `sed "s/${VARIABLE}//"`
Dennis Williamson
@Dennis: Does this work as correctly even if the variable contains regex meta characters?
Tomalak
@Tomalak: You're right.
Dennis Williamson
+4  A: 

Simple output the first column. Here is the awk solution

du -h test.txt  | awk '{ print $1 }'
tux21b
+++1 :-) aus .at
Flavius
A: 

This is a little more verbose, but in any case, this is another way to what you want:

du -h file_size.txt |while read x junk; do echo $x; done
vezult
A: 

@OP if your data has distinct fields, for example spaces/tabs, then its easier to use tools like awk/cut to split them on those fields by delimiters. Using regex (eg sed ) is not necessary. Even better is just using the shell.

various ways

$ du -h file | awk '{print $1}'
4.0K

$ du -h file | cut -f1
4.0K

$ set -- $(du -h file) #using shell only
$ echo $1
4.0K
ghostdog74
A: 

Your original command would have worked except that you are including a space in the sed regexp, and du outputs tabs.

Peter Westlake
A: 

the output of du -h is [tab] delimited.

du -h file_size.txt |sed 's/[\t].*//g'