Ryan Sisson
Ryan Sisson

Reputation: 139

grep command to display lines with leading and trailing blanks

I have a test folder that contains some text:

1  Line one. $
2      Line $
3 This is a new line $
4 This is the same line$
5                 $

How would I go about using a PIPED grep command to only displays lines 1, 2 and 5? My attempt at this was grep -n '^ ' test | grep -n ' $' test However I get lines 1, 2, 3, 5

Upvotes: 0

Views: 1344

Answers (1)

user2719058
user2719058

Reputation: 2233

Your second grep innvocation in the pipe reads the file test and ignores the pipe. The first grep writes to the pipe, but its output is discarded. So, your command effectively is identical to

grep -n ' $' test

Just remove the test argument from the second grep invocation.

You are probably better off removing the -n option there as well, because it will count the lines it reads from the pipe then, which is probably not what you want.

Upvotes: 2

Related Questions