file="B1_Site4_5aT4ZNHN691AQSB6B65_KYEC_SLT_2013-11-24-00-30_935985e7_100m_PASS1.tar" with GNU grepgrep when built with PCRE support (and with zsh or recent versions of ksh93 or bash for <<<):
grep -oP '(?<=Z).{6}' <<< "$file" > file with bashksh93, bash or recent versions of zsh:
tmp=${file#*Z} # remove chars up to the first Z echo "${tmp:0:6}" > file Just for fun, awkawk
awk -F Z '{print substr($2, 1, 6)}' <<< "$file"