0

This is my text file:

line A line B line C 

Now I'm trying to find the line that ends with A:

cat foo.txt | grep 'A$' 

BTW:

$ grep --version GNU grep 2.6.3 
2
  • 1
    what's wrong with the above grep? does it print nothing? Commented Mar 28, 2015 at 1:33
  • 2
    Are you on Windows, perhaps? What end-of-line character is being used? If you run cat -e foot.txt do you see ^M or $? Commented Mar 28, 2015 at 1:34

2 Answers 2

1

There is nothing wrong with your grep command. But there may be a chance of space characters exists after the last A. so use the below grep command.

grep 'A\s*$' file 

\s matches any kind of horizontal or vertical whitespace character.

OR

You could use the POSIX character class [[:space:]] to match any kind of space character.

grep 'A[[:space:]]*$' file 
Sign up to request clarification or add additional context in comments.

1 Comment

\s only works in newer versions of GNU grep. Use [[:space:]] for portability.
0

If last character is always a single one, this awk should do:

awk '$NF=="A"' file line A 

It test if last field equals to A
This will also work if there are hidden spaces/tabs after last character.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.