What's the unix command to copy specific lines from one file to another file?












2















I searched the web for hours, please excuse me if I overlooked something. I'm a beginner.
I want to copy lines that include a certain string from file1 to file2. These lines from file 1 have to be inserted in file2, but only in specific lines that include another string.



(It's about the entire lines with the timecode)



Content of file1:




1
00:00:16,520 --> 00:00:23,200
Some text

2
00:00:25,800 --> 00:00:32,600
Some more text


Content of file2:




1
00: 00: 16,520 -> 00: 00: 23,200
Different text

2
00: 00: 25,720 -> 00: 00: 32,520
More different text




awk '/ --> /' file1


lists the lines I need from file1. But what do I have to add to the code to take these awk results and copy them only into the lines of file2 that include
'/ -> /'
??


Thanks a lot for your support!!!

Result in file2 should be:




1
00:00:16,520 --> 00:00:23,200
Different text

2
00:00:25,800 --> 00:00:32,600
More different text









share|improve this question


















  • 1





    Next time please show more of your initial attempt; it is barely sufficient to justify a good question. Additionally, the problem statement should have been stated clearly. Please read How to Ask.

    – Rafael
    Dec 31 '18 at 16:42


















2















I searched the web for hours, please excuse me if I overlooked something. I'm a beginner.
I want to copy lines that include a certain string from file1 to file2. These lines from file 1 have to be inserted in file2, but only in specific lines that include another string.



(It's about the entire lines with the timecode)



Content of file1:




1
00:00:16,520 --> 00:00:23,200
Some text

2
00:00:25,800 --> 00:00:32,600
Some more text


Content of file2:




1
00: 00: 16,520 -> 00: 00: 23,200
Different text

2
00: 00: 25,720 -> 00: 00: 32,520
More different text




awk '/ --> /' file1


lists the lines I need from file1. But what do I have to add to the code to take these awk results and copy them only into the lines of file2 that include
'/ -> /'
??


Thanks a lot for your support!!!

Result in file2 should be:




1
00:00:16,520 --> 00:00:23,200
Different text

2
00:00:25,800 --> 00:00:32,600
More different text









share|improve this question


















  • 1





    Next time please show more of your initial attempt; it is barely sufficient to justify a good question. Additionally, the problem statement should have been stated clearly. Please read How to Ask.

    – Rafael
    Dec 31 '18 at 16:42
















2












2








2








I searched the web for hours, please excuse me if I overlooked something. I'm a beginner.
I want to copy lines that include a certain string from file1 to file2. These lines from file 1 have to be inserted in file2, but only in specific lines that include another string.



(It's about the entire lines with the timecode)



Content of file1:




1
00:00:16,520 --> 00:00:23,200
Some text

2
00:00:25,800 --> 00:00:32,600
Some more text


Content of file2:




1
00: 00: 16,520 -> 00: 00: 23,200
Different text

2
00: 00: 25,720 -> 00: 00: 32,520
More different text




awk '/ --> /' file1


lists the lines I need from file1. But what do I have to add to the code to take these awk results and copy them only into the lines of file2 that include
'/ -> /'
??


Thanks a lot for your support!!!

Result in file2 should be:




1
00:00:16,520 --> 00:00:23,200
Different text

2
00:00:25,800 --> 00:00:32,600
More different text









share|improve this question














I searched the web for hours, please excuse me if I overlooked something. I'm a beginner.
I want to copy lines that include a certain string from file1 to file2. These lines from file 1 have to be inserted in file2, but only in specific lines that include another string.



(It's about the entire lines with the timecode)



Content of file1:




1
00:00:16,520 --> 00:00:23,200
Some text

2
00:00:25,800 --> 00:00:32,600
Some more text


Content of file2:




1
00: 00: 16,520 -> 00: 00: 23,200
Different text

2
00: 00: 25,720 -> 00: 00: 32,520
More different text




awk '/ --> /' file1


lists the lines I need from file1. But what do I have to add to the code to take these awk results and copy them only into the lines of file2 that include
'/ -> /'
??


Thanks a lot for your support!!!

Result in file2 should be:




1
00:00:16,520 --> 00:00:23,200
Different text

2
00:00:25,800 --> 00:00:32,600
More different text






file unix awk copy






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 30 '18 at 17:22









piano_worldpiano_world

134




134








  • 1





    Next time please show more of your initial attempt; it is barely sufficient to justify a good question. Additionally, the problem statement should have been stated clearly. Please read How to Ask.

    – Rafael
    Dec 31 '18 at 16:42
















  • 1





    Next time please show more of your initial attempt; it is barely sufficient to justify a good question. Additionally, the problem statement should have been stated clearly. Please read How to Ask.

    – Rafael
    Dec 31 '18 at 16:42










1




1





Next time please show more of your initial attempt; it is barely sufficient to justify a good question. Additionally, the problem statement should have been stated clearly. Please read How to Ask.

– Rafael
Dec 31 '18 at 16:42







Next time please show more of your initial attempt; it is barely sufficient to justify a good question. Additionally, the problem statement should have been stated clearly. Please read How to Ask.

– Rafael
Dec 31 '18 at 16:42














1 Answer
1






active

oldest

votes


















1














Note: below is for GNU awk



So you wanna replace timeline of subtitles, right?

Given that they're indentically indexed, i.e. the number above the timecode are the same.

Then you can try this:



awk 'ARGIND==1 && /^[0-9]+$/{getline timeline; tl[$0]=timeline;}ARGIND==2 &&/^[0-9]+$/{getline tmp2drop; print $0 ORS tl[$0];} ' file1 file2


Note that /^[0-9]+$/ is the criterial, which match a whole line with a number only.

But if you have such subtitle text exists, then it will leads to error replace.



Another way is to use the line number(FNR denoted) as index:



awk 'ARGIND==1 && /-->/{tl[FNR]=$0} ARGIND==2 {if (/->/) print tl[FNR]; else print $0} ' file1 file2


But if the line number are not the same between two files, for example some subtitle texts are multiline, it still will replace wronly.



Given the occurances are at the relatively same places, we can manage a index on our own:



awk 'ARGIND==1 && /-->/{tl[i++]=$0} ARGIND==2 {if (/->/) print tl[j++]; else print $0} ' file1 file2


None of these are perfect, but to give you an idea how you could do the thing.

Choose depends on your situation, and improve the code yourself :)



note: They are just print to console, if you want replace the file. you can use > or '>>` to print the output to a temp file, and later rename to file2.

For example:



awk 'ARGIND==1 && /-->/{tl[i++]=$0} ARGIND==2 {if (/->/) print tl[j++]; else print $0} ' file1 file2 >> tmpFile2check  


If you are not using GNU awk, ARGIND==1 won't work, then use this:



awk 'NR==FNR && /-->/{tl[i++]=$0} NR>FNR {if (/->/) print tl[j++]; else print $0} ' file1 file2 >> tmpFile2check


NR means the Number of Records, FNR means current File's Number of Records. If they are equal then it's the first file the script is dealing with. If NR>FNR means it's not the first file.
Note if file1 is or could be empty, then this mechanism will fail, then you should change to FILENAME=="file1" or other file checking method to avoid error processing.






share|improve this answer


























  • Thanks a lot for your input. I tried all of your suggestions, but with no success so far. I keep trying, but as stated, I'm just a beginner :)

    – piano_world
    Dec 30 '18 at 18:25











  • @piano_world Learn some basic of awk instead of search endlessly. Also check the last one I just updated.

    – Tiw
    Dec 30 '18 at 18:33






  • 1





    did learn the basics, and knew about the output... still trying. Thanks anyways.

    – piano_world
    Dec 30 '18 at 18:41






  • 1





    @piano_world It's hard to tell that you learned anything about awk. Your question doesn't show your competence especially when augmented by "I'm just a beginner." Tiw is nice enough to help and "thanks anyways" isn't sufficient appreciation. Please consider up-voting his answer instead of leaving a thank you comment.

    – Rafael
    Dec 31 '18 at 16:48








  • 1





    @Rafael hope I did it correctly now. Still new to this forum.

    – piano_world
    Jan 1 at 9:33











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53979800%2fwhats-the-unix-command-to-copy-specific-lines-from-one-file-to-another-file%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









1














Note: below is for GNU awk



So you wanna replace timeline of subtitles, right?

Given that they're indentically indexed, i.e. the number above the timecode are the same.

Then you can try this:



awk 'ARGIND==1 && /^[0-9]+$/{getline timeline; tl[$0]=timeline;}ARGIND==2 &&/^[0-9]+$/{getline tmp2drop; print $0 ORS tl[$0];} ' file1 file2


Note that /^[0-9]+$/ is the criterial, which match a whole line with a number only.

But if you have such subtitle text exists, then it will leads to error replace.



Another way is to use the line number(FNR denoted) as index:



awk 'ARGIND==1 && /-->/{tl[FNR]=$0} ARGIND==2 {if (/->/) print tl[FNR]; else print $0} ' file1 file2


But if the line number are not the same between two files, for example some subtitle texts are multiline, it still will replace wronly.



Given the occurances are at the relatively same places, we can manage a index on our own:



awk 'ARGIND==1 && /-->/{tl[i++]=$0} ARGIND==2 {if (/->/) print tl[j++]; else print $0} ' file1 file2


None of these are perfect, but to give you an idea how you could do the thing.

Choose depends on your situation, and improve the code yourself :)



note: They are just print to console, if you want replace the file. you can use > or '>>` to print the output to a temp file, and later rename to file2.

For example:



awk 'ARGIND==1 && /-->/{tl[i++]=$0} ARGIND==2 {if (/->/) print tl[j++]; else print $0} ' file1 file2 >> tmpFile2check  


If you are not using GNU awk, ARGIND==1 won't work, then use this:



awk 'NR==FNR && /-->/{tl[i++]=$0} NR>FNR {if (/->/) print tl[j++]; else print $0} ' file1 file2 >> tmpFile2check


NR means the Number of Records, FNR means current File's Number of Records. If they are equal then it's the first file the script is dealing with. If NR>FNR means it's not the first file.
Note if file1 is or could be empty, then this mechanism will fail, then you should change to FILENAME=="file1" or other file checking method to avoid error processing.






share|improve this answer


























  • Thanks a lot for your input. I tried all of your suggestions, but with no success so far. I keep trying, but as stated, I'm just a beginner :)

    – piano_world
    Dec 30 '18 at 18:25











  • @piano_world Learn some basic of awk instead of search endlessly. Also check the last one I just updated.

    – Tiw
    Dec 30 '18 at 18:33






  • 1





    did learn the basics, and knew about the output... still trying. Thanks anyways.

    – piano_world
    Dec 30 '18 at 18:41






  • 1





    @piano_world It's hard to tell that you learned anything about awk. Your question doesn't show your competence especially when augmented by "I'm just a beginner." Tiw is nice enough to help and "thanks anyways" isn't sufficient appreciation. Please consider up-voting his answer instead of leaving a thank you comment.

    – Rafael
    Dec 31 '18 at 16:48








  • 1





    @Rafael hope I did it correctly now. Still new to this forum.

    – piano_world
    Jan 1 at 9:33
















1














Note: below is for GNU awk



So you wanna replace timeline of subtitles, right?

Given that they're indentically indexed, i.e. the number above the timecode are the same.

Then you can try this:



awk 'ARGIND==1 && /^[0-9]+$/{getline timeline; tl[$0]=timeline;}ARGIND==2 &&/^[0-9]+$/{getline tmp2drop; print $0 ORS tl[$0];} ' file1 file2


Note that /^[0-9]+$/ is the criterial, which match a whole line with a number only.

But if you have such subtitle text exists, then it will leads to error replace.



Another way is to use the line number(FNR denoted) as index:



awk 'ARGIND==1 && /-->/{tl[FNR]=$0} ARGIND==2 {if (/->/) print tl[FNR]; else print $0} ' file1 file2


But if the line number are not the same between two files, for example some subtitle texts are multiline, it still will replace wronly.



Given the occurances are at the relatively same places, we can manage a index on our own:



awk 'ARGIND==1 && /-->/{tl[i++]=$0} ARGIND==2 {if (/->/) print tl[j++]; else print $0} ' file1 file2


None of these are perfect, but to give you an idea how you could do the thing.

Choose depends on your situation, and improve the code yourself :)



note: They are just print to console, if you want replace the file. you can use > or '>>` to print the output to a temp file, and later rename to file2.

For example:



awk 'ARGIND==1 && /-->/{tl[i++]=$0} ARGIND==2 {if (/->/) print tl[j++]; else print $0} ' file1 file2 >> tmpFile2check  


If you are not using GNU awk, ARGIND==1 won't work, then use this:



awk 'NR==FNR && /-->/{tl[i++]=$0} NR>FNR {if (/->/) print tl[j++]; else print $0} ' file1 file2 >> tmpFile2check


NR means the Number of Records, FNR means current File's Number of Records. If they are equal then it's the first file the script is dealing with. If NR>FNR means it's not the first file.
Note if file1 is or could be empty, then this mechanism will fail, then you should change to FILENAME=="file1" or other file checking method to avoid error processing.






share|improve this answer


























  • Thanks a lot for your input. I tried all of your suggestions, but with no success so far. I keep trying, but as stated, I'm just a beginner :)

    – piano_world
    Dec 30 '18 at 18:25











  • @piano_world Learn some basic of awk instead of search endlessly. Also check the last one I just updated.

    – Tiw
    Dec 30 '18 at 18:33






  • 1





    did learn the basics, and knew about the output... still trying. Thanks anyways.

    – piano_world
    Dec 30 '18 at 18:41






  • 1





    @piano_world It's hard to tell that you learned anything about awk. Your question doesn't show your competence especially when augmented by "I'm just a beginner." Tiw is nice enough to help and "thanks anyways" isn't sufficient appreciation. Please consider up-voting his answer instead of leaving a thank you comment.

    – Rafael
    Dec 31 '18 at 16:48








  • 1





    @Rafael hope I did it correctly now. Still new to this forum.

    – piano_world
    Jan 1 at 9:33














1












1








1







Note: below is for GNU awk



So you wanna replace timeline of subtitles, right?

Given that they're indentically indexed, i.e. the number above the timecode are the same.

Then you can try this:



awk 'ARGIND==1 && /^[0-9]+$/{getline timeline; tl[$0]=timeline;}ARGIND==2 &&/^[0-9]+$/{getline tmp2drop; print $0 ORS tl[$0];} ' file1 file2


Note that /^[0-9]+$/ is the criterial, which match a whole line with a number only.

But if you have such subtitle text exists, then it will leads to error replace.



Another way is to use the line number(FNR denoted) as index:



awk 'ARGIND==1 && /-->/{tl[FNR]=$0} ARGIND==2 {if (/->/) print tl[FNR]; else print $0} ' file1 file2


But if the line number are not the same between two files, for example some subtitle texts are multiline, it still will replace wronly.



Given the occurances are at the relatively same places, we can manage a index on our own:



awk 'ARGIND==1 && /-->/{tl[i++]=$0} ARGIND==2 {if (/->/) print tl[j++]; else print $0} ' file1 file2


None of these are perfect, but to give you an idea how you could do the thing.

Choose depends on your situation, and improve the code yourself :)



note: They are just print to console, if you want replace the file. you can use > or '>>` to print the output to a temp file, and later rename to file2.

For example:



awk 'ARGIND==1 && /-->/{tl[i++]=$0} ARGIND==2 {if (/->/) print tl[j++]; else print $0} ' file1 file2 >> tmpFile2check  


If you are not using GNU awk, ARGIND==1 won't work, then use this:



awk 'NR==FNR && /-->/{tl[i++]=$0} NR>FNR {if (/->/) print tl[j++]; else print $0} ' file1 file2 >> tmpFile2check


NR means the Number of Records, FNR means current File's Number of Records. If they are equal then it's the first file the script is dealing with. If NR>FNR means it's not the first file.
Note if file1 is or could be empty, then this mechanism will fail, then you should change to FILENAME=="file1" or other file checking method to avoid error processing.






share|improve this answer















Note: below is for GNU awk



So you wanna replace timeline of subtitles, right?

Given that they're indentically indexed, i.e. the number above the timecode are the same.

Then you can try this:



awk 'ARGIND==1 && /^[0-9]+$/{getline timeline; tl[$0]=timeline;}ARGIND==2 &&/^[0-9]+$/{getline tmp2drop; print $0 ORS tl[$0];} ' file1 file2


Note that /^[0-9]+$/ is the criterial, which match a whole line with a number only.

But if you have such subtitle text exists, then it will leads to error replace.



Another way is to use the line number(FNR denoted) as index:



awk 'ARGIND==1 && /-->/{tl[FNR]=$0} ARGIND==2 {if (/->/) print tl[FNR]; else print $0} ' file1 file2


But if the line number are not the same between two files, for example some subtitle texts are multiline, it still will replace wronly.



Given the occurances are at the relatively same places, we can manage a index on our own:



awk 'ARGIND==1 && /-->/{tl[i++]=$0} ARGIND==2 {if (/->/) print tl[j++]; else print $0} ' file1 file2


None of these are perfect, but to give you an idea how you could do the thing.

Choose depends on your situation, and improve the code yourself :)



note: They are just print to console, if you want replace the file. you can use > or '>>` to print the output to a temp file, and later rename to file2.

For example:



awk 'ARGIND==1 && /-->/{tl[i++]=$0} ARGIND==2 {if (/->/) print tl[j++]; else print $0} ' file1 file2 >> tmpFile2check  


If you are not using GNU awk, ARGIND==1 won't work, then use this:



awk 'NR==FNR && /-->/{tl[i++]=$0} NR>FNR {if (/->/) print tl[j++]; else print $0} ' file1 file2 >> tmpFile2check


NR means the Number of Records, FNR means current File's Number of Records. If they are equal then it's the first file the script is dealing with. If NR>FNR means it's not the first file.
Note if file1 is or could be empty, then this mechanism will fail, then you should change to FILENAME=="file1" or other file checking method to avoid error processing.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 1 at 10:10

























answered Dec 30 '18 at 17:43









TiwTiw

3,60841229




3,60841229













  • Thanks a lot for your input. I tried all of your suggestions, but with no success so far. I keep trying, but as stated, I'm just a beginner :)

    – piano_world
    Dec 30 '18 at 18:25











  • @piano_world Learn some basic of awk instead of search endlessly. Also check the last one I just updated.

    – Tiw
    Dec 30 '18 at 18:33






  • 1





    did learn the basics, and knew about the output... still trying. Thanks anyways.

    – piano_world
    Dec 30 '18 at 18:41






  • 1





    @piano_world It's hard to tell that you learned anything about awk. Your question doesn't show your competence especially when augmented by "I'm just a beginner." Tiw is nice enough to help and "thanks anyways" isn't sufficient appreciation. Please consider up-voting his answer instead of leaving a thank you comment.

    – Rafael
    Dec 31 '18 at 16:48








  • 1





    @Rafael hope I did it correctly now. Still new to this forum.

    – piano_world
    Jan 1 at 9:33



















  • Thanks a lot for your input. I tried all of your suggestions, but with no success so far. I keep trying, but as stated, I'm just a beginner :)

    – piano_world
    Dec 30 '18 at 18:25











  • @piano_world Learn some basic of awk instead of search endlessly. Also check the last one I just updated.

    – Tiw
    Dec 30 '18 at 18:33






  • 1





    did learn the basics, and knew about the output... still trying. Thanks anyways.

    – piano_world
    Dec 30 '18 at 18:41






  • 1





    @piano_world It's hard to tell that you learned anything about awk. Your question doesn't show your competence especially when augmented by "I'm just a beginner." Tiw is nice enough to help and "thanks anyways" isn't sufficient appreciation. Please consider up-voting his answer instead of leaving a thank you comment.

    – Rafael
    Dec 31 '18 at 16:48








  • 1





    @Rafael hope I did it correctly now. Still new to this forum.

    – piano_world
    Jan 1 at 9:33

















Thanks a lot for your input. I tried all of your suggestions, but with no success so far. I keep trying, but as stated, I'm just a beginner :)

– piano_world
Dec 30 '18 at 18:25





Thanks a lot for your input. I tried all of your suggestions, but with no success so far. I keep trying, but as stated, I'm just a beginner :)

– piano_world
Dec 30 '18 at 18:25













@piano_world Learn some basic of awk instead of search endlessly. Also check the last one I just updated.

– Tiw
Dec 30 '18 at 18:33





@piano_world Learn some basic of awk instead of search endlessly. Also check the last one I just updated.

– Tiw
Dec 30 '18 at 18:33




1




1





did learn the basics, and knew about the output... still trying. Thanks anyways.

– piano_world
Dec 30 '18 at 18:41





did learn the basics, and knew about the output... still trying. Thanks anyways.

– piano_world
Dec 30 '18 at 18:41




1




1





@piano_world It's hard to tell that you learned anything about awk. Your question doesn't show your competence especially when augmented by "I'm just a beginner." Tiw is nice enough to help and "thanks anyways" isn't sufficient appreciation. Please consider up-voting his answer instead of leaving a thank you comment.

– Rafael
Dec 31 '18 at 16:48







@piano_world It's hard to tell that you learned anything about awk. Your question doesn't show your competence especially when augmented by "I'm just a beginner." Tiw is nice enough to help and "thanks anyways" isn't sufficient appreciation. Please consider up-voting his answer instead of leaving a thank you comment.

– Rafael
Dec 31 '18 at 16:48






1




1





@Rafael hope I did it correctly now. Still new to this forum.

– piano_world
Jan 1 at 9:33





@Rafael hope I did it correctly now. Still new to this forum.

– piano_world
Jan 1 at 9:33




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53979800%2fwhats-the-unix-command-to-copy-specific-lines-from-one-file-to-another-file%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

How to fix TextFormField cause rebuild widget in Flutter