sed Regex to find name of database in USE sentence -
i have file:
hi use dbname; hi
which sql text.
i want capture dbname:
sed -n '1,/^\(use\|use\)/{/^\(use\|use\)/{/^\(use\|use\)\s\+\([a-za-z0-9]*\);/\1/p }}' 001.sql
edit: why try here: find line 1 line starts use. in lines, grab 1 starts use. in line, replace dbname , print.
however, says 'unknown command \' (backslash before 1/p)
what doing wrong?
if wanting extract, use grep
, made extracting things.
grep -po '(?i)\buse *\k\w+' file
-p
option interprets pattern perl regular expression.-o
option shows matching part matches pattern.(?i)
makes regular expression case insensitive.\k
throws away has matched point.
but if want stick sed, do:
sed -n 's/^use \([[:alnum:]]*\);/\1/pi' file
Comments
Post a Comment