How can I add 10 lines from a file (file2) to another one after 2 lines (file1)?
Clash Royale CLAN TAG#URR8PPP
I have two different files separated by Tab
. File 1 looks like this:
transcr_15824 3.95253441295071 3.99992738843234 3.93880798313547
YML042W 10.3143219248979 10.6898819949325 11.0073811719421
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YCR105W 8.46144110056843 8.30995100411912 8.85470858413405
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YMR325W 6.2822794040082 6.46992587787936 7.00507748994596
File 2 looks like this:
YLR177W 11.321823973245 12.1264440368589 11.7777091957438
YOR117W 10.7514234580732 11.3932687209745 11.2587694561818
TY_120 5.95114867088525 5.93580053538449 5.89166059690558
YMR174C 8.49545850099485 8.72467418433346 9.6518559706269
YPL117C 10.7211879012765 10.5046713289602 10.6145538571844
TY2_LTR_77 11.9297940548212 11.9801206538102 12.049127298122
YOL101C 7.76141097131674 9.89522697916433 7.85466704627526
YLR053C 7.62843998411388 7.49205634213499 7.10263942962051
YBR135W 9.70614244227352 9.3114074341804 9.36413815370247
YNL168C 9.93928326709444 10.3036524361223 10.0704544058998
What I'm trying to do right now is to add 10 lines from File 2
to File 1
after 2 lines. It should look like this:
transcr_15824 3.95253441295071 3.99992738843234 3.93880798313547
YML042W 10.3143219248979 10.6898819949325 11.0073811719421
YLR177W 11.321823973245 12.1264440368589 11.7777091957438
YOR117W 10.7514234580732 11.3932687209745 11.2587694561818
TY_120 5.95114867088525 5.93580053538449 5.89166059690558
YMR174C 8.49545850099485 8.72467418433346 9.6518559706269
YPL117C 10.7211879012765 10.5046713289602 10.6145538571844
TY2_LTR_77 11.9297940548212 11.9801206538102 12.049127298122
YOL101C 7.76141097131674 9.89522697916433 7.85466704627526
YLR053C 7.62843998411388 7.49205634213499 7.10263942962051
YBR135W 9.70614244227352 9.3114074341804 9.36413815370247
YNL168C 9.93928326709444 10.3036524361223 10.0704544058998
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YCR105W 8.46144110056843 8.30995100411912 8.85470858413405
So, basically, I'm trying to move 10 lines from File 2
between each transcr_
keeping the already existent line that is already below each transcr_
.
Edit:
File 2
has around 2,000 lines and File 1
has around 200 "transcr_" rows. So, it would be: pick up the first 10 lines of File 2
, put them between the first and the second "transcr_" rows (and after the already existing line between those two "transcr_". Then, get the lines from 11 to 20 from File 2
and put them between the second and the third "transcr_". Then, get the lines from 21 to 30 from File 2
and put them between the third and the fourth "transcr_" and so on.
It may look like this:
transcr_1
already existing line
10 first lines from `File 2`
transcr_2
already existing line
Lines 11-20 from `File 2`
transcr_3
already existing line
Lines 21-30 from `File 2`
transcr_4
.....
text-processing
|
show 3 more comments
I have two different files separated by Tab
. File 1 looks like this:
transcr_15824 3.95253441295071 3.99992738843234 3.93880798313547
YML042W 10.3143219248979 10.6898819949325 11.0073811719421
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YCR105W 8.46144110056843 8.30995100411912 8.85470858413405
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YMR325W 6.2822794040082 6.46992587787936 7.00507748994596
File 2 looks like this:
YLR177W 11.321823973245 12.1264440368589 11.7777091957438
YOR117W 10.7514234580732 11.3932687209745 11.2587694561818
TY_120 5.95114867088525 5.93580053538449 5.89166059690558
YMR174C 8.49545850099485 8.72467418433346 9.6518559706269
YPL117C 10.7211879012765 10.5046713289602 10.6145538571844
TY2_LTR_77 11.9297940548212 11.9801206538102 12.049127298122
YOL101C 7.76141097131674 9.89522697916433 7.85466704627526
YLR053C 7.62843998411388 7.49205634213499 7.10263942962051
YBR135W 9.70614244227352 9.3114074341804 9.36413815370247
YNL168C 9.93928326709444 10.3036524361223 10.0704544058998
What I'm trying to do right now is to add 10 lines from File 2
to File 1
after 2 lines. It should look like this:
transcr_15824 3.95253441295071 3.99992738843234 3.93880798313547
YML042W 10.3143219248979 10.6898819949325 11.0073811719421
YLR177W 11.321823973245 12.1264440368589 11.7777091957438
YOR117W 10.7514234580732 11.3932687209745 11.2587694561818
TY_120 5.95114867088525 5.93580053538449 5.89166059690558
YMR174C 8.49545850099485 8.72467418433346 9.6518559706269
YPL117C 10.7211879012765 10.5046713289602 10.6145538571844
TY2_LTR_77 11.9297940548212 11.9801206538102 12.049127298122
YOL101C 7.76141097131674 9.89522697916433 7.85466704627526
YLR053C 7.62843998411388 7.49205634213499 7.10263942962051
YBR135W 9.70614244227352 9.3114074341804 9.36413815370247
YNL168C 9.93928326709444 10.3036524361223 10.0704544058998
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YCR105W 8.46144110056843 8.30995100411912 8.85470858413405
So, basically, I'm trying to move 10 lines from File 2
between each transcr_
keeping the already existent line that is already below each transcr_
.
Edit:
File 2
has around 2,000 lines and File 1
has around 200 "transcr_" rows. So, it would be: pick up the first 10 lines of File 2
, put them between the first and the second "transcr_" rows (and after the already existing line between those two "transcr_". Then, get the lines from 11 to 20 from File 2
and put them between the second and the third "transcr_". Then, get the lines from 21 to 30 from File 2
and put them between the third and the fourth "transcr_" and so on.
It may look like this:
transcr_1
already existing line
10 first lines from `File 2`
transcr_2
already existing line
Lines 11-20 from `File 2`
transcr_3
already existing line
Lines 21-30 from `File 2`
transcr_4
.....
text-processing
1
Welcome on U&L! Have you got only two files, or more files likeFile 2
to be merged intoFile 1
? Should the whole content ofFile 2
be added toFile 1
? Or a part of it only? Should the content ofFile 2
be added after a specific occurrence oftranscr_
, or after all of them?
– fra-san
Dec 13 at 14:01
2
there are three "transcr_" lines in file1; do you want the same 10 lines from file2 after each of those transcr_ lines, or ... subsequent 10 lines from file2 each time, or ???
– Jeff Schaller
Dec 13 at 14:02
Sorry for not being clear, I need the subsequent 10 lines from file 2. File 2 has more than 2,000 lines
– Lucas Farinazzo Marques
Dec 13 at 17:51
Sorry for not being clear, I need the subsequent 10 lines from file 2. File 2 has more than 2,000 lines. I would need the first ten rows ofFile 2
to be moved between 1st transcr_ and 2nd transcr_, then the next 10 to be moved between 2nd transcr_ and 3rd transcr_ and so on
– Lucas Farinazzo Marques
Dec 13 at 17:59
1
Hi @LucasFarinazzoMarques. Please edit your question to include what you're actually trying to achieve. Currently, your question seems like it has an answer.
– zarose
Dec 13 at 18:07
|
show 3 more comments
I have two different files separated by Tab
. File 1 looks like this:
transcr_15824 3.95253441295071 3.99992738843234 3.93880798313547
YML042W 10.3143219248979 10.6898819949325 11.0073811719421
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YCR105W 8.46144110056843 8.30995100411912 8.85470858413405
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YMR325W 6.2822794040082 6.46992587787936 7.00507748994596
File 2 looks like this:
YLR177W 11.321823973245 12.1264440368589 11.7777091957438
YOR117W 10.7514234580732 11.3932687209745 11.2587694561818
TY_120 5.95114867088525 5.93580053538449 5.89166059690558
YMR174C 8.49545850099485 8.72467418433346 9.6518559706269
YPL117C 10.7211879012765 10.5046713289602 10.6145538571844
TY2_LTR_77 11.9297940548212 11.9801206538102 12.049127298122
YOL101C 7.76141097131674 9.89522697916433 7.85466704627526
YLR053C 7.62843998411388 7.49205634213499 7.10263942962051
YBR135W 9.70614244227352 9.3114074341804 9.36413815370247
YNL168C 9.93928326709444 10.3036524361223 10.0704544058998
What I'm trying to do right now is to add 10 lines from File 2
to File 1
after 2 lines. It should look like this:
transcr_15824 3.95253441295071 3.99992738843234 3.93880798313547
YML042W 10.3143219248979 10.6898819949325 11.0073811719421
YLR177W 11.321823973245 12.1264440368589 11.7777091957438
YOR117W 10.7514234580732 11.3932687209745 11.2587694561818
TY_120 5.95114867088525 5.93580053538449 5.89166059690558
YMR174C 8.49545850099485 8.72467418433346 9.6518559706269
YPL117C 10.7211879012765 10.5046713289602 10.6145538571844
TY2_LTR_77 11.9297940548212 11.9801206538102 12.049127298122
YOL101C 7.76141097131674 9.89522697916433 7.85466704627526
YLR053C 7.62843998411388 7.49205634213499 7.10263942962051
YBR135W 9.70614244227352 9.3114074341804 9.36413815370247
YNL168C 9.93928326709444 10.3036524361223 10.0704544058998
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YCR105W 8.46144110056843 8.30995100411912 8.85470858413405
So, basically, I'm trying to move 10 lines from File 2
between each transcr_
keeping the already existent line that is already below each transcr_
.
Edit:
File 2
has around 2,000 lines and File 1
has around 200 "transcr_" rows. So, it would be: pick up the first 10 lines of File 2
, put them between the first and the second "transcr_" rows (and after the already existing line between those two "transcr_". Then, get the lines from 11 to 20 from File 2
and put them between the second and the third "transcr_". Then, get the lines from 21 to 30 from File 2
and put them between the third and the fourth "transcr_" and so on.
It may look like this:
transcr_1
already existing line
10 first lines from `File 2`
transcr_2
already existing line
Lines 11-20 from `File 2`
transcr_3
already existing line
Lines 21-30 from `File 2`
transcr_4
.....
text-processing
I have two different files separated by Tab
. File 1 looks like this:
transcr_15824 3.95253441295071 3.99992738843234 3.93880798313547
YML042W 10.3143219248979 10.6898819949325 11.0073811719421
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YCR105W 8.46144110056843 8.30995100411912 8.85470858413405
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YMR325W 6.2822794040082 6.46992587787936 7.00507748994596
File 2 looks like this:
YLR177W 11.321823973245 12.1264440368589 11.7777091957438
YOR117W 10.7514234580732 11.3932687209745 11.2587694561818
TY_120 5.95114867088525 5.93580053538449 5.89166059690558
YMR174C 8.49545850099485 8.72467418433346 9.6518559706269
YPL117C 10.7211879012765 10.5046713289602 10.6145538571844
TY2_LTR_77 11.9297940548212 11.9801206538102 12.049127298122
YOL101C 7.76141097131674 9.89522697916433 7.85466704627526
YLR053C 7.62843998411388 7.49205634213499 7.10263942962051
YBR135W 9.70614244227352 9.3114074341804 9.36413815370247
YNL168C 9.93928326709444 10.3036524361223 10.0704544058998
What I'm trying to do right now is to add 10 lines from File 2
to File 1
after 2 lines. It should look like this:
transcr_15824 3.95253441295071 3.99992738843234 3.93880798313547
YML042W 10.3143219248979 10.6898819949325 11.0073811719421
YLR177W 11.321823973245 12.1264440368589 11.7777091957438
YOR117W 10.7514234580732 11.3932687209745 11.2587694561818
TY_120 5.95114867088525 5.93580053538449 5.89166059690558
YMR174C 8.49545850099485 8.72467418433346 9.6518559706269
YPL117C 10.7211879012765 10.5046713289602 10.6145538571844
TY2_LTR_77 11.9297940548212 11.9801206538102 12.049127298122
YOL101C 7.76141097131674 9.89522697916433 7.85466704627526
YLR053C 7.62843998411388 7.49205634213499 7.10263942962051
YBR135W 9.70614244227352 9.3114074341804 9.36413815370247
YNL168C 9.93928326709444 10.3036524361223 10.0704544058998
transcr_18545 7.76182774638543 7.25508954643215 7.92562682485731
YCR105W 8.46144110056843 8.30995100411912 8.85470858413405
So, basically, I'm trying to move 10 lines from File 2
between each transcr_
keeping the already existent line that is already below each transcr_
.
Edit:
File 2
has around 2,000 lines and File 1
has around 200 "transcr_" rows. So, it would be: pick up the first 10 lines of File 2
, put them between the first and the second "transcr_" rows (and after the already existing line between those two "transcr_". Then, get the lines from 11 to 20 from File 2
and put them between the second and the third "transcr_". Then, get the lines from 21 to 30 from File 2
and put them between the third and the fourth "transcr_" and so on.
It may look like this:
transcr_1
already existing line
10 first lines from `File 2`
transcr_2
already existing line
Lines 11-20 from `File 2`
transcr_3
already existing line
Lines 21-30 from `File 2`
transcr_4
.....
text-processing
text-processing
edited Dec 16 at 11:53
Rui F Ribeiro
38.8k1479128
38.8k1479128
asked Dec 13 at 13:36
Lucas Farinazzo Marques
284
284
1
Welcome on U&L! Have you got only two files, or more files likeFile 2
to be merged intoFile 1
? Should the whole content ofFile 2
be added toFile 1
? Or a part of it only? Should the content ofFile 2
be added after a specific occurrence oftranscr_
, or after all of them?
– fra-san
Dec 13 at 14:01
2
there are three "transcr_" lines in file1; do you want the same 10 lines from file2 after each of those transcr_ lines, or ... subsequent 10 lines from file2 each time, or ???
– Jeff Schaller
Dec 13 at 14:02
Sorry for not being clear, I need the subsequent 10 lines from file 2. File 2 has more than 2,000 lines
– Lucas Farinazzo Marques
Dec 13 at 17:51
Sorry for not being clear, I need the subsequent 10 lines from file 2. File 2 has more than 2,000 lines. I would need the first ten rows ofFile 2
to be moved between 1st transcr_ and 2nd transcr_, then the next 10 to be moved between 2nd transcr_ and 3rd transcr_ and so on
– Lucas Farinazzo Marques
Dec 13 at 17:59
1
Hi @LucasFarinazzoMarques. Please edit your question to include what you're actually trying to achieve. Currently, your question seems like it has an answer.
– zarose
Dec 13 at 18:07
|
show 3 more comments
1
Welcome on U&L! Have you got only two files, or more files likeFile 2
to be merged intoFile 1
? Should the whole content ofFile 2
be added toFile 1
? Or a part of it only? Should the content ofFile 2
be added after a specific occurrence oftranscr_
, or after all of them?
– fra-san
Dec 13 at 14:01
2
there are three "transcr_" lines in file1; do you want the same 10 lines from file2 after each of those transcr_ lines, or ... subsequent 10 lines from file2 each time, or ???
– Jeff Schaller
Dec 13 at 14:02
Sorry for not being clear, I need the subsequent 10 lines from file 2. File 2 has more than 2,000 lines
– Lucas Farinazzo Marques
Dec 13 at 17:51
Sorry for not being clear, I need the subsequent 10 lines from file 2. File 2 has more than 2,000 lines. I would need the first ten rows ofFile 2
to be moved between 1st transcr_ and 2nd transcr_, then the next 10 to be moved between 2nd transcr_ and 3rd transcr_ and so on
– Lucas Farinazzo Marques
Dec 13 at 17:59
1
Hi @LucasFarinazzoMarques. Please edit your question to include what you're actually trying to achieve. Currently, your question seems like it has an answer.
– zarose
Dec 13 at 18:07
1
1
Welcome on U&L! Have you got only two files, or more files like
File 2
to be merged into File 1
? Should the whole content of File 2
be added to File 1
? Or a part of it only? Should the content of File 2
be added after a specific occurrence of transcr_
, or after all of them?– fra-san
Dec 13 at 14:01
Welcome on U&L! Have you got only two files, or more files like
File 2
to be merged into File 1
? Should the whole content of File 2
be added to File 1
? Or a part of it only? Should the content of File 2
be added after a specific occurrence of transcr_
, or after all of them?– fra-san
Dec 13 at 14:01
2
2
there are three "transcr_" lines in file1; do you want the same 10 lines from file2 after each of those transcr_ lines, or ... subsequent 10 lines from file2 each time, or ???
– Jeff Schaller
Dec 13 at 14:02
there are three "transcr_" lines in file1; do you want the same 10 lines from file2 after each of those transcr_ lines, or ... subsequent 10 lines from file2 each time, or ???
– Jeff Schaller
Dec 13 at 14:02
Sorry for not being clear, I need the subsequent 10 lines from file 2. File 2 has more than 2,000 lines
– Lucas Farinazzo Marques
Dec 13 at 17:51
Sorry for not being clear, I need the subsequent 10 lines from file 2. File 2 has more than 2,000 lines
– Lucas Farinazzo Marques
Dec 13 at 17:51
Sorry for not being clear, I need the subsequent 10 lines from file 2. File 2 has more than 2,000 lines. I would need the first ten rows of
File 2
to be moved between 1st transcr_ and 2nd transcr_, then the next 10 to be moved between 2nd transcr_ and 3rd transcr_ and so on– Lucas Farinazzo Marques
Dec 13 at 17:59
Sorry for not being clear, I need the subsequent 10 lines from file 2. File 2 has more than 2,000 lines. I would need the first ten rows of
File 2
to be moved between 1st transcr_ and 2nd transcr_, then the next 10 to be moved between 2nd transcr_ and 3rd transcr_ and so on– Lucas Farinazzo Marques
Dec 13 at 17:59
1
1
Hi @LucasFarinazzoMarques. Please edit your question to include what you're actually trying to achieve. Currently, your question seems like it has an answer.
– zarose
Dec 13 at 18:07
Hi @LucasFarinazzoMarques. Please edit your question to include what you're actually trying to achieve. Currently, your question seems like it has an answer.
– zarose
Dec 13 at 18:07
|
show 3 more comments
1 Answer
1
active
oldest
votes
You could use ed
!
ed -s file1 <<< $'2r !head -10 file2nwnq'
This tells ed to edit file1 with three commands:
- on line 2, read in the output of the command
head -10 file2
and insert it w
rite the file outq
uit ed
With GNU sed (using the e
extension, which pipes input from a shell command):
sed -i '3e head -10 file2' file1
Extended solution, to iterate through file2
The script below is a for
loop that repeats the ed
idea as many times as there are transcr_
blocks in file1. Each time through the loop, we calculate three items:
- the line number for
ed
to start reading from file1 - the line number for
sed
to start reading from file2 - the line number for
sed
to stop reading from file2
Item #1 is spelled out more clearly as: 10*(N-1) + 2*N
, which I reduced to 12*N - 10
.
Items #2 and #3 are spelled out more clearly as 10*(N-1) + 1
through 10*N
, which I reduced to 10*N - 9
through 10*N
.
I replaced the head
command with the more flexible & powerful sed
command for picking out blocks of lines from file2.
This will rewrite file1 times
times as it goes through the loop.
# how many times we need to insert blocks
times=$(grep -c transcr_ file1)
for((index=1;index <= times; index++));
do
printf "%dr !sed -n %d,%dp file2nwnqn" $((12 * index - 10)) $((10 * index - 9)) $(( 10 * index )) |
ed -s file1
done
Hey there Jeff! Thanks for your answer! Both worked for the first iteration. Do you know how can I replicate it for the whole File 2? It has at least 2,000 rows and I would need the first ten rows to be moved between transcr_1 and transcr_2, then the next 10 to be moved between transcr_2 and transcr_3 and so on
– Lucas Farinazzo Marques
Dec 13 at 17:56
Sorry for the misunderstanding; see if the update gets you closer; thanks!
– Jeff Schaller
Dec 13 at 18:30
1
Thanks a lot Jeff! It worked flawlessly :D
– Lucas Farinazzo Marques
Dec 13 at 18:41
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487764%2fhow-can-i-add-10-lines-from-a-file-file2-to-another-one-after-2-lines-file1%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could use ed
!
ed -s file1 <<< $'2r !head -10 file2nwnq'
This tells ed to edit file1 with three commands:
- on line 2, read in the output of the command
head -10 file2
and insert it w
rite the file outq
uit ed
With GNU sed (using the e
extension, which pipes input from a shell command):
sed -i '3e head -10 file2' file1
Extended solution, to iterate through file2
The script below is a for
loop that repeats the ed
idea as many times as there are transcr_
blocks in file1. Each time through the loop, we calculate three items:
- the line number for
ed
to start reading from file1 - the line number for
sed
to start reading from file2 - the line number for
sed
to stop reading from file2
Item #1 is spelled out more clearly as: 10*(N-1) + 2*N
, which I reduced to 12*N - 10
.
Items #2 and #3 are spelled out more clearly as 10*(N-1) + 1
through 10*N
, which I reduced to 10*N - 9
through 10*N
.
I replaced the head
command with the more flexible & powerful sed
command for picking out blocks of lines from file2.
This will rewrite file1 times
times as it goes through the loop.
# how many times we need to insert blocks
times=$(grep -c transcr_ file1)
for((index=1;index <= times; index++));
do
printf "%dr !sed -n %d,%dp file2nwnqn" $((12 * index - 10)) $((10 * index - 9)) $(( 10 * index )) |
ed -s file1
done
Hey there Jeff! Thanks for your answer! Both worked for the first iteration. Do you know how can I replicate it for the whole File 2? It has at least 2,000 rows and I would need the first ten rows to be moved between transcr_1 and transcr_2, then the next 10 to be moved between transcr_2 and transcr_3 and so on
– Lucas Farinazzo Marques
Dec 13 at 17:56
Sorry for the misunderstanding; see if the update gets you closer; thanks!
– Jeff Schaller
Dec 13 at 18:30
1
Thanks a lot Jeff! It worked flawlessly :D
– Lucas Farinazzo Marques
Dec 13 at 18:41
add a comment |
You could use ed
!
ed -s file1 <<< $'2r !head -10 file2nwnq'
This tells ed to edit file1 with three commands:
- on line 2, read in the output of the command
head -10 file2
and insert it w
rite the file outq
uit ed
With GNU sed (using the e
extension, which pipes input from a shell command):
sed -i '3e head -10 file2' file1
Extended solution, to iterate through file2
The script below is a for
loop that repeats the ed
idea as many times as there are transcr_
blocks in file1. Each time through the loop, we calculate three items:
- the line number for
ed
to start reading from file1 - the line number for
sed
to start reading from file2 - the line number for
sed
to stop reading from file2
Item #1 is spelled out more clearly as: 10*(N-1) + 2*N
, which I reduced to 12*N - 10
.
Items #2 and #3 are spelled out more clearly as 10*(N-1) + 1
through 10*N
, which I reduced to 10*N - 9
through 10*N
.
I replaced the head
command with the more flexible & powerful sed
command for picking out blocks of lines from file2.
This will rewrite file1 times
times as it goes through the loop.
# how many times we need to insert blocks
times=$(grep -c transcr_ file1)
for((index=1;index <= times; index++));
do
printf "%dr !sed -n %d,%dp file2nwnqn" $((12 * index - 10)) $((10 * index - 9)) $(( 10 * index )) |
ed -s file1
done
Hey there Jeff! Thanks for your answer! Both worked for the first iteration. Do you know how can I replicate it for the whole File 2? It has at least 2,000 rows and I would need the first ten rows to be moved between transcr_1 and transcr_2, then the next 10 to be moved between transcr_2 and transcr_3 and so on
– Lucas Farinazzo Marques
Dec 13 at 17:56
Sorry for the misunderstanding; see if the update gets you closer; thanks!
– Jeff Schaller
Dec 13 at 18:30
1
Thanks a lot Jeff! It worked flawlessly :D
– Lucas Farinazzo Marques
Dec 13 at 18:41
add a comment |
You could use ed
!
ed -s file1 <<< $'2r !head -10 file2nwnq'
This tells ed to edit file1 with three commands:
- on line 2, read in the output of the command
head -10 file2
and insert it w
rite the file outq
uit ed
With GNU sed (using the e
extension, which pipes input from a shell command):
sed -i '3e head -10 file2' file1
Extended solution, to iterate through file2
The script below is a for
loop that repeats the ed
idea as many times as there are transcr_
blocks in file1. Each time through the loop, we calculate three items:
- the line number for
ed
to start reading from file1 - the line number for
sed
to start reading from file2 - the line number for
sed
to stop reading from file2
Item #1 is spelled out more clearly as: 10*(N-1) + 2*N
, which I reduced to 12*N - 10
.
Items #2 and #3 are spelled out more clearly as 10*(N-1) + 1
through 10*N
, which I reduced to 10*N - 9
through 10*N
.
I replaced the head
command with the more flexible & powerful sed
command for picking out blocks of lines from file2.
This will rewrite file1 times
times as it goes through the loop.
# how many times we need to insert blocks
times=$(grep -c transcr_ file1)
for((index=1;index <= times; index++));
do
printf "%dr !sed -n %d,%dp file2nwnqn" $((12 * index - 10)) $((10 * index - 9)) $(( 10 * index )) |
ed -s file1
done
You could use ed
!
ed -s file1 <<< $'2r !head -10 file2nwnq'
This tells ed to edit file1 with three commands:
- on line 2, read in the output of the command
head -10 file2
and insert it w
rite the file outq
uit ed
With GNU sed (using the e
extension, which pipes input from a shell command):
sed -i '3e head -10 file2' file1
Extended solution, to iterate through file2
The script below is a for
loop that repeats the ed
idea as many times as there are transcr_
blocks in file1. Each time through the loop, we calculate three items:
- the line number for
ed
to start reading from file1 - the line number for
sed
to start reading from file2 - the line number for
sed
to stop reading from file2
Item #1 is spelled out more clearly as: 10*(N-1) + 2*N
, which I reduced to 12*N - 10
.
Items #2 and #3 are spelled out more clearly as 10*(N-1) + 1
through 10*N
, which I reduced to 10*N - 9
through 10*N
.
I replaced the head
command with the more flexible & powerful sed
command for picking out blocks of lines from file2.
This will rewrite file1 times
times as it goes through the loop.
# how many times we need to insert blocks
times=$(grep -c transcr_ file1)
for((index=1;index <= times; index++));
do
printf "%dr !sed -n %d,%dp file2nwnqn" $((12 * index - 10)) $((10 * index - 9)) $(( 10 * index )) |
ed -s file1
done
edited Dec 13 at 18:30
answered Dec 13 at 14:07
Jeff Schaller
38.5k1053125
38.5k1053125
Hey there Jeff! Thanks for your answer! Both worked for the first iteration. Do you know how can I replicate it for the whole File 2? It has at least 2,000 rows and I would need the first ten rows to be moved between transcr_1 and transcr_2, then the next 10 to be moved between transcr_2 and transcr_3 and so on
– Lucas Farinazzo Marques
Dec 13 at 17:56
Sorry for the misunderstanding; see if the update gets you closer; thanks!
– Jeff Schaller
Dec 13 at 18:30
1
Thanks a lot Jeff! It worked flawlessly :D
– Lucas Farinazzo Marques
Dec 13 at 18:41
add a comment |
Hey there Jeff! Thanks for your answer! Both worked for the first iteration. Do you know how can I replicate it for the whole File 2? It has at least 2,000 rows and I would need the first ten rows to be moved between transcr_1 and transcr_2, then the next 10 to be moved between transcr_2 and transcr_3 and so on
– Lucas Farinazzo Marques
Dec 13 at 17:56
Sorry for the misunderstanding; see if the update gets you closer; thanks!
– Jeff Schaller
Dec 13 at 18:30
1
Thanks a lot Jeff! It worked flawlessly :D
– Lucas Farinazzo Marques
Dec 13 at 18:41
Hey there Jeff! Thanks for your answer! Both worked for the first iteration. Do you know how can I replicate it for the whole File 2? It has at least 2,000 rows and I would need the first ten rows to be moved between transcr_1 and transcr_2, then the next 10 to be moved between transcr_2 and transcr_3 and so on
– Lucas Farinazzo Marques
Dec 13 at 17:56
Hey there Jeff! Thanks for your answer! Both worked for the first iteration. Do you know how can I replicate it for the whole File 2? It has at least 2,000 rows and I would need the first ten rows to be moved between transcr_1 and transcr_2, then the next 10 to be moved between transcr_2 and transcr_3 and so on
– Lucas Farinazzo Marques
Dec 13 at 17:56
Sorry for the misunderstanding; see if the update gets you closer; thanks!
– Jeff Schaller
Dec 13 at 18:30
Sorry for the misunderstanding; see if the update gets you closer; thanks!
– Jeff Schaller
Dec 13 at 18:30
1
1
Thanks a lot Jeff! It worked flawlessly :D
– Lucas Farinazzo Marques
Dec 13 at 18:41
Thanks a lot Jeff! It worked flawlessly :D
– Lucas Farinazzo Marques
Dec 13 at 18:41
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f487764%2fhow-can-i-add-10-lines-from-a-file-file2-to-another-one-after-2-lines-file1%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Welcome on U&L! Have you got only two files, or more files like
File 2
to be merged intoFile 1
? Should the whole content ofFile 2
be added toFile 1
? Or a part of it only? Should the content ofFile 2
be added after a specific occurrence oftranscr_
, or after all of them?– fra-san
Dec 13 at 14:01
2
there are three "transcr_" lines in file1; do you want the same 10 lines from file2 after each of those transcr_ lines, or ... subsequent 10 lines from file2 each time, or ???
– Jeff Schaller
Dec 13 at 14:02
Sorry for not being clear, I need the subsequent 10 lines from file 2. File 2 has more than 2,000 lines
– Lucas Farinazzo Marques
Dec 13 at 17:51
Sorry for not being clear, I need the subsequent 10 lines from file 2. File 2 has more than 2,000 lines. I would need the first ten rows of
File 2
to be moved between 1st transcr_ and 2nd transcr_, then the next 10 to be moved between 2nd transcr_ and 3rd transcr_ and so on– Lucas Farinazzo Marques
Dec 13 at 17:59
1
Hi @LucasFarinazzoMarques. Please edit your question to include what you're actually trying to achieve. Currently, your question seems like it has an answer.
– zarose
Dec 13 at 18:07