Find and copy text from one file to another
My goal is to extract text from a specific line with a hotword (don't know how to call it else) in it. The line number can vary because its an weekly updated file. When hotword is detected it should copy this line and all following text to another file.
Could this be done by sed, awk or something else?
shell-script
add a comment |
My goal is to extract text from a specific line with a hotword (don't know how to call it else) in it. The line number can vary because its an weekly updated file. When hotword is detected it should copy this line and all following text to another file.
Could this be done by sed, awk or something else?
shell-script
Provide an example of the text, indicate the word that you are looking for, and give the expected output.
– Nasir Riley
3 hours ago
add a comment |
My goal is to extract text from a specific line with a hotword (don't know how to call it else) in it. The line number can vary because its an weekly updated file. When hotword is detected it should copy this line and all following text to another file.
Could this be done by sed, awk or something else?
shell-script
My goal is to extract text from a specific line with a hotword (don't know how to call it else) in it. The line number can vary because its an weekly updated file. When hotword is detected it should copy this line and all following text to another file.
Could this be done by sed, awk or something else?
shell-script
shell-script
asked 4 hours ago
diggidrediggidre
263
263
Provide an example of the text, indicate the word that you are looking for, and give the expected output.
– Nasir Riley
3 hours ago
add a comment |
Provide an example of the text, indicate the word that you are looking for, and give the expected output.
– Nasir Riley
3 hours ago
Provide an example of the text, indicate the word that you are looking for, and give the expected output.
– Nasir Riley
3 hours ago
Provide an example of the text, indicate the word that you are looking for, and give the expected output.
– Nasir Riley
3 hours ago
add a comment |
2 Answers
2
active
oldest
votes
grep -A 10
can be used to print the line with the matching word and ten lines after (you can substitute 10 with whatever number you want) but as you haven't indicated how many lines are in the file, you can use the following instead:
sed -n '/word/,$p' file >> file2
That will print the line with the word and all of the lines afterwords and then append them to another file. This way, you don't have to account for the total number of lines if the file contains a large number of lines such as 1,000 or more.
add a comment |
grep -A 99 hotword $A >> $B
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%2f495871%2ffind-and-copy-text-from-one-file-to-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
grep -A 10
can be used to print the line with the matching word and ten lines after (you can substitute 10 with whatever number you want) but as you haven't indicated how many lines are in the file, you can use the following instead:
sed -n '/word/,$p' file >> file2
That will print the line with the word and all of the lines afterwords and then append them to another file. This way, you don't have to account for the total number of lines if the file contains a large number of lines such as 1,000 or more.
add a comment |
grep -A 10
can be used to print the line with the matching word and ten lines after (you can substitute 10 with whatever number you want) but as you haven't indicated how many lines are in the file, you can use the following instead:
sed -n '/word/,$p' file >> file2
That will print the line with the word and all of the lines afterwords and then append them to another file. This way, you don't have to account for the total number of lines if the file contains a large number of lines such as 1,000 or more.
add a comment |
grep -A 10
can be used to print the line with the matching word and ten lines after (you can substitute 10 with whatever number you want) but as you haven't indicated how many lines are in the file, you can use the following instead:
sed -n '/word/,$p' file >> file2
That will print the line with the word and all of the lines afterwords and then append them to another file. This way, you don't have to account for the total number of lines if the file contains a large number of lines such as 1,000 or more.
grep -A 10
can be used to print the line with the matching word and ten lines after (you can substitute 10 with whatever number you want) but as you haven't indicated how many lines are in the file, you can use the following instead:
sed -n '/word/,$p' file >> file2
That will print the line with the word and all of the lines afterwords and then append them to another file. This way, you don't have to account for the total number of lines if the file contains a large number of lines such as 1,000 or more.
edited 3 hours ago
answered 3 hours ago
Nasir RileyNasir Riley
2,461249
2,461249
add a comment |
add a comment |
grep -A 99 hotword $A >> $B
add a comment |
grep -A 99 hotword $A >> $B
add a comment |
grep -A 99 hotword $A >> $B
grep -A 99 hotword $A >> $B
answered 3 hours ago
user1133275user1133275
2,919620
2,919620
add a comment |
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.
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%2f495871%2ffind-and-copy-text-from-one-file-to-another%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
Provide an example of the text, indicate the word that you are looking for, and give the expected output.
– Nasir Riley
3 hours ago