I have some text files that I want to grep a section of code from. The goal I am trying to achieve is to start the view at a certain line & then be able to read anything below it. For example. In the text below, how do I view the text file at the start point of yellow. I want to view the content of "yellow" as well as everything underneath it, regardless of what that content is.
green
blue
cyan
magenta
purple
brown
yellow
red
orange
more orange
more blue
this is enough
AWK Use
AWK
- that's the simplest as it can get:Sample run
Grep
You could also use
grep
with--after-context
option, to print certain amount of lines after the matchFor automatic setting of context, you could use
$(wc -l textfile.txt)
. The basic idea is that if you have a very first line as a match and you want to print everything after that match, you will need to know the number of lines in file minus 1. Luckly,--after-context
won't throw errors about the number of lines, so you could give it number completely out of range, but in case you don't know it, total number of lines will doIf you want to shorten the command
--after-context
is the same option as-A
and$(wc -l textfile.txt)
, will expand to number of lines followed by file name. So that way you typetextfile.txt
only oncePython
Or alternatively without
printable
flagYou can do it by:
where "file" is the file name containing your text.
Not
grep
, but usingsed
:-n
: inhibits printing/^yellow$/,$
: address range that goes from the first occurence of a line matching exactlyyellow
to the last line inclusivep
: prints the lines in the address rangeLate to the party :)
Using
grep
:-P
enables us to use Perl compatible Regex-z
makes the input file separated by ASCII NUL, rather that newline-o
takes only the desired portion(?s)
is the DOTALL modifier, enables us to match newline using token.
(any character)In
\n\K
,\n
matches a newline,\K
discards the matchyellow\n.*
matchesyellow
followed by a newline and everything after this is selected too and shown in the output.Example:
Using little
python
:lines
is the list containing all the lines of the file (with trailing newlines too)lines.index('yellow\n')
gives us the lowest index oflines
whereyellow\n
is foundlines[lines.index('yellow\n'):]
will use list slicing to get the portion starting fromyellow\n
till endjoin
will join the elements of the list to output as a stringSince the question refers to viewing the file, there's always the good ol'