hi all,
i have a file name using the following pattern:
PREFIX: AR
SOURCE: LEGACY
DATETIME: YYYYMMDD_HH24MISS
SUFFIX: .txt
sample filename:
AR_LEGACY_20101104_105500.txt
i want to extract the source which is LEGACY in this case. how do i do this using shell?
thanks.
You didn't mention what shell. If you use the external sed command this should work for most:
SOURCE=`echo $file | sed "s/^AR_//;s/_.*//"`
For bash/ksh
SOURCE=${file#AR_}
SOURCE=${SOURCE%%_*}
thanks. how can i get the shell ? i'm using Linux.
[ ~]$ uname
Linux
how can i do this in sed even if prefix is other than AR? i tried the following but doesnt work.
[ ~]$ source=`echo $file | sed "s/^*_//;s/_.*//"`
[ ~]$ echo $source
AR
[ ~]$ source=`echo $file | sed "s/*_//;s/_.*//"`
[ ~]$ echo $source
AR
thanks.. actually the prefix can be other than AR. so i tried
file=AR_LEGACY_20101104_105500.txt
source=${file#*_}
source=${source%%_*}
echo $source
seems to work.
To determine shell:
$ ps -p$$
If other than AR_ this should work too:
SOURCE=`echo $file | sed "s/^[^_]*_//;s/_.*//"`
[~]$ ps -p$$
PID TTY TIME CMD
20855 pts/5 00:00:00 bash
[~]$
thanks