获取bash脚本中的第一行未注释行(即不以#开头)
我正在GitHook中处理开发人员提交消息 假设该文件具有以下内容获取bash脚本中的第一行未注释行(即不以#开头),bash,shell,git-bash,Bash,Shell,Git Bash,我正在GitHook中处理开发人员提交消息 假设该文件具有以下内容 \n new lines here # this is a sample commit # only for the developers Ticket-ID: we fix old bugs and introduces new ones we always do this stuff so cool, not really :P # company name 我的目的是只获取这一行票证ID:我们修复了旧的bug并引入
\n new lines here
# this is a sample commit
# only for the developers
Ticket-ID: we fix old bugs and introduces new ones
we always do this stuff
so cool, not really :P
# company name
我的目的是只获取这一行
票证ID:我们修复了旧的bug并引入了新的bug
User123的评论既漂亮又简洁:grep-E“^[:alnum:]”但是,文件| head-n 1
不会捕获以非#
的非字母数字字符开头的文本行,例如以表情符号、破折号、括号等开头的提交消息
- 下面的单行
应根据您的要求工作:grep
说明:grep -E "^[[:alnum:]]" file |head -n 1
^[[:alnum:]] :: to capture only the line starting with any alphanumeric character[0-9A-Za-z] head -n 1 :: to capture the first occurrence
或只是cat文件| grep-v“#”| head-n1
或只是cat文件| grep“Ticket ID”
@RobertoManfreda谢谢你的回答,但如果第一行是新行,我只会得到新行字符。此外,我不能grep ticket id,因为它可以是任何grep“Ticket ID”文件
应该做的事情。。。。grep-E“^[:alnum:][]文件| head-n1