LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   grep command problem (https://www.linuxquestions.org/questions/linux-newbie-8/grep-command-problem-4175581443/)

torito 06-04-2016 01:46 PM

grep command problem
 
1 Attachment(s)
Hi guys!

I have a file called text1.txt which contains:

one is not as much as ten!
onerous he could be, often
one says you can’t be beaten

I am running this command to see which lines would be matched:
grep ‘^one’ text1.txt | grep ‘ten$’

The problem that I have is the output is different of what I think it should be. My thinking process is grep ‘^one’ text1.txt will display the lines that start with one, so the three lines should be printed. Then, the output will be sent as input to grep ‘ten$’.
grep ‘ten$’ will display the lines that end with ten from the input. So the final output should be:

one is not as much as ten!
onerous he could be, often
one says you can’t be beaten



But instead I am getting this output from console.

onerous he could be, often
one says you can't be beaten



From you guys that know a lot about Linux, would you please explain this? thank you very much as always!

Shadow_7 06-04-2016 01:57 PM

The first line ends in a punctuation. ! . And therefor does not end with ten.

torito 06-04-2016 02:05 PM

UPSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS!!!!!!!

I was soooo focus on "ten" that I missed that. Thank you soooo very much for your help!

Turbocapitalist 06-04-2016 02:12 PM

If you have GNU grep (or others), you can do that in one move.

Code:

grep -e '^one' -e 'ten$' text1.txt
There are several ways you can deal with the punctuation mark at the end of the line. One way is with standard character classes found in POSIX regular expressions:

Code:

grep -e '^one' -e 'ten[[:punct:]]*$' text1.txt
See the manual page for regex about that.

Code:

man 7 regex
However, there are also extended regular expression with the -E option. And if you want to get really fancy you can use perl-compatible regular expresssions with GNU grep via the -P option.


All times are GMT -5. The time now is 12:25 AM.