Skip to content

Instantly share code, notes, and snippets.

@chenkaie
Created April 19, 2012 15:25
Show Gist options
  • Save chenkaie/2421678 to your computer and use it in GitHub Desktop.
Save chenkaie/2421678 to your computer and use it in GitHub Desktop.
Sed example
Try this, 剛好是 14 個連續數字,才會echo出東西
echo "./12M-20120419010203-aaaa-bbb-ccc.log" | sed -n "s/.*-\([0-9]\{13\}\)-.*/\1/p"
echo "./12M-20120419010203-aaaa-bbb-ccc.log" | sed -n "s/.*-\([0-9]\{14\}\)-.*/\1/p"
echo "./12M-20120419010203-aaaa-bbb-ccc.log" | sed -n "s/.*-\([0-9]\{15\}\)-.*/\1/p"
所以你可以判斷stdout吐出來的字串是否有長度不為0來判斷是否match
如下:
kent@rd1-2:usr $ matchstr=`echo "./12M-20120419010203-aaaa-bbb-ccc.log" | sed -n "s/.*-\([0-9]\{14\}\)-.*/\1/p"`
kent@rd1-2:usr $ [[ -n $matchstr ]] && echo matched || echo not-match
matched
kent@rd1-2:usr $ matchstr=`echo "./12M-20120419010203-aaaa-bbb-ccc.log" | sed -n "s/.*-\([0-9]\{13\}\)-.*/\1/p"`
kent@rd1-2:usr $ [[ -n $matchstr ]] && echo matched || echo not-match
not-match
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment