Issue
Hi I try to use Sed in a Bash script and I'm facing a problem. (I'm new to both of them)
I'm trying to insert $log
after the flag <!-- Beginning git log -->
.
#!/bin/bash
log=`git log -1 --stat --date=short --pretty=format:'[ %cd | %an | %s ]'`
sed "/<!-- Beginning git log -->/a $log" ~/opt/prime.dropbox/commit.md
So in commit.md I would have :
some text
<!-- Beginning git log -->
git log output
some text
I have tried all possible single/double quotes tricks,.. or maybe only all the wrong ones.
This is the error I get most of the time.
sed: -e expression #1, char 102: unknown command: `f'
I know there should be an easier way with awk, but I'm so close. ^^
Maybe are the Html chars <!-- -->
blocking or something ?
Thx
Solution
sed is an excellent tool for simple substitutions on a single line, but for anything else, just use awk:
awk -v gitlog="$log" '{print} /<!-- Beginning git log -->/{print gitlog}' ~/opt/prime.dropbox/commit.md
To run a command in shell and safely over-write the original file with that commands output:
the_command orig_file > /usr/tmp/tmp$$ && mv /usr/tmp/tmp$$ orig_file
In this case:
awk -v gitlog="$log" '{print} /<!-- Beginning git log -->/{print gitlog}' ~/opt/prime.dropbox/commit.md > /usr/tmp/tmp$$ &&
mv /usr/tmp/tmp$$ ~/opt/prime.dropbox/commit.md
The temporary file can be in your working directory or in your $HOME or in /usr/tmp or wherever you want and can be named whatever you want.
Answered By - Ed Morton Answer Checked By - Willingham (WPSolving Volunteer)