如何从外壳中的“更新2f8b547d..eb94967a快速转发...”日志中获取“ 2f8b547d..eb94967a”字符串?

我正在构建一个shell脚本。

该脚本获取git日志,例如:

"Updating 2f8b547d..eb94967a Fast-forward...."

but I want to get 2f8b547d..eb94967a snippet.

我是新手。因此,感谢您的帮助。

更新:

For the more, I want use the snippet as a param. Because I will excute git log 2f8b547d..eb94967a

评论
  • lmagni
    lmagni 回复

    As an alternative to awk (don't get me wrong, awk is super for this job as well), you can simply use cut with a space delimiter extract the second field, e.g.

    cut -d' ' -f2 yourgit.log
    

    You can also pipe output to cut or redirect the input file to it using < as well. It essentially does the same as the awk command, it just being a different choice of which utility to use.

  • 渗透的葬礼
    渗透的葬礼 回复

    You can pipe it to awk like so:

    echo "Updating 2f8b547d..eb94967a Fast-forward...." | awk '{print $2}'
    

    Your result will be 2f8b547d..eb94967a.

    如果它是一个脚本(例如,abc.sh)具有这样的输出,则可以运行:

    $> ./abc.sh | awk '{print $2}'
    

    awk takes the output and splits the information by space. Updating is represented with $1. 2f8b547d..eb94967a is $2 and so on. In the above example, we ask awk to print out the 2nd item in the output.