Extract substring using regexp in plain bash

前端 未结 4 1203
清酒与你
清酒与你 2020-11-27 10:52

I\'m trying to extract the time from a string using bash, and I\'m having a hard time figuring it out.

My string is like this:

US/Central - 10:26 PM          


        
4条回答
  •  情深已故
    2020-11-27 11:08

        echo "US/Central - 10:26 PM (CST)" | sed -n "s/^.*-\s*\(\S*\).*$/\1/p"
    
    -n      suppress printing
    s       substitute
    ^.*     anything at the beginning
    -       up until the dash
    \s*     any space characters (any whitespace character)
    \(      start capture group
    \S*     any non-space characters
    \)      end capture group
    .*$     anything at the end
    \1      substitute 1st capture group for everything on line
    p       print it
    

提交回复
热议问题