Tuesday, July 26, 2022

[SOLVED] Pipe git command to sed output

Issue

I am trying to pipe output from git to sed, which will inturn replace a value in file.config that represents the last commit on a branch.

I am unsure how to do this using piping in bash. I thought maybe something like this? But it doesnt work (outputs nothing)

git rev-parse --short HEAD | sed -i 's/"commit".*:.*".*"/"commit": "${$_}"/g' file.config

Is this even possible, or is there a better approach I can take?

Contents of file.config

{
    "commit" : "684e8ba31"
}

Contents of file.config (after running command)

{
    "commit" : "${$_}"
}

Contents of file.config (expected output)

{
    "commit" : "441d6fc22"
}

Ouput from git rev-parse --short HEAD

441d6fc22

Solution

sed -i '/"commit" *:/s/: *"[^"]*"/: "'$(git rev-parse HEAD)'"/' file.config

will do it. You're confusing command-line arguments with stdin input. They're both good and widely-used ways of feeding the invoked program data but the differences in capacity and timing matter, so spend more time studying how they work.



Answered By - jthill
Answer Checked By - David Marino (WPSolving Volunteer)