Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
获取bash脚本中的第一行未注释行(即不以#开头)_Bash_Shell_Git Bash - Fatal编程技术网

获取bash脚本中的第一行未注释行(即不以#开头)

获取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并引入

我正在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并引入了新的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”
    或只是
    grep“Ticket ID”文件
    @RobertoManfreda谢谢你的回答,但如果第一行是新行,我只会得到新行字符。此外,我不能grep ticket id,因为它可以是任何
    grep-E“^[:alnum:][]文件| head-n1
    应该做的事情。。。。