N
N
Nikita2015-10-05 19:06:49
bash
Nikita, 2015-10-05 19:06:49

How to insert text in last convergence in bash?

Good day! Can you please tell me how to insert a line in the last convergence using bash? For example, you have a file that contains the following text:
one
two
three
two You
want to insert some text at the last occurrence of two in the entire file.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abcd0x00, 2015-10-06
@Caelestius

echo -e "one\ntwo\nthree\ntwo\nfour\ntwo\nfive" | tac | sed '/^two$/ { x; /./! { g; s/.*/xxx\n&/; }; }' | tac

Conclusion
one
two
three
two
four
two
xxx
five

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question