Inserting file into other file after pattern [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
This question already has an answer here:
Inserting a file into another file after the 1st occurrence of a pattern
1 answer
How might I insert the content of the file a
, followed by a newline,
into the file b
, after the first line containing PATTERN
?
text-processing
marked as duplicate by don_crissti, RalfFriedl, Thomas, ñÃÂsýù÷, Goro yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
This question already has an answer here:
Inserting a file into another file after the 1st occurrence of a pattern
1 answer
How might I insert the content of the file a
, followed by a newline,
into the file b
, after the first line containing PATTERN
?
text-processing
marked as duplicate by don_crissti, RalfFriedl, Thomas, ñÃÂsýù÷, Goro yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
Inserting a file into another file after the 1st occurrence of a pattern
1 answer
How might I insert the content of the file a
, followed by a newline,
into the file b
, after the first line containing PATTERN
?
text-processing
This question already has an answer here:
Inserting a file into another file after the 1st occurrence of a pattern
1 answer
How might I insert the content of the file a
, followed by a newline,
into the file b
, after the first line containing PATTERN
?
This question already has an answer here:
Inserting a file into another file after the 1st occurrence of a pattern
1 answer
text-processing
text-processing
asked yesterday
Toothrot
781517
781517
marked as duplicate by don_crissti, RalfFriedl, Thomas, ñÃÂsýù÷, Goro yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by don_crissti, RalfFriedl, Thomas, ñÃÂsýù÷, Goro yesterday
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
sed '/pattern/r file_b
a
:f n; b f ' file_a
If file_b
is already terminated by a newline, and you don't want an empty line in the output, omit the a
line from the script.
On the last line, the :f
defines a label, n
reads a newline (which will be automatically printed since the -n
option was not given to sed
), and b f
branches to the f
label, creating a loop. All this in order to append file_b
only after the first line matching pattern. If you want to append file_b
after each line matching pattern
, it's much simpler:
sed '/pattern/r file_b' file_a
Example:
$ cat file_a
first
second
third
fourth
first
second
third
fourth
$ cat file_b
b1
b2
b3
$ sed '/second/r file_b
:f n; b f ' file_a
first
second
b1
b2
b3
third
fourth
first
second
third
fourth
$ printf "xnynz" >file_c
$ sed '/second/r file_c
a
:f n; b f ' file_a
first
second
x
y
z
third
fourth
first
second
third
fourth
$ sed '/second/r file_b' file_a
first
second
b1
b2
b3
third
fourth
first
second
b1
b2
b3
third
fourth
Another solution would be using ed(1)
(which surprisingly isn't available by default in modern linux distributions, despite being mandated by POSIX and present in all unix systems since 45 years or so):
echo '/pattern/r file_b
w' | ed file_a
This will edit file_a
in place and append a newline if there's any at the end of file_b
; if you want the output written to another file, change the w
to w output_file
.
Thanks. Could you please explain the last line?
â Toothrot
yesterday
So the loop is necessary because there is no exit command?
â Toothrot
yesterday
1
No, there is an exit command:q
. But if you use it, no line that comes afterpattern
infile_a
will be printed.
â mosvy
yesterday
add a comment |Â
up vote
0
down vote
How about
sed '/PATTERN/r a' b
You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
â ilkkachu
yesterday
This is a subset of mosvyâÂÂs answer.âÂÂIf you believe that there are problems with that other (earlier) answer that your post resolves, explain that.â (Pleaseâ¯doâ¯not respond inâ¯comments; editâ¯your answer toâ¯make it clearer andâ¯more complete.)âÂÂOtherwise, this answer would appear to add no new information.
â G-Man
yesterday
2
@G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
â don_crissti
yesterday
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
sed '/pattern/r file_b
a
:f n; b f ' file_a
If file_b
is already terminated by a newline, and you don't want an empty line in the output, omit the a
line from the script.
On the last line, the :f
defines a label, n
reads a newline (which will be automatically printed since the -n
option was not given to sed
), and b f
branches to the f
label, creating a loop. All this in order to append file_b
only after the first line matching pattern. If you want to append file_b
after each line matching pattern
, it's much simpler:
sed '/pattern/r file_b' file_a
Example:
$ cat file_a
first
second
third
fourth
first
second
third
fourth
$ cat file_b
b1
b2
b3
$ sed '/second/r file_b
:f n; b f ' file_a
first
second
b1
b2
b3
third
fourth
first
second
third
fourth
$ printf "xnynz" >file_c
$ sed '/second/r file_c
a
:f n; b f ' file_a
first
second
x
y
z
third
fourth
first
second
third
fourth
$ sed '/second/r file_b' file_a
first
second
b1
b2
b3
third
fourth
first
second
b1
b2
b3
third
fourth
Another solution would be using ed(1)
(which surprisingly isn't available by default in modern linux distributions, despite being mandated by POSIX and present in all unix systems since 45 years or so):
echo '/pattern/r file_b
w' | ed file_a
This will edit file_a
in place and append a newline if there's any at the end of file_b
; if you want the output written to another file, change the w
to w output_file
.
Thanks. Could you please explain the last line?
â Toothrot
yesterday
So the loop is necessary because there is no exit command?
â Toothrot
yesterday
1
No, there is an exit command:q
. But if you use it, no line that comes afterpattern
infile_a
will be printed.
â mosvy
yesterday
add a comment |Â
up vote
2
down vote
accepted
sed '/pattern/r file_b
a
:f n; b f ' file_a
If file_b
is already terminated by a newline, and you don't want an empty line in the output, omit the a
line from the script.
On the last line, the :f
defines a label, n
reads a newline (which will be automatically printed since the -n
option was not given to sed
), and b f
branches to the f
label, creating a loop. All this in order to append file_b
only after the first line matching pattern. If you want to append file_b
after each line matching pattern
, it's much simpler:
sed '/pattern/r file_b' file_a
Example:
$ cat file_a
first
second
third
fourth
first
second
third
fourth
$ cat file_b
b1
b2
b3
$ sed '/second/r file_b
:f n; b f ' file_a
first
second
b1
b2
b3
third
fourth
first
second
third
fourth
$ printf "xnynz" >file_c
$ sed '/second/r file_c
a
:f n; b f ' file_a
first
second
x
y
z
third
fourth
first
second
third
fourth
$ sed '/second/r file_b' file_a
first
second
b1
b2
b3
third
fourth
first
second
b1
b2
b3
third
fourth
Another solution would be using ed(1)
(which surprisingly isn't available by default in modern linux distributions, despite being mandated by POSIX and present in all unix systems since 45 years or so):
echo '/pattern/r file_b
w' | ed file_a
This will edit file_a
in place and append a newline if there's any at the end of file_b
; if you want the output written to another file, change the w
to w output_file
.
Thanks. Could you please explain the last line?
â Toothrot
yesterday
So the loop is necessary because there is no exit command?
â Toothrot
yesterday
1
No, there is an exit command:q
. But if you use it, no line that comes afterpattern
infile_a
will be printed.
â mosvy
yesterday
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
sed '/pattern/r file_b
a
:f n; b f ' file_a
If file_b
is already terminated by a newline, and you don't want an empty line in the output, omit the a
line from the script.
On the last line, the :f
defines a label, n
reads a newline (which will be automatically printed since the -n
option was not given to sed
), and b f
branches to the f
label, creating a loop. All this in order to append file_b
only after the first line matching pattern. If you want to append file_b
after each line matching pattern
, it's much simpler:
sed '/pattern/r file_b' file_a
Example:
$ cat file_a
first
second
third
fourth
first
second
third
fourth
$ cat file_b
b1
b2
b3
$ sed '/second/r file_b
:f n; b f ' file_a
first
second
b1
b2
b3
third
fourth
first
second
third
fourth
$ printf "xnynz" >file_c
$ sed '/second/r file_c
a
:f n; b f ' file_a
first
second
x
y
z
third
fourth
first
second
third
fourth
$ sed '/second/r file_b' file_a
first
second
b1
b2
b3
third
fourth
first
second
b1
b2
b3
third
fourth
Another solution would be using ed(1)
(which surprisingly isn't available by default in modern linux distributions, despite being mandated by POSIX and present in all unix systems since 45 years or so):
echo '/pattern/r file_b
w' | ed file_a
This will edit file_a
in place and append a newline if there's any at the end of file_b
; if you want the output written to another file, change the w
to w output_file
.
sed '/pattern/r file_b
a
:f n; b f ' file_a
If file_b
is already terminated by a newline, and you don't want an empty line in the output, omit the a
line from the script.
On the last line, the :f
defines a label, n
reads a newline (which will be automatically printed since the -n
option was not given to sed
), and b f
branches to the f
label, creating a loop. All this in order to append file_b
only after the first line matching pattern. If you want to append file_b
after each line matching pattern
, it's much simpler:
sed '/pattern/r file_b' file_a
Example:
$ cat file_a
first
second
third
fourth
first
second
third
fourth
$ cat file_b
b1
b2
b3
$ sed '/second/r file_b
:f n; b f ' file_a
first
second
b1
b2
b3
third
fourth
first
second
third
fourth
$ printf "xnynz" >file_c
$ sed '/second/r file_c
a
:f n; b f ' file_a
first
second
x
y
z
third
fourth
first
second
third
fourth
$ sed '/second/r file_b' file_a
first
second
b1
b2
b3
third
fourth
first
second
b1
b2
b3
third
fourth
Another solution would be using ed(1)
(which surprisingly isn't available by default in modern linux distributions, despite being mandated by POSIX and present in all unix systems since 45 years or so):
echo '/pattern/r file_b
w' | ed file_a
This will edit file_a
in place and append a newline if there's any at the end of file_b
; if you want the output written to another file, change the w
to w output_file
.
edited yesterday
answered yesterday
mosvy
2,002110
2,002110
Thanks. Could you please explain the last line?
â Toothrot
yesterday
So the loop is necessary because there is no exit command?
â Toothrot
yesterday
1
No, there is an exit command:q
. But if you use it, no line that comes afterpattern
infile_a
will be printed.
â mosvy
yesterday
add a comment |Â
Thanks. Could you please explain the last line?
â Toothrot
yesterday
So the loop is necessary because there is no exit command?
â Toothrot
yesterday
1
No, there is an exit command:q
. But if you use it, no line that comes afterpattern
infile_a
will be printed.
â mosvy
yesterday
Thanks. Could you please explain the last line?
â Toothrot
yesterday
Thanks. Could you please explain the last line?
â Toothrot
yesterday
So the loop is necessary because there is no exit command?
â Toothrot
yesterday
So the loop is necessary because there is no exit command?
â Toothrot
yesterday
1
1
No, there is an exit command:
q
. But if you use it, no line that comes after pattern
in file_a
will be printed.â mosvy
yesterday
No, there is an exit command:
q
. But if you use it, no line that comes after pattern
in file_a
will be printed.â mosvy
yesterday
add a comment |Â
up vote
0
down vote
How about
sed '/PATTERN/r a' b
You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
â ilkkachu
yesterday
This is a subset of mosvyâÂÂs answer.âÂÂIf you believe that there are problems with that other (earlier) answer that your post resolves, explain that.â (Pleaseâ¯doâ¯not respond inâ¯comments; editâ¯your answer toâ¯make it clearer andâ¯more complete.)âÂÂOtherwise, this answer would appear to add no new information.
â G-Man
yesterday
2
@G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
â don_crissti
yesterday
add a comment |Â
up vote
0
down vote
How about
sed '/PATTERN/r a' b
You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
â ilkkachu
yesterday
This is a subset of mosvyâÂÂs answer.âÂÂIf you believe that there are problems with that other (earlier) answer that your post resolves, explain that.â (Pleaseâ¯doâ¯not respond inâ¯comments; editâ¯your answer toâ¯make it clearer andâ¯more complete.)âÂÂOtherwise, this answer would appear to add no new information.
â G-Man
yesterday
2
@G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
â don_crissti
yesterday
add a comment |Â
up vote
0
down vote
up vote
0
down vote
How about
sed '/PATTERN/r a' b
How about
sed '/PATTERN/r a' b
answered yesterday
RudiC
1,86719
1,86719
You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
â ilkkachu
yesterday
This is a subset of mosvyâÂÂs answer.âÂÂIf you believe that there are problems with that other (earlier) answer that your post resolves, explain that.â (Pleaseâ¯doâ¯not respond inâ¯comments; editâ¯your answer toâ¯make it clearer andâ¯more complete.)âÂÂOtherwise, this answer would appear to add no new information.
â G-Man
yesterday
2
@G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
â don_crissti
yesterday
add a comment |Â
You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
â ilkkachu
yesterday
This is a subset of mosvyâÂÂs answer.âÂÂIf you believe that there are problems with that other (earlier) answer that your post resolves, explain that.â (Pleaseâ¯doâ¯not respond inâ¯comments; editâ¯your answer toâ¯make it clearer andâ¯more complete.)âÂÂOtherwise, this answer would appear to add no new information.
â G-Man
yesterday
2
@G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
â don_crissti
yesterday
You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
â ilkkachu
yesterday
You might want to add some explanation to that. Answers with just code and little text can get automatically marked as "low quality"
â ilkkachu
yesterday
This is a subset of mosvyâÂÂs answer.âÂÂIf you believe that there are problems with that other (earlier) answer that your post resolves, explain that.â (Pleaseâ¯doâ¯not respond inâ¯comments; editâ¯your answer toâ¯make it clearer andâ¯more complete.)âÂÂOtherwise, this answer would appear to add no new information.
â G-Man
yesterday
This is a subset of mosvyâÂÂs answer.âÂÂIf you believe that there are problems with that other (earlier) answer that your post resolves, explain that.â (Pleaseâ¯doâ¯not respond inâ¯comments; editâ¯your answer toâ¯make it clearer andâ¯more complete.)âÂÂOtherwise, this answer would appear to add no new information.
â G-Man
yesterday
2
2
@G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
â don_crissti
yesterday
@G-Man - fyi the other answer was deleted (so not visible to users with < 10K rep) at the time RudiC posted this answer.
â don_crissti
yesterday
add a comment |Â