I've just started learning Linux and all of the relatively basic commands over the past month, but I am having some trouble using the Grep command. I'm currently stuck on how to use the Grep command to search only one column of a CSV file for a numeric string that starts with two specified numbers. I'm specifically trying to list the lines that have a zip code that starts with "48", which zip code in this case is included in the 7th column of the CSV file. There is also other numeric data on each line, which is why I need to use the Grep command only on the "Provider" column. Any help to point me in the right direction will be appreciated, as I have been trying to figure this out for some time now and I think I may be slightly over-thinking what I need to do.

Here's an example of the CSV data I'm using:

    Provider  Hospital        Total Discharges  Average Covered Charges
    49444     MI - Muskegon   53                18694.9
    49007     MI - Kalamazoo  40                20494.25
    48075     MI - Royal Oak  14                20386.28
    48124     MI - Dearborn   34                34338.47
  • 4
    There aren’t a lot of C’s in that CSV...?
    – Jeff Schaller
    Jan 28, 2018 at 18:20

2 Answers 2


grep doesn't have a concept of fields or columns, so you'd have to write a pattern that somehow accomplishes that. In the general case, that will get ugly, but if the number you want is at the start of line, as the sample seems to indicate, it's simple enough:

grep -Ee '^[[:space:]]*48[0-9]+[[:space:]]' file

That would look for lines that start with any number of whitespace, a 4, an 8, some other numbers, and any whitespace character.

For looking at particular fields, awk is better. By default, it splits the lines to fields on any whitespace. This would look for lines where the first field contains 48, and some other numbers:

awk '$1 ~ /^48[0-9]+$/' file

Change the $1 to $7 or whatever to use another field.

  • The zip code that he wants to find is in the 7th column, if we may believe the OP. Jan 28, 2018 at 18:33
  • @GerardH.Pille, hum, it indeed says so. But they also mention the "Provider" column, which is first in the sample, and the only one that contains numbers that look like that. Perhaps they'll clarify the matter.
    – ilkkachu
    Jan 28, 2018 at 18:38
  • As in Jeff's comment on the OP, I wonder what the OP thinks CSV stands for. Jan 28, 2018 at 18:50

Assuming your file is named test.csv:

grep -E "48[0-9]{3}" test.csv

grep -E search a string using a regexp

48 is the start number you need

[0-9] is a number between 0 and 9 and {3} means 3 times

  • Great this is exactly what I was looking for, thank you very much! Just trying to understand the code a little more what does the "{3}' represent?
    – JoeObe25
    Jan 28, 2018 at 18:19
  • Check regex101.com which is kinda useful trying to develop regexp it explains the statements
    – k4cy
    Jan 28, 2018 at 18:25
  • ok, my point was just to give him the regexp...
    – k4cy
    Jan 28, 2018 at 18:31
  • Would it be possible to also search for lines that contain "MI" in this line as well, so in all I could search for lines that have a zipcode that start with "48" and contain "MI"? I was able to correctly find the lines that have "MI", I'm just wondering if I can combine the two searches into one line.
    – JoeObe25
    Jan 28, 2018 at 18:34
  • 2
    This will match those numbers anywhere in the line.
    – Jeff Schaller
    Jan 28, 2018 at 19:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .