Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

truncate output in BASH

Tags:

linux

bash

How do I truncate output in BASH?

For example, if I "du file.name" how do I just get the numeric value and nothing more?

later addition:
all solutions work perfectly. I chose to accept the most enlightning "cut" answer because I prefer the simplest approach in bash files others are supposed to be able to read.

like image 596
Amir Arad Avatar asked Oct 27 '08 10:10

Amir Arad


People also ask

What does truncate do in bash?

truncate is a command-line utility that allows you to shrink or extend the size of a file to a given size.


1 Answers

If you know what the delimiters are then cut is your friend

du | cut -f1

Cut defaults to tab delimiters so in this case you are selecting the first field.

You can change delimiters: cut -d ' ' would use a space as a delimiter. (from Tomalak)

You can also select individual character positions or ranges:

ls | cut -c1-2
like image 164
Ken Avatar answered Sep 24 '22 03:09

Ken