Score:1

How to cut a file in this way?

us flag

I have to cut a long file (approx. 2 millions of lines) at the line that follows the line at which there is the last appearance of a word.

For example the file could be:

line 1
line 2 
....
word
line n
word
line n+2

This file should be cut at the second appearance of word, so the lines following line n+2 (and that line too) should be dropped.

The problem is that I do not know in advance the number of times word appears in the file.

bac0n avatar
cn flag
`IFS=: read a _ < <(grep -wn 'word' text.txt | tail -1) && head -n $a text.txt`
us flag
@bac0n Thanks that did the trick
Zanna avatar
kr flag
@bac0n you seem to have written your answer in the comments section by mistake
Score:0
cn flag

Assuming that "word" or the marker text does not appear in any of the lines preceding the second "word" then this should do the job:

awk '{if($0!="word"){print} else {c=c+1;{print} if(c==2){exit}}}' in-file > out-file
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.