How to extract timestamp from the filename?

Hi.,

My file name is of the format:

name_abc_20100531_142528.txt
where.,

my timestamp is of the format:
yyyymmdd_hhmmss

How to extract the date strring and time string into seperate variables in the shell script, after reading the file as the input?
I want to get the variables as in:

dateStr = 20100531
timeStr=142528

for furter processing for validation.

Thanks.,

---------- Post updated at 02:19 AM ---------- Previous update was at 02:04 AM ----------

Hi.,

I tried with :

 echo name_abc_20100531_142528.txt  | awk -F "_" '{print $3}' | read av ;echo $av

in shell itself, but failing to achieve the result, please help me out.

Thanks.,

cut can do it too

 echo name_abc_20100531_142528.txt | cut -d_ -f3 | read av ;echo $av
1 Like

Try:

echo name_abc_20100531_142528.txt |awk -F"[_.]" '{print $3, $4}'| read d t ;echo $d;echo $t
1 Like

I tried with the cut option, with which I am able to extract first date field, but it is not extracting time string as in :

dateStr=$(echo name_abc_20100531_142528.txt | cut -d_ -f3)

timeStr=$(echo name_abc_20100531_142528.txt | cut -d_ -f4)

echo $dateStr "   " $timeStr

Its printing:

20100531 142528.txt

But my desired o/p should be:

20100531 142528

Help me out to extract the field without the file extension.

Thanks.,

Franklin52's solution works better.
but i need to use nawk in solaris to get it to work.

echo name_abc_20100531_142528.txt |nawk -F"[_.]" '{print $3, $4}'| read d t ;echo $d;echo $t

On the cut, you can use

echo name_abc_20100531_142528.txt | cut -d_ -f4 | cut -d. -f1

which is less efficient..

1 Like

Thanks for your help.
nawk version of the script worked for me.

Thanks once again.:slight_smile: :b:

---------- Post updated at 03:31 AM ---------- Previous update was at 02:43 AM ----------

Hi.,

I am with other concern about the same question which I initially posted.
Actually I tested the above things by hard coding name of the file in the script. But I want it to be dynamically placed into the script.

Actually after reading file name from the shell, the i/p var acts as the file as against to string. So how to treat the file _ name as the string?

Like in:

 
read fname
IFILE=$fname
 

dateStr=$(echo $IFILE | awk -F"[_]" '{print $3}')
timeStr=$(echo $IFILE | awk -F"[_.]" '{print $4}')
echo $dateStr "   " $timeStr

Kindly suggest the solution for the same.

Thanks.,

With variable expansion

F="name_abc_20100531_142528.txt"
T=${F%.*}
D=${T#*_*_}
T=${T##*_}
D=${D%_*}
echo "TIME: $T - DATE: $D"

Using shell builtin array properties.

#!/bin/ksh or bash
F="name_abc_20100531_142528.txt"
IFS="_."  # delimiters are _ and .
f=($F)    # parse string to the array f, id 0 = 1st fld
echo "Date ${f[2]} Time ${f[3]}"
1 Like

:b: I didn't know that you could use multiple delimiters in bash !

Nobody used SED, so here it is. This is similar to what Frans did with an array.

mytime=($( echo name_abc_20100531_142528.txt | \
    sed 's/^.*_\([0-9]\+\)_\([0-9]\+\).*$/\1 \2/' ) )
echo ${mytime[0]}
echo ${mytime[1]}