Find and copy text from one file to another [duplicate]
This question already has an answer here:
How to delete whole text from the beginning of a line with a certain string
3 answers
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
marked as duplicate by Jeff Schaller, RalfFriedl, msp9011, Archemar, elbarna Jan 24 at 2:45
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 |
This question already has an answer here:
How to delete whole text from the beginning of a line with a certain string
3 answers
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
marked as duplicate by Jeff Schaller, RalfFriedl, msp9011, Archemar, elbarna Jan 24 at 2:45
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.
1
Provide an example of the text, indicate the word that you are looking for, and give the expected output.
– Nasir Riley
Jan 21 at 23:35
add a comment |
This question already has an answer here:
How to delete whole text from the beginning of a line with a certain string
3 answers
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
This question already has an answer here:
How to delete whole text from the beginning of a line with a certain string
3 answers
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?
This question already has an answer here:
How to delete whole text from the beginning of a line with a certain string
3 answers
shell-script
shell-script
asked Jan 21 at 23:16
diggidrediggidre
384
384
marked as duplicate by Jeff Schaller, RalfFriedl, msp9011, Archemar, elbarna Jan 24 at 2:45
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 Jeff Schaller, RalfFriedl, msp9011, Archemar, elbarna Jan 24 at 2:45
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.
1
Provide an example of the text, indicate the word that you are looking for, and give the expected output.
– Nasir Riley
Jan 21 at 23:35
add a comment |
1
Provide an example of the text, indicate the word that you are looking for, and give the expected output.
– Nasir Riley
Jan 21 at 23:35
1
1
Provide an example of the text, indicate the word that you are looking for, and give the expected output.
– Nasir Riley
Jan 21 at 23:35
Provide an example of the text, indicate the word that you are looking for, and give the expected output.
– Nasir Riley
Jan 21 at 23:35
add a comment |
3 Answers
3
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.
OK thx that's great. Is there a way not to copy the text lines but to cut them out. is that possible in just one command or do i have to purge after i copied text with sed?
– diggidre
Jan 22 at 9:33
@diggidre Do you mean that you just want to send them standard output as in only having them appear onscreen or do you mean that you just want to remove them from the file?
– Nasir Riley
Jan 22 at 12:29
add a comment |
grep -A $(< $A wc -l) hotword $A >> $B
And what if the file contains more than 99 lines after the hotword?
– Psychonaut
Jan 22 at 8:36
@Psychonaut. Fixed.
– user1133275
Jan 22 at 12:43
add a comment |
Another option is to use awk:
awk "/word/, 0" infile >outfile
This copies the range of lines starting from the first one containing ‘word’, and ending never (hence the 0
).
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
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.
OK thx that's great. Is there a way not to copy the text lines but to cut them out. is that possible in just one command or do i have to purge after i copied text with sed?
– diggidre
Jan 22 at 9:33
@diggidre Do you mean that you just want to send them standard output as in only having them appear onscreen or do you mean that you just want to remove them from the file?
– Nasir Riley
Jan 22 at 12:29
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.
OK thx that's great. Is there a way not to copy the text lines but to cut them out. is that possible in just one command or do i have to purge after i copied text with sed?
– diggidre
Jan 22 at 9:33
@diggidre Do you mean that you just want to send them standard output as in only having them appear onscreen or do you mean that you just want to remove them from the file?
– Nasir Riley
Jan 22 at 12:29
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 Jan 22 at 0:11
answered Jan 22 at 0:05
Nasir RileyNasir Riley
2,709249
2,709249
OK thx that's great. Is there a way not to copy the text lines but to cut them out. is that possible in just one command or do i have to purge after i copied text with sed?
– diggidre
Jan 22 at 9:33
@diggidre Do you mean that you just want to send them standard output as in only having them appear onscreen or do you mean that you just want to remove them from the file?
– Nasir Riley
Jan 22 at 12:29
add a comment |
OK thx that's great. Is there a way not to copy the text lines but to cut them out. is that possible in just one command or do i have to purge after i copied text with sed?
– diggidre
Jan 22 at 9:33
@diggidre Do you mean that you just want to send them standard output as in only having them appear onscreen or do you mean that you just want to remove them from the file?
– Nasir Riley
Jan 22 at 12:29
OK thx that's great. Is there a way not to copy the text lines but to cut them out. is that possible in just one command or do i have to purge after i copied text with sed?
– diggidre
Jan 22 at 9:33
OK thx that's great. Is there a way not to copy the text lines but to cut them out. is that possible in just one command or do i have to purge after i copied text with sed?
– diggidre
Jan 22 at 9:33
@diggidre Do you mean that you just want to send them standard output as in only having them appear onscreen or do you mean that you just want to remove them from the file?
– Nasir Riley
Jan 22 at 12:29
@diggidre Do you mean that you just want to send them standard output as in only having them appear onscreen or do you mean that you just want to remove them from the file?
– Nasir Riley
Jan 22 at 12:29
add a comment |
grep -A $(< $A wc -l) hotword $A >> $B
And what if the file contains more than 99 lines after the hotword?
– Psychonaut
Jan 22 at 8:36
@Psychonaut. Fixed.
– user1133275
Jan 22 at 12:43
add a comment |
grep -A $(< $A wc -l) hotword $A >> $B
And what if the file contains more than 99 lines after the hotword?
– Psychonaut
Jan 22 at 8:36
@Psychonaut. Fixed.
– user1133275
Jan 22 at 12:43
add a comment |
grep -A $(< $A wc -l) hotword $A >> $B
grep -A $(< $A wc -l) hotword $A >> $B
edited Jan 22 at 12:43
answered Jan 21 at 23:43
user1133275user1133275
3,451723
3,451723
And what if the file contains more than 99 lines after the hotword?
– Psychonaut
Jan 22 at 8:36
@Psychonaut. Fixed.
– user1133275
Jan 22 at 12:43
add a comment |
And what if the file contains more than 99 lines after the hotword?
– Psychonaut
Jan 22 at 8:36
@Psychonaut. Fixed.
– user1133275
Jan 22 at 12:43
And what if the file contains more than 99 lines after the hotword?
– Psychonaut
Jan 22 at 8:36
And what if the file contains more than 99 lines after the hotword?
– Psychonaut
Jan 22 at 8:36
@Psychonaut. Fixed.
– user1133275
Jan 22 at 12:43
@Psychonaut. Fixed.
– user1133275
Jan 22 at 12:43
add a comment |
Another option is to use awk:
awk "/word/, 0" infile >outfile
This copies the range of lines starting from the first one containing ‘word’, and ending never (hence the 0
).
add a comment |
Another option is to use awk:
awk "/word/, 0" infile >outfile
This copies the range of lines starting from the first one containing ‘word’, and ending never (hence the 0
).
add a comment |
Another option is to use awk:
awk "/word/, 0" infile >outfile
This copies the range of lines starting from the first one containing ‘word’, and ending never (hence the 0
).
Another option is to use awk:
awk "/word/, 0" infile >outfile
This copies the range of lines starting from the first one containing ‘word’, and ending never (hence the 0
).
answered Jan 23 at 10:16
giddsgidds
1012
1012
add a comment |
add a comment |
1
Provide an example of the text, indicate the word that you are looking for, and give the expected output.
– Nasir Riley
Jan 21 at 23:35