Skip to main content
added 141 characters in body
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k
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" 
file="B1_Site4_5aT4ZNHN691AQSB6B65_KYEC_SLT_2013-11-24-00-30_935985e7_100m_PASS1.tar" 

with GNU grep

grep -oP '(?<=Z).{6}' <<< "$file" > file 

with bash

tmp=${file#*Z} # remove chars up to the first Z echo "${tmp:0:6}" > file 

Just for fun, awk

awk -F Z '{print substr($2, 1, 6)}' <<< "$file" 
file="B1_Site4_5aT4ZNHN691AQSB6B65_KYEC_SLT_2013-11-24-00-30_935985e7_100m_PASS1.tar" 

with GNU grep when built with PCRE support (and with zsh or recent versions of ksh93 or bash for <<<):

grep -oP '(?<=Z).{6}' <<< "$file" > file 

with ksh93, bash or recent versions of zsh:

tmp=${file#*Z} # remove chars up to the first Z echo "${tmp:0:6}" > file 

Just for fun, awk

awk -F Z '{print substr($2, 1, 6)}' <<< "$file" 
Source Link
glenn jackman
  • 88.6k
  • 16
  • 124
  • 179

file="B1_Site4_5aT4ZNHN691AQSB6B65_KYEC_SLT_2013-11-24-00-30_935985e7_100m_PASS1.tar" 

with GNU grep

grep -oP '(?<=Z).{6}' <<< "$file" > file 

with bash

tmp=${file#*Z} # remove chars up to the first Z echo "${tmp:0:6}" > file 

Just for fun, awk

awk -F Z '{print substr($2, 1, 6)}' <<< "$file"