Reputation: 445
I have a file that contain redundant Git statements, like this:
git fetch
git checkout foo
git checkout bar
git checkout baz
git merge origin/baz
The checkout statements are redundant, and I would like to keep only the last one, so that the file ends up looking like this:
git fetch
git checkout baz
git merge origin/baz
I can only use Bash script. How could I go about this?
Upvotes: 1
Views: 197
Reputation: 9417
sed
has a convenient way of printing the last occurrence of a pattern in a line:
sed '/checkout/h; $!d; x' file
Which, in your case would extract the string git checkout baz
.
You can then use this in a script, save the sed
output to a variable, and use it to print out all the lines which you specify:
file=$1
line=$(sed '/checkout/h; $!d; x' $file)
sed -n "/checkout/!p;/$line/p" $file
The last sed
states, print all lines that do not contain checkout
, as well as the one that contains the output from the first sed
command.
Upvotes: 0
Reputation: 161674
Try this awk
command:
tac input.txt | awk '{cmd = $1$2}; cmd != last; {last = cmd}' | tac
result:
git fetch
git checkout baz
git merge origin/baz
Upvotes: 1