replace the line to add some text - sed

The MySQL dump backup file has the following line...
# head -40 backup20-Apr-2010-07-32.sql | grep 'CHANGE MASTER TO '
-- CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000068', MASTER_LOG_POS=176357756;
a) I need to complete the statement with the parameters like Master host, user and password.
b) I do also need to remove the comment "--"
The line should look something like this...
CHANGE MASTER TO MASTER_HOST='111.222.333.444', MASTER_USER='slave_user', MASTER_PASSWORD='slave_user', MASTER_LOG_FILE='mysql-bin.000068', MASTER_LOG_POS=176357756;

head -40 backup20-Apr-2010-07-32.sql |
grep 'CHANGE MASTER TO ' |
perl -lpe "s/-- CHANGE MASTER TO/CHANGE MASTER TO MASTER_HOST='111.222.333.444', MASTER_USER='slave_user', MASTER_PASSWORD='slave_user',/"

head -40 ORS20-Apr-2010-07-32.sql | grep 'CHANGE MASTER TO ' | sed 's/CHANGE MASTER TO/CHANGE MASTER TO MASTER_HOST='111.222.333.444', MASTER_USER='slave_user', MASTER_PASSWORD='slave_user'/g' | sed 's/^--//g'
Is it the correct way to do this?

#!/bin/bash
s="MASTER_HOST='111.222.333.444', MASTER_USER='slave_user',"
awk -vs="$s" ' /-- CHANGE MASTER TO/{
gsub("-- CHANGE MASTER TO","CHANGE MASTER TO "s)
}1 ' file >temp
mv temp file

Related

How would I generate list of dates and git commits?

I wrote some code that generates a github contributions-style heatmap in the terminal given a csv file that contains timestamps and some unsigned value.
I'd like to generate a csv that contains dates and the number of github contributions I made on that date.
Is there a simple way to do this?
You could use git log and a custom format:
git log --date=short --format="%an %ad [%h] %s" | cut -d ' ' -f1 -f2 -f3 -f4- | sed -E 's/ /,/' | sed -E 's/ /,/' | sed -E 's/ /,/'
I get:
Lachlan,Miller,2019-03-25,[e20b847] Rename method
Lachlan,Miller,2019-03-25,[6c47dbf] Add a POC using JS
lmiller1990,2018-04-12,[c295307],Add song class
lmiller1990,2018-04-12,[876cbe2],Add timer
You could use grep for this job. Also, flags like i, A and color will help you cleaning things up a bit. Also, output the result in a .csv file using >
use man grep to know a more about its flags.
Try using:
git log | grep -E -A 2 --color "commit|Date" > output.csv
You could also add --summary flag to log.

How can I get this bash sub shell to work in Fish?

I'm trying to get this command line working in Fish.
git show $(git log --pretty=oneline | fzf | cut -d ' ' -f1)
What is supposed to happen is git log --pretty=oneline | fzf | cut -d ' ' -f1 lets you select a commit interactively from git log and then returns the commit hash which is passed to git show.
I thought Fish uses parentheses for "subcommands" but this doesn't work.
git show (git log --pretty=oneline | fzf | cut -d ' ' -f1)
It goes straight to the default output of git show which is the HEAD commit.
I suspect my idea of how the shell works is incorrect. Any help appreciated.
UPDATE
This is the output from the pipeline
$ git log --pretty=oneline | fzf | cut -d ' ' -f1
3eb7a8fa09ac94cf4a76109b896f7ba58959f5a8
UPDATE 2
As answered by #faho, this is a bug in Fish.
You can workaround for now by using a tempfile
git log --pretty=oneline | fzf | cut -d ' ' -f1 > $TMPDIR/fzf.result; and git show (cat $TMPDIR/fzf.result)`
Or, more succinctly using xargs
git log --pretty=oneline | fzf | cut -d ' ' -f1 | xargs -o git show
This is fish issue #1362, which is also mentioned in fzf's readme.
There's an easy workaround: Instead of a command substitution, use read, like
git log --pretty=oneline | fzf | cut -d ' ' -f 1 | read -l answer
git show $answer
(fzf currently uses a tempfile in its fish bindings, but I'm working on rectifying that)

How to open modified in git file for edit in sublime

The use case is the following
you modified some files and git status shows them to you
you want to edit one of those files in Sublime
you feel hacky and want to do this from command line
using Vim you can do the following:
vim 'git status --porcelain | grep "myfile.txt" | cut -c 4-'
I use Sublime and Windows and don't know how to accomplish the same. Sublime seems to have --command command, but I can't get it to work. So the question is how to do that? (I am okay with powershell)
In powershell you can do it like that:
sublime $(git status --porcelain | grep "myfile.txt" | cut -c 4-)
Open all modified/deleted/new files:
vim `git status --porcelain | awk '{print $2}'`

How to display only the last field?

I used
cut -d " " -f 8
and
awk '{print $8}'
But this assumes that the 8th field is the last one, which is not always true.
How can I display the last field in a shell script?
Try doing this :
$ awk '{print $NF}'
or the funny
$ echo "foo bar base" | rev | cut -d ' ' -f1 | rev
base
Edit: this answer is wrong now because the question changed. Move along, nothing to see here.
You can use tail to print a specified number of bytes from the end of the input
tail -c 1

How to determine date/time when a file was first added to CVS repository

Is there any simple cvs command through which I can get date/time when a file was first added to the module in CVS repository.
I actually want a one line output that can be consumed by my some script.
This is not directly possible in CVS. One can get activity logs for a file and then identify the date from them. Following is the single line command that works like a charm and gives the date when the file was first added in the repository.
cvs -Q -d :pserver:*User*:*Pass*#*HostName*:/cvsroot rlog -N *FilePath* | grep ^date: | sort | head -n 1 | cut -d\; -f1 | sed -e 's/date: //'
Above command looks through the entire repository and gives the date. If one is looking for first activity on that file on a branch use following commands.
For Branch:
cvs -Q -d :pserver:*User*:*Pass*#*HostName*:/cvsroot rlog -N -r*BranchName* *FilePath* | grep ^date: | sort | head -n 1 | cut -d\; -f1 | sed -e 's/date: //'
For Trunk:
cvs -Q -d :pserver:*User*:*Pass*#*HostName*:/cvsroot rlog -N -r::HEAD *FilePath* | grep ^date: | sort | head -n 1 | cut -d\; -f1 | sed -e 's/date: //'