2

My output of command is like:

File Size                       : 23 kB
File Size                       : 9.8 kB
File Size                       : 9 kB
File Size                       : 128 kB
File Size                       : 921.8 kB

How to extract from this to this:

23kB
9.8kB
9kB
128kB
921.8kB

I tried to use sed with command

sed 's/^..................................//'

output is

23 kB
9.8 kB
9 kB
128 kB
921.8 kB

and in this point my knowledge about sed and regex is end. How to do that?

3

You want to remove everything before a colon, and to remove all spaces:

sed 's/.*://; s/ //g' file

The first substitution replaces .*:, i.e. anything up to the last colon, with nothing. The second substitution replaces space with nothing globally.

6

I would use cut instead of sed:

Set the delimiter to colon and choose second field:

cut -d ':' -f 2 file

Output:

 23 kB
 9.8 kB
 9 kB
 128 kB
 921.8 kB

Then remove the spaces with tr -d:

cut -d ':' -f 2 file | tr -d ' '

Output:

23kB
9.8kB
9kB
128kB
921.8kB
2

You can chain separate replacements:

's/^.*: //; s/ //'

But parsing verbose commands like that is always a bit chonky and unreliable. I would suggest something like find (with whatever filters you need) and chaining that into stat, like this:

find . -exec stat --printf="%s\n" {} +

See man stat for more options on output.

0

Using awk:

awk -F\: '{gsub(/ /, ""); print $2}'

This will use : as a delimiter, remove all spaces, and print the second column.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.