LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 03-07-2011, 04:47 PM   #16
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946

It's generally good to give a variety of solutions, even if only for educational purposes, which is the main reason I posted what I did. I agree that in this particular case awk may indeed be the more efficient solution, but I was more concerned with demonstrating how regular expressions work than in what works "best" here. And it may help some reader down the line working on something similar; someone who doesn't have access to or understand how to use awk, for example, or who needs a regex solution specifically.
 
2 members found this post helpful.
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 03-07-2011, 05:01 PM   #17
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Dark Helmet, your arguments are reasonable and well phrased. Evidently, the slightly sarcastic tone you used in your previous post has not been well accepted by Telengard and he overreacted to criticism. Now that your reasons are more clear, we hope that the does not lead to anything discussion will terminate and that you will shake your hands.
 
Old 03-07-2011, 05:06 PM   #18
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
David the H., thank you for clarifying.
 
Old 03-07-2011, 07:26 PM   #19
fancylad
Member
 
Registered: Mar 2008
Distribution: slackware
Posts: 175

Rep: Reputation: 19
what about using perl?

Quote:
matt@amd:~$echo -e "Fable Mabel\nHairy Mary \nMary Martian" | perl -lane 'print if $F[1] =~/^M/'
Fable Mabel
Hairy Mary
Mary Martian

Last edited by fancylad; 03-07-2011 at 07:29 PM.
 
Old 03-07-2011, 08:08 PM   #20
kurumi
Member
 
Registered: Apr 2010
Posts: 223

Rep: Reputation: 45
Quote:
Originally Posted by Telengard View Post
awk already considers tabs and normal space characters to be whitespace. My awk command is only 13 characters, while your egrep weighs in at a whopping 35 characters. How is your egrep different or better than this?

Code:
awk '$NF~"M"'
use // instead of ". and your regex is wrong.
Code:
awk '$NF~/^M/'
 
0 members found this post helpful.
Old 03-08-2011, 02:57 AM   #21
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Quote:
Originally Posted by kurumi View Post
use // instead of ". and your regex is wrong.
Code:
awk '$NF~/^M/'
Please let's stop to address other members' solutions as wrong or worse. The difference between
Code:
'$NF ~ "M"'
and
Code:
'$NF ~ /^M/'
is that the former uses a so-called dynamic regexp (even if it's actually a string constant), the latter uses a regexp constant. In both cases the expression is very simple and shouldn't bring to any problem, but there are some situations where the usage of one is preferred to the other. See http://www.gnu.org/software/gawk/man...mputed-Regexps for an insight. Furthermore the additional anchor ^ is not really needed as per OP's requirements, since the M (if present) will be the first character in the last word. Checking if it is present should be enough.

Please note the thread has been marked as solved by the OP, therefore let's add further information only if they are really valuable and give an actual improvement to the technical discussion. Thanks.
 
1 members found this post helpful.
  


Reply

Tags
awk, grep, regex, regular expressions


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
print second word in 1st line along with 5th word in all the lines after the first bangaram Programming 5 08-31-2009 03:42 AM
grep a word from some files??? shahz Red Hat 3 09-06-2008 03:48 AM
select all text between a patteren using grep mauran Programming 22 07-14-2007 06:30 PM
text select Abi Word thegreatgatsby Linux - Software 8 02-20-2004 03:51 AM
grep to NOT select a word robertmarkbram Programming 2 08-21-2003 10:48 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration