Operating System - HP-UX
1752604 Members
4879 Online
108788 Solutions
New Discussion юеВ

grep exact syntax with some spaces in front

 
SOLVED
Go to solution
yc_2
Regular Advisor

grep exact syntax with some spaces in front

Hi,

How to grep the exact syntax with some spaces in front of the syntax?

eg: grep only abc from the following in a text file:
:
abcde
abc
:



Rgds,
YC
6 REPLIES 6
Con O'Kelly
Honored Contributor

Re: grep exact syntax with some spaces in front

Hi

Use the following syntax:

$ grep "^abc "

OR

$ grep " abc "

The '^' greps for any line begining with 'abc'

Cheers
Con
yc_2
Regular Advisor

Re: grep exact syntax with some spaces in front

Hi Con O'Kelly,

There is 3 to 4 spaces in front of abc after the line abcde. No result from your 1st solution.

Both abcde and abc will be output. Could it just output abc only.

Donald Kok
Respected Contributor

Re: grep exact syntax with some spaces in front

grep "abc$" filename
My systems are 100% Murphy Compliant. Guaranteed!!!
Eknath
Trusted Contributor
Solution

Re: grep exact syntax with some spaces in front

Hi Leong,

I think you can try following...
#grep " abc " filename

here give exact no of spaces before and after abc in codes.

This should help

Cheers!!!
eknath
Ermin Borovac
Honored Contributor

Re: grep exact syntax with some spaces in front

$ grep -E "^[ ]+abc"

or

$ grep -w abc
Ermin Borovac
Honored Contributor

Re: grep exact syntax with some spaces in front

First grep from my last post should have a space character inside square brackets.

$ grep -E "^[]+abc"

It can be also written as

$ grep -E "^[[:space:]]+abc" testfile