bash - Extracting last 10 lines of a file that match "foo" -
I want to write the last ten lines which contain a special word such as a new text file example in the "foo" file The name for boo.txt . How can I get it at the command prompt of Unix Terminal? You grep and tail : grep can use "foo" input .txt | Tail-N10 & gt; The default number of lines printed by Boo.txt tail is 10, so you can skip the -n 10 section if you Always want that many. & gt; redirection will make boo.txt if it does not exist. If it was present before it was running, the file would be truncated (i.e. empty) first. Then in either case, there should be up to 10 rows of text in boo.txt . If you want to add boo.txt , then you should change & gt; & Gt; . Redirect to using "grep" bar "input.txt. Tail-4 42 & gt; & Gt; Boo.txt If you are looking for the first version of the string, then there may be interest in head .