1752777 Members
6361 Online
108789 Solutions
New Discussion юеВ

grep'ping for newline

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

grep'ping for newline

If i have this in VAR
echo $VAR
2

and I want to make sure it's not confused with
20
.
.
299

How do I grep for a newline character or blank

ie
cat /tmp/file
2
3
21

cat /tmp/file | grep '2'
2
21

prefer
cat /tmp/file | grep '2"***Insert_your_10_points_here**"'
2


Later,
Bill
It works for me (tm)
8 REPLIES 8
Solution

Re: grep'ping for newline

In regular experssions '$' is the end of a line, so:

cat /tmp/file | grep '2$'

will match

2

but not:

20
2999

etc.


HTH

Duncan

I am an HPE Employee
Accept or Kudo
Carlos Fernandez Riera
Honored Contributor

Re: grep'ping for newline

2$
unsupported
Robin Wakefield
Honored Contributor

Re: grep'ping for newline

Hi Bill,

grep '2$' filename

$ anchors the grep to the end-of-line
^ anchors to the beginning.

Rgds, Robin.
Thierry Poels_1
Honored Contributor

Re: grep'ping for newline

hi,

how about
cat /tmp/file | grep '2$'

regards,
Thierry
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.
Vincent Farrugia
Honored Contributor

Re: grep'ping for newline

Hello,

grep -x should do the trick.

HTH,
Vince
Tape Drives RULE!!!

Re: grep'ping for newline

And '^' is the start of a line, so to be sure of matching the *exact* contents of a line you could do:

cat /tmp/file | grep '^2$'

which will match on

2

only, and NOT

foobarbaz2999

HTH

^Duncan$
;o)

I am an HPE Employee
Accept or Kudo
Vincent Farrugia
Honored Contributor

Re: grep'ping for newline

I mean grep -x '2'

Vince
Tape Drives RULE!!!
Thierry Poels_1
Honored Contributor

Re: grep'ping for newline

Hi,

and how about
grep "id2[[:space:]]"

this will also work for id's followed by whitespace and more text.

regards,
Thierry.
All unix flavours are exactly the same . . . . . . . . . . for end users anyway.