带有bash的Regex从多行文件中获取特定字符串并存储为var
我有一个工作脚本,它可以在var中存储单个字符串,而不需要回车符、空格或多行文件的表格:git config文件 但它不再工作,因为github更改了该文件的语法 文件当前的外观:带有bash的Regex从多行文件中获取特定字符串并存储为var,bash,shell,sh,Bash,Shell,Sh,我有一个工作脚本,它可以在var中存储单个字符串,而不需要回车符、空格或多行文件的表格:git config文件 但它不再工作,因为github更改了该文件的语法 文件当前的外观: [core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true [remote "origin"] url = git@github.com:user/repo.gi
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@github.com:user/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
#commented line
"this is repo"
"git@github.com:user/repo.git"
我当前用于提取所需字符串的内容:
var3=$(sed 's/^[ \t]*//' .git/config | sed ':a;N;$!ba;s/\n//g' | sed -rn 's/.*https:\/\/(.*?)fetch.*/\1/p')
echo "this is repo"
echo $var3
"this is repo"
""
我希望它返回的内容:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@github.com:user/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*
#commented line
"this is repo"
"git@github.com:user/repo.git"
我得到的:
var3=$(sed 's/^[ \t]*//' .git/config | sed ':a;N;$!ba;s/\n//g' | sed -rn 's/.*https:\/\/(.*?)fetch.*/\1/p')
echo "this is repo"
echo $var3
"this is repo"
""
你可以做:
echo -e "\"this is repo\"\n\"$(sed -n "s/.*url = \(.*\)/\1/p" inputfile.txt)\""
输出:
"this is repo"
"git@github.com:user/repo.git"
sed的命令:sed-ns/*url=\(.*\)/\1/p
将打印以任何(*
)开头,后跟url=
和url的行。匹配行将被URL替换,URL将被打印(通过sed的p
命令) 你可以做:
echo -e "\"this is repo\"\n\"$(sed -n "s/.*url = \(.*\)/\1/p" inputfile.txt)\""
输出:
"this is repo"
"git@github.com:user/repo.git"
sed的命令:
sed-ns/*url=\(.*\)/\1/p
将打印以任何(*
)开头,后跟url=
和url的行。匹配行将被URL替换,URL将被打印(通过sed的p
命令) 据我所知,数据不包含https,因此找不到任何内容。据我所知,代码在bash中不可用:这个正则表达式不是bash
它是sed
,因此shell应该没有什么区别(bash
支持正则表达式,但您不在这里使用它们)。据我所知,数据不包含https
,所以它找不到任何东西。据我所知,代码在bash中不可用:这个正则表达式不是bash
而是sed
,因此shell应该不会有什么区别(bash
不支持正则表达式,但这里不使用它们)。