2

I need some help in setting the correct pattern for grep. I need to find all occurrences of pattern where line may have leading space(s). For example: In the following file:

 1. No pattern recognized.
 2. Pattern to be recognized
 3.          Pattern to be recognized here also
 4.  pattern with only one leading space 

I would like to grep only lines 2,3 and 4. The line numbers are just for the reference.

So far I have tried the following:

grep -i '^ [[:blank:]]pattern', but it returns only line 4.

grep -i '[[:blank:]]pattern' returns 1,3 and 4.

grep -i '^[[:blank:]]pattern' returns 1,3 and 4.

-- Mike P.S. If this is not the appropriate forum, then please guide me accordingly.

  • What is the difference between 3 and 4? they both start with space right? – Ravexina Jun 19 '17 at 15:43
  • 1
    Can't you simply match zero or more spaces at the start i.e. grep '^ *Pattern' file? (or '^[ \t]*Pattern' for spaces or tabs; or '^[[:blank:]]*Pattern' for any horizontal whitespace) – steeldriver Jun 19 '17 at 15:52
  • @Ravexina, the diff is number of spaces. As in my first attempt, I was getting only 4 and not 3. – Mike V.D.C. Jun 19 '17 at 16:02
  • Thanks, @steeldriver, I was missing * between the blank and the pattern. Its working! – Mike V.D.C. Jun 19 '17 at 16:03
3

Your line 2 and 3 has upper case P and requires zero or more spaces, so specify exactly that:

$ grep '[[:blank:]]*Pattern'  input.txt                                  
 Pattern to be recognized
         Pattern to be recognized here also

Personally, I'd recommend extending your pattern with something else, like '[[:blank:]]Pattern.*recognized'

  • Thanks. Its working. To take care of uppercase and lowercase, I use grep -i. I was actually missing * between the [[:blank:]] and the pattern in my first attempt. But what I actually wanted was grep '^[[:blank:]]*Pattern' input.txt, otherwise it returns 1 as well. – Mike V.D.C. Jun 19 '17 at 16:05
  • @MikeV.D.C. well, assuming your input is the same as your example, you have leading spaces in each line. So with grep -i it doesn't quite work. I guess it would be much better if we knew the actual data and pattern you're trying to do – Sergiy Kolodyazhnyy Jun 19 '17 at 16:07
  • The numbers in the sample are just for reference. May be I should have mentioned that in the OP. – Mike V.D.C. Jun 19 '17 at 16:10
  • Ah,so lines 1 and 4 actually don't have leading space ? – Sergiy Kolodyazhnyy Jun 19 '17 at 16:11
  • Line 1 doesnt have, but line 4 has one. – Mike V.D.C. Jun 19 '17 at 16:12
2

What I get is you want either no leading space at all, like line #2 or more than of one space, cause you are excluding line #4 which has one space.

so I suggest:

egrep -i '^\s*pattern' file.txt | grep -v '^\spattern'

or using a single awk:

awk 'tolower($0) ~ /^\s*pattern/ && !/^\spattern/ ' file.txt
  • \s as space you can change it with blank if you want.
  • egrep -i '^\s*pattern' file.txt first we get all lines started with or without any times leading space following by pattern.
  • grep -v '^\spattern': then we exclude the ones which contains exactly one leading space.

The above example works on a file without numbering, if your file contains leading numbers use this one:

egrep -i '\s*pattern' file.txt | grep -v '\spattern'

or for awk:

awk 'tolower($0) ~ /\s*pattern/ && !/\spattern/ ' file.txt

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.