user3426358 user3426358 - 1 year ago 100
Bash Question

Remove multiple spaces in ls -l output

I need to display the filesize and the filename. Like this:

4.0K Desktop

I'm extracting these two fields using cut from the ls -l output:

ls -lhS | cut -d' ' -f5,9

Due to multiple spaces in the
ls -l
output, I'm getting a few erroneous outputs, like:

4.0K 19:54
4.0K 19:55

How should I fix this?

I need to accomplish this task using pipes only and no bash scripting ( output could be multiple pipes ) and preferably no sed, awk.

If no alternative to sed or awk is available- use of sed is OK.

Answer Source

You can use translate character command before using cut.

ls -lhS | tr -s ' ' | cut -d' ' -f 5,9
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download