Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
up vote 13 down vote accepted

AWK Use AWK - that's the simplest as it can get:

awk '/yellow/,0' textfile.txt

Sample run

$ awk '/yellow/,0' textfile.txt                                
yellow
red
orange
more orange
more blue
this is enough

Grep

You could also use grep with --after-context option, to print certain amount of lines after the match

grep 'yellow' --after-context=999999  textfile.txt

For 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 do

$ grep 'yellow' --after-context=$(wc -l < textfile.txt) textfile.txt
yellow
red
orange
more orange
more blue
this is enough

If 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 type textfile.txt only once

grep "yellow" -A $(wc -l textfile.txt)

Python

skolodya@ubuntu:$ ./printAfter.py textfile.txt                                 
yellow
red
orange
more orange
more blue
this is enough

DIR:/xieerqi
skolodya@ubuntu:$ cat ./printAfter.py                                          
#!/usr/bin/env python
import sys

printable=False
with open(sys.argv[1]) as f:
     for line in f:
        if "yellow" in line:
           printable=True
        if printable:
           print line.rstrip('\n')

Or alternatively without printable flag

#!/usr/bin/env python
import sys

with open(sys.argv[1]) as f:
     for line in f:
        if "yellow" in line:
          for lines in f: # will print remaining lines
             print lines.rstrip('\n')
          exit()
share|improve this answer
    
You can simplify the grep command to grep "yellow" -A $(wc -l textfile.txt). – Byte Commander Jan 30 at 21:40
    
@ByteCommander yup, can be done as well. Just used full option for clarity – Serg Jan 30 at 21:41
1  
@ByteCommander What a lovely hack. Unfortunately it only works because there are no spaces in the file name. – kasperd Jan 31 at 10:39
    
@kasperd Oh yes, you're right. In that case, you would have to fall back to Serg's original command grep "yellow" -A $(wc -l < "my colors.txt") "my colors.txt". – Byte Commander Jan 31 at 12:30

You can do it by:

awk '/yellow/{f=1}f' file

where "file" is the file name containing your text.

share|improve this answer
    
Great minds think alike >:) – Serg Jan 30 at 21:11

Not grep, but using sed:

sed -n '/^yellow$/,$p' file
  • -n: inhibits printing
  • /^yellow$/,$: address range that goes from the first occurence of a line matching exactly yellow to the last line inclusive
  • p: prints the lines in the address range
% sed -n '/^yellow$/,$p' file
yellow
red
orange
more orange
more blue
this is enough
share|improve this answer

Late to the party :)

Using grep:

grep -Pzo '(?s)\n\Kyellow\n.*' file.txt
  • -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 match

  • yellow\n.* matches yellow followed by a newline and everything after this is selected too and shown in the output.

Example:

% grep -Pzo '(?s)\n\Kyellow\n.*' file.txt
yellow
red
orange
more orange
more blue
this is enough

Using little python:

#!/usr/bin/env python2
with open('file.txt') as f:
    lines = f.readlines()
    print ''.join(lines[lines.index('yellow\n'):])
  • lines is the list containing all the lines of the file (with trailing newlines too)

  • lines.index('yellow\n') gives us the lowest index of lines where yellow\n is found

  • lines[lines.index('yellow\n'):] will use list slicing to get the portion starting from yellow\n till end

  • join will join the elements of the list to output as a string

share|improve this answer
    
Nice, but you should mention that the Python code only finds entire lines that equal to "yellow", it does not detect e.g. lines like "more yellow". – Byte Commander Jan 31 at 12:33
    
@ByteCommander From OP's example i think its clear that they want to match just yellow in the line..also if that is not the case then we need to change the python one's algo.. – heemayl Feb 1 at 1:21
    
Yeah, sure. That wasn't criticism anyway, just a hint to improve the answer. Somebody else reading this might assume that the code works like grep and does not match only full lines. I upvoted btw. – Byte Commander Feb 1 at 7:09

Since the question refers to viewing the file, there's always the good ol'

less +/yellow file
share|improve this answer
    
Didn't know less can do that. Very nice ! – Serg Jan 31 at 22:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.