how to insert file contents on match using sed - first occurrence only

Zell

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
-----
steeldriver

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove only the first occurrence of a line in a file using sed

From Dev

How can I get sed to only match to the first occurrence of a character?

From Dev

How to *return* *only* the nth occurrence of a regex match using sed?

From Dev

How to use sed to replace only the first occurrence?

From Dev

How to match only first occurrence of space at line

From Java

Insert line after first match using sed

From Dev

Want to replace a string only at first occurrence using sed

From Dev

Match only first occurrence of digit

From Dev

Match only the first occurrence of a phrase

From Dev

Match only the first occurrence of a phrase

From Dev

sed - trying to replace first occurrence after a match

From Dev

Insert contents of file at given line for multiple files, using sed

From Dev

Insert contents of file at given line for multiple files, using sed

From Dev

replace only the first and only the last match using 'sed'

From Dev

Sed insert before first match

From Dev

How to replace first n no. of occurrence of a string using sed and/or awk?

From Dev

Insert character after first match using sed or awk

From Dev

Sed : replace words only with first occurrence of string in the line not till last match

From Dev

How to replace every nth occurrence of pattern using sed ONLY

From Dev

how to match first regex occurrence using grok filter

From Dev

regex replace only the first occurrence of every match

From Dev

regex replace only the first occurrence of every match

From Dev

How can I change the third occurrence of a word in a file using sed?

From Dev

How do I have sed only perform actions on the first match?

From Dev

Delete till first occurrence of colon using sed

From Dev

Append text to first occurrence of a pattern using sed

From Dev

Delete till first occurrence of pipe "|" using sed

From Dev

Finding 'first occurrence' using Match Function in R

From Dev

How to comment out contents in xml file using sed?

Related Related

  1. 1

    How to remove only the first occurrence of a line in a file using sed

  2. 2

    How can I get sed to only match to the first occurrence of a character?

  3. 3

    How to *return* *only* the nth occurrence of a regex match using sed?

  4. 4

    How to use sed to replace only the first occurrence?

  5. 5

    How to match only first occurrence of space at line

  6. 6

    Insert line after first match using sed

  7. 7

    Want to replace a string only at first occurrence using sed

  8. 8

    Match only first occurrence of digit

  9. 9

    Match only the first occurrence of a phrase

  10. 10

    Match only the first occurrence of a phrase

  11. 11

    sed - trying to replace first occurrence after a match

  12. 12

    Insert contents of file at given line for multiple files, using sed

  13. 13

    Insert contents of file at given line for multiple files, using sed

  14. 14

    replace only the first and only the last match using 'sed'

  15. 15

    Sed insert before first match

  16. 16

    How to replace first n no. of occurrence of a string using sed and/or awk?

  17. 17

    Insert character after first match using sed or awk

  18. 18

    Sed : replace words only with first occurrence of string in the line not till last match

  19. 19

    How to replace every nth occurrence of pattern using sed ONLY

  20. 20

    how to match first regex occurrence using grok filter

  21. 21

    regex replace only the first occurrence of every match

  22. 22

    regex replace only the first occurrence of every match

  23. 23

    How can I change the third occurrence of a word in a file using sed?

  24. 24

    How do I have sed only perform actions on the first match?

  25. 25

    Delete till first occurrence of colon using sed

  26. 26

    Append text to first occurrence of a pattern using sed

  27. 27

    Delete till first occurrence of pipe "|" using sed

  28. 28

    Finding 'first occurrence' using Match Function in R

  29. 29

    How to comment out contents in xml file using sed?

HotTag

Archive