I have a huge text file (~1.5GB) with numerous lines ending with ".Ends".
I need a linux oneliner (perl\ awk\ sed) to find the last place '.Ends' appear in the file and add a couple of lines before it.
I tried using tac
twice, and stumbled with my perl:
When I use:
tac ../../test | perl -pi -e 'BEGIN {$flag = 1} if ($flag==1 && /.Ends/) {$flag = 0 ; print "someline\n"}' | tac
It first prints the "someline\n" and only than prints the .Ends
The result is:
…
.Ends
someline
When I use:
tac ../../test | perl -e 'BEGIN {$flag = 1} print ; if ($flag==1 && /.Ends/) {$flag = 0 ; print "someline\n"}' | tac
It doesn’t print anything.
And when I use:
tac ../../test | perl -p -e 'BEGIN {$flag = 1} print $_ ; if ($flag==1 && /.Ends/) {$flag = 0 ; print "someline\n"}' | tac
It prints everything twice:
…
.Ends
someline
.Ends
Is there a smooth way to perform this edit?
Don't have to be with my solution direction, I'm not picky...
Bonus - if the lines can come from a different file, it would be great (but really not a must)
Edit
test input file:
gla2
fla3
dla4
rfa5
.Ends
shu
sha
she
.Ends
res
pes
ges
.Ends
--->
...
pes
ges
someline
.Ends
# * some irrelevant junk * #
.Ends
? – Thromboembolism.Ends
) it would matter when coming up with a solution, ie, it's easier to always replace the last line – Thromboembolismit's not relevant
- yes, it is. If you don't state in your question that there could be lines after the last.Ends
and don't include lines after the last.Ends
in your example then someone trying to help you might reasonably create and test a solution that relies on.Ends
being the last line and thereby waste their time and, to a much lesser extent, yours. – Pilpul.Ends
line in your input now - can that really be present or is it a mistake? – Pilpullines ending with ".Ends"
, notlines ending with ".Ends" possibly followed by spaces or other characters
. Does this mean the lines might also befoobar.Ends
orfoo.Ends.bar
or other sequences of characters with.Ends
in the middle? I don't know what2 whitespaces, to skip line.
andif skip line can be taken without them
means. – Pilpul