extract number from string

14,774

Solution 1

echo "ABCD20110420.txt" | sed -e 's/ABCD//' -e 's/.txt//' -e 's/\(....\)\(..\)\(..\)/\1-\2-\3/'

Read: sed FAQ

Solution 2

Just use the shell (bash)

$> file=ABCD20110420.txt
$> echo "${file//[^0-9]/}"
20110420
$> file="${file//[^0-9]/}"
$> echo $file
20110420
$> echo ${file:0:4}-${file:4:2}-${file:6:2}
2011-04-20

The above is applicable to files like your sample. If you have files like A1BCD20110420.txt, then will not work.

For that case,

$> file=A1BCD20110420.txt    
$> echo ${file%.*} #get rid of .txt
A1BCD20110420
$> file=${file%.*}
$> echo "2011${file#*2011}"
20110420

Or you can use regular expression (Bash 3.2+)

$> file=ABCD20110420.txt
$> [[ $file =~ ^.*(2011)([0-9][0-9])([0-9][0-9])\.*$ ]]
$> echo ${BASH_REMATCH[1]}
2011
$> echo ${BASH_REMATCH[2]}
04
$> echo ${BASH_REMATCH[3]}
20

Solution 3

echo "ABCD20110420.txt" | sed -r 's/.+([0-9]{4})([0-9]{2})([0-9]{2}).+/\1-\2-\3/'
Share:
14,774

Related videos on Youtube

shantanuo
Author by

shantanuo

open source contributor

Updated on June 04, 2022

Comments

  • shantanuo
    shantanuo almost 2 years

    I have a string ABCD20110420.txt and I want to extract the date out of it. Expected 2011-04-20 I can use replace to remove the text part, but how do I insert the "-" ?

    # echo "ABCD20110420.txt" | replace 'ABCD' '' | replace '.txt' ''
    20110420