0

I have a very long file with lines with multiple columns of numbers -- more than 3 columns per each line except for one line. It's a very large file, and I'm trying to locate a line that has 13 only. Since it's a big file, when I grep 13, I get many many lines with numbers that have 13 somewhere in them. How can I use grep such that only lines containing 13 with wc=1 are returned?

1

grep(1) has an option to search for only entire lines that match a specified regexp:

 -x, --line-regexp
         Only input lines selected against an entire fixed string or
         regular expression are considered to be matching lines.

Assuming that the lines you want to match contain no whitespace, the command:

grep -x 13

will provide the output you're seeking.

OTOH, if the lines you seek to match do contain leading and/or trailing whitespace, then you need a more complex pattern:

grep -x '[[:blank:]]*13[[:blank:]]*'
  • Thanks for the input. Wouldn't that be a problem if the line has space characters? – sodiumnitrate Feb 14 at 19:23
  • 1
    @sodiumnitrate Yes, the answer mentions that. I've made an edit to handle the case where the line does contain whitespace. – Jim L. Feb 15 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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