Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

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

The data I am working with data that has this format:

1880    20  David   7570    Mabel   13096

I need to pull the year (1880), the rank (20), and the name only (David or Mabel). The end result is:

1880        20      David


1880        20      Mabel

I have been successful in just pulling the year, rank and name seperately, but I have issues putting together a whole regular expression. I know the basics of regex, but I cannot currently access my notes.

I am attempting to use egrep.

share|improve this question
up vote 2 down vote accepted

I'd recommend a Python solution (I don't know what you're currently using):

import re

re_find_data = re.compile(r'^(\d+)\s+(\d+)\s+(\S+)\s+(\d+)\s+(\S+)\s+(\d+)$')

for line in open(r'/path/to/file'):
    for match in re_find_date.findall(line):
        # Do something with 'match'

        # You can index the 'match' tuple like so:
        # Print 3rd part (name)

Regarding grep

I'm not sure grep can be used in this case. It lacks the ability to print specific numbered groups, which is what you need (I believe). I'd be interested to hear if your professor (or anyone else) has a solution with pure grep.

This Regex should work, but you'd need to extract the fields you want from the numbered groups (demonstrated in Python example):


grep is just not the right tool. A Regex is, but under a different implementation (hello, Python!).

share|improve this answer
Sadly, I'm fairly limited with what I can use. I cannot use Python in this case. I have to keep within the bounds of what I have been currently taught in the class. – gbrooks Feb 12 '14 at 10:33
@gbrooks: sigh. I'll see what I can do with grep. – minerz029 Feb 12 '14 at 10:33
Thanks. This becomes more difficult as it gets later along with my course site crashing (along with my professor's walkthrough). I appreciate the help. – gbrooks Feb 12 '14 at 10:35
@gbrooks: Don't think it's possible with grep. See edit for more details. – minerz029 Feb 12 '14 at 10:43
I believe you are correct with it not being able to be just grep. I'm going to start a pipeline with grep pulling the desired name and using cut for the excess. Thank you for your help. If I do get a good answer from my professor I'll post it here. – gbrooks Feb 12 '14 at 10:47
grep -Po '\d+\s+\d+\s+\w+' file.txt
share|improve this answer
That only pulled the year and rank. How can I print the desired name as well? – gbrooks Feb 12 '14 at 10:30
Hi @gbrooks, it's rather easy to extend, maybe just read some more of the man pages to ensure you understand grep well? This is a very helpful skill to develop. – Forkrul Assail Feb 12 '14 at 10:49
@TiGR I have spent a good amount time on the man page for grep. For now I will just use a combination of grep and cut until I can contact my professor. – gbrooks Feb 12 '14 at 10:53
Sorry, don't know how I managed to miss last \w+. – TiGR Feb 13 '14 at 9:00

This was impossible (at least from my scope of the class) with just using grep. I was informed a few days later by my professor that he wanted us to use a combination of commands to achieve the desired result. However, all of the responses were valid and worked when I tried them. This is just meant as a direct answer to this question based on very beginner knowledge (someone of my current Ubuntu experience).

share|improve this answer

Your Answer


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.