LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 04-04-2020, 06:15 AM   #1
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Rep: Reputation: 2
Word That Begin With q and end with k


I have a sentence: the question, did the quick brown foxxxxxx quack wearing nik


Here, I want to extract words starting with q and ending with k using preferably grep.

What I got so far is:

Code:
grep -P "\bq.*?k\b" regex.txt
It yields 2 matches: question, did the quick and quack

How can I yield quick and quack from the sentence using regex?
 
Old 04-04-2020, 06:19 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
For the pattern itself, you might need the \w operator instead of .* because the latter would get any characters including spaces.

For grep specifically, take a look at adding the -o option.
 
Old 04-04-2020, 06:20 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,838

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
you need to replace . (dot) in your regexp, because that will match everything, but you only want to look for words (\w).
 
Old 04-04-2020, 06:37 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Or to put it another way, you need to make the regex non-greedy. I've never seen the need to use (experimental) perlre in grep - that's what perl was invented for ... :shrug:
 
Old 04-04-2020, 06:42 AM   #5
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 136

Original Poster
Rep: Reputation: 2
The solution that is working for me is:

Code:
grep -P "\bq\w*k\b" regex.txt

Last edited by blueray; 04-04-2020 at 06:43 AM.
 
  


Reply

Tags
grep, grep regex gnu, regex, regexp



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
regex for phrase like'word-word-word' Zero4 Linux - General 9 07-06-2019 06:36 AM
sed append word at end of line if word is missing franjo124 Linux - Newbie 3 03-08-2012 08:41 PM
[SOLVED] Script to remove single quote that begin and end file names SteveInTallyFL Programming 6 08-16-2010 09:31 AM
wanna to add in newlines in begin and end of multiple files tiaratiara Linux - Newbie 8 03-18-2009 09:32 PM
LXer: KDE 4.0 beta 4 released, extensive end user testing to begin LXer Syndicated Linux News 0 11-10-2007 12:20 PM

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

All times are GMT -5. The time now is 12:53 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