R
R
Rustem Vorozheikin2015-06-01 01:27:34
bash
Rustem Vorozheikin, 2015-06-01 01:27:34

How to use a bash script/command to insert a line before another line?

Good afternoon.
There is a t.txt file with the content:

AAA
BBB
DDD
EEE

How can I insert the string "CCC" between "BBB" and "DDD" using a bash script/command?
Can you advise how to implement this? The main nuance is that the file has already been created with certain content and, therefore, the command
echo "
AAA
BBB
CCC
DDD
EEE" > t.txt
the issue is not resolved)
Thanks in advance)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2015-06-01
@Te7ris

# после паттерна
sed -i.bak '/BBB/a CCC' /path/to/file

# перед паттерном
sed -i.bak '/BBB/i CCC' /path/to/file

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question