LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2017, 03:27 PM   #16
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,586
Blog Entries: 11

Rep: Reputation: 3618Reputation: 3618Reputation: 3618Reputation: 3618Reputation: 3618Reputation: 3618Reputation: 3618Reputation: 3618Reputation: 3618Reputation: 3618Reputation: 3618

Welcome to LQ vaibhav5587!

You have replied to a thread that has been quiet for seven years.

If you have a similar problem or simply wish to share your own knowledge, please consider starting your own thread to get better exposure and attract discussion from currently active members.
 
Old 11-02-2017, 05:25 PM   #17
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,680

Rep: Reputation: 759Reputation: 759Reputation: 759Reputation: 759Reputation: 759Reputation: 759Reputation: 759
If you have users like "andy.stewart" where the dot is a wildcard character in RE then I would use a string match in awk.
Code:
awk -F: '$1=="andy.stewart" {print; m=1} END {exit !m}'
An interesting variant is to quit after the first match
Code:
awk -F: '$1=="andy.stewart" {print; m=1; exit} END {exit !m}'
Note that an exit in the main loop jumps to the END section.
 
Old 11-05-2017, 02:03 AM   #18
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,848

Rep: Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013Reputation: 2013
I'd just like to focus for a second on the conceptual background at work here.

Regular expressions are designed for matching, amazingly enough, text strings that follow regular patterns. So to properly create a regex rule, you first have to step back and define for yourself exactly what it is that you need to match; to find a pattern that makes the string you want unique from all other similar ones you may encounter. And preferably the simplest one, if multiple possibilities are available.

Remember that regex processes lines from left to right, so your goal is often as much or more about properly matching the starting and ending points of the string as the content itself. Does "string" always start with the same letter? Is it always preceded and/or followed by the same character or pattern? Is it always found in the same location in the line, or is always the same length? Is some character always found in the string, or never found in it? Does "I" always come before "E", except after "C"? Things like that.

If you cannot clearly define the string in a regular way like this, you can't use regex on it, naturally.

So in the OP, you want to match the username in /etc/passwd. Since that file is well-defined and regularly delimited, and the username is always the first entry on the line, it turns out that the string that works best is simply "line start" + "username" + "colon". Now that we have properly defined the problem, the regex itself is easy enough to construct. All too often users over-complicate things by trying to match something much more complex than is really needed.

Of course the opposite often happens too; you create a simple regex that appears to work just fine, only to find that the input is more variable than initially expected and it fails to always match as planned. Again, properly defining the matching pattern first helps to avoid such problems.

It also goes without saying that you have to understand how regex works, as well as the tool you are using to apply it with. That just takes study and experience.


And finally, with all this said, I would actually recommend awk in this case instead, such as that posted by MadeInGermany just above. awk is designed specifically for working on regularly-delimited text strings like this. The right tool for the right job and all that jazz.
 
Old 02-14-2019, 07:56 AM   #19
ericnyamu
LQ Newbie
 
Registered: Mar 2015
Posts: 6

Rep: Reputation: Disabled
Quote:
Originally Posted by lesca View Post
Try this:

cat yourfile | cut -f1 -d: | grep '\<word\>'

Note:
1. both single quote and the backslash are necessary
2. it is ':' following -d
 you may change this delimiter to suit future file format.

Explanation:
Your problem is how to cut the right column, because grep only grab the lines which matches your patten.

Hope this may solve your problem
thanks it worked. pretty neat
 
Old 02-14-2019, 08:36 AM   #20
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,664

Rep: Reputation: 6919Reputation: 6919Reputation: 6919Reputation: 6919Reputation: 6919Reputation: 6919Reputation: 6919Reputation: 6919Reputation: 6919Reputation: 6919Reputation: 6919
::removed::
 
  


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
grep question - match exact string Panagiotis_IOA Linux - General 2 01-20-2014 04:34 AM
[SOLVED] Match datetime by the minute (not an exact match by the second) [mysql] hattori.hanzo Programming 1 10-21-2010 05:43 PM
how to match the exact pattern using grep utility vinaytp Linux - Newbie 3 05-11-2009 12:36 AM
grep/sed/awk - find match, then match on next line gctaylor1 Programming 3 07-11-2007 08:55 AM
how to find an exact substring match? ldp Programming 7 02-22-2005 06:28 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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