file contents:
# cat file.txt
-----
MATCH
-----
MATCH
-----
MATCH
-----
# cat text.txt
add this text file
before first match
sed commands:
# sed '0,/MATCH/r text.txt' file.txt
-----
add this text file
before first match
MATCH
add this text file
before first match
-----
MATCH
-----
MATCH
-----
# sed '0,/MATCH/i prependme once' file.txt
prependme once
-----
prependme once
MATCH
-----
MATCH
-----
MATCH
-----
I'm trying to merge these commands somehow to get the following output:
-----
add this text file
before first match
MATCH
-----
MATCH
-----
MATCH
-----
With ed
instead of sed
ed -s << EOF file.txt
0,/MATCH/-1 r text.txt
,p
q
EOF
or as a one-liner
printf '0,/MATCH/-1 r text.txt\n,p\nq' | ed -s file.txt
-----
add this text file
before first match
MATCH
-----
MATCH
-----
MATCH
-----
(replace ,p
by w
for in-place editing).
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments