LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-04-2016, 01:46 PM   #1
torito
LQ Newbie
 
Registered: May 2016
Posts: 23

Rep: Reputation: Disabled
Smile grep command problem


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!
Attached Thumbnails
Click image for larger version

Name:	2-grep.jpg
Views:	10
Size:	56.2 KB
ID:	22003  
 
Old 06-04-2016, 01:57 PM   #2
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
The first line ends in a punctuation. ! . And therefor does not end with ten.
 
Old 06-04-2016, 02:05 PM   #3
torito
LQ Newbie
 
Registered: May 2016
Posts: 23

Original Poster
Rep: Reputation: Disabled
Thumbs up

UPSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS!!!!!!!

I was soooo focus on "ten" that I missed that. Thank you soooo very much for your help!
 
Old 06-04-2016, 02:12 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,258
Blog Entries: 3

Rep: Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713Reputation: 3713
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.

Last edited by Turbocapitalist; 06-04-2016 at 02:14 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] problem in grep command prasunjit Linux - Newbie 5 01-18-2013 05:18 PM
problem in using grep command !!! apanimesh061 Programming 5 09-26-2011 01:29 AM
Grep command problem~ Bleek Linux - Newbie 7 12-18-2009 09:49 AM
Problem with simple grep command jacksmash Linux - General 3 09-10-2008 05:01 PM
problem with grep command thodo Linux - Newbie 1 10-25-2007 04:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration