How to retrieve digital string using sed or awk

Hi,

I have filename in the following format:

YUENLONG_20070818.DMP
HK_20070818_V0.DMP
WANCHAI_20070820.DMP
KWUNTONG_20070820_V0.DMP

How to retrieve only the digital part with sed or awk and return the following format:

20070818
20070818
20070820
20070820

Thanks!
Victor

Assuming standard formatting:

#  echo "YUENLONG_20070818.DMP" | nawk -F"[_\.]" '{print $2}'
20070818

or if you know these are the only numerics, but position may be an issue:

#  echo "YUENLONG_20070818.DMP" | sed 's/[^0-9]//g'
20070818

HTH

1 Like

Hi, Something like this.

ls filename | sed 's/[^0-9]//g'
# sed 's/.*\([0-9]\{8\}\).*/\1/' infile
20070818
20070818
20070820
20070820
1 Like