Daeto
Daeto

Reputation: 480

Sed creating duplicates

I have used the command sed in shell to remove everything except for numbers from my string.

Now, my string contains three 0s among other numbers and after running

sed 's/[^0-9]*//g'

Instead of three 0s, i now have 0 01 and 02.

How can I prevent sed from doing that so that I can have the three 0s?

sample of the string:

0      cat
42     dog 
24     fish
0      bird
0      tiger
5      fly

Upvotes: 0

Views: 47

Answers (2)

mklement0
mklement0

Reputation: 437111

Now that we know that digits in filenames in the output from the du utility caused the problem (tip of the hat to Lars Fischer), simply use cut to extract only first column (which contains the data of interest, each file's/subdir.'s size in blocks):

 du -a "$var" | cut -f1

du outputs tab-separated data, and a tab is also cut's default separator, so all that is needed is to ask for the 1st field (-f1).


In hindsight, your problem was unrelated to sed; your sample data simply wasn't representative of your actual data. It's always worth creating an MCVE (Minimal, Complete, and Verifiable Example) when asking a question.

Upvotes: 3

MaxU - stand with Ukraine
MaxU - stand with Ukraine

Reputation: 210832

try this:

du -a "$var" | sed -e 's/ .*//' -e 's/[^0-9]*//g'

Upvotes: 0

Related Questions