0

I had a working script which could store in a var a single string without carriage returns or spaces or tabulations of a multi-line file: the git config file.

But it no longer works as github has changed that file's syntax.

What the file currently looks like :

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = [email protected]:user/repo.git
    fetch = +refs/heads/*:refs/remotes/origin/*
#commented line

What I'm currently using to extract the desired string :

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

What I want it to return :

"this is repo"
"[email protected]:user/repo.git"

What I get :

"this is repo"
""
1
  • 1
    So far as I can see the data does not contain https, so it won't find anything. code isn't usable so far as I can tell with bash. :this regex is not bash it is sed, so the shell should make little or no difference (bash does support regular expressions, but you are not using them here). Commented May 9, 2018 at 16:36

1 Answer 1

2

You could do:

echo -e "\"this is repo\"\n\"$(sed -n "s/.*url = \(.*\)/\1/p" inputfile.txt)\""

output:

"this is repo"
"[email protected]:user/repo.git"

sed's command : sed -n s/.*url = \(.*\)/\1/p will print lines that start with anything (.*) followed by url = followed by an URL. Matching lines will get replaced by the URL, which will get printed (by sed's p command).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.