Skip to main content
added 1 character in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

Another awk approach. Since awk can take multiple characters as field separators, this can be done in a single step:

$ awk -F'[-_]' '{print $3$4$5$6}' <<<"$var"
20161014111250

Alternatively, in Perl:

$ perl -pe 's/.+?_.+?_//; s/[-_]//g' <<<"$var"
20161014111250

Or

$perl$ perl -F_ -ane 's/-//g for @F; print @F[2..$#F]' <<<"$var"
20161014111250

I'm using here strings, but if your shell doesn't support them just echo $var | command for each command above.

Another awk approach. Since awk can take multiple characters as field separators, this can be done in a single step:

$ awk -F'[-_]' '{print $3$4$5$6}' <<<"$var"
20161014111250

Alternatively, in Perl:

$ perl -pe 's/.+?_.+?_//; s/[-_]//g' <<<"$var"
20161014111250

Or

$perl -F_ -ane 's/-//g for @F; print @F[2..$#F]' <<<"$var"
20161014111250

I'm using here strings, but if your shell doesn't support them just echo $var | command for each command above.

Another awk approach. Since awk can take multiple characters as field separators, this can be done in a single step:

$ awk -F'[-_]' '{print $3$4$5$6}' <<<"$var"
20161014111250

Alternatively, in Perl:

$ perl -pe 's/.+?_.+?_//; s/[-_]//g' <<<"$var"
20161014111250

Or

$ perl -F_ -ane 's/-//g for @F; print @F[2..$#F]' <<<"$var"
20161014111250

I'm using here strings, but if your shell doesn't support them just echo $var | command for each command above.

Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

Another awk approach. Since awk can take multiple characters as field separators, this can be done in a single step:

$ awk -F'[-_]' '{print $3$4$5$6}' <<<"$var"
20161014111250

Alternatively, in Perl:

$ perl -pe 's/.+?_.+?_//; s/[-_]//g' <<<"$var"
20161014111250

Or

$perl -F_ -ane 's/-//g for @F; print @F[2..$#F]' <<<"$var"
20161014111250

I'm using here strings, but if your shell doesn't support them just echo $var | command for each command above.