LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 07:15 AM   #1
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 114

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, 07:19 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,426
Blog Entries: 3

Rep: Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691Reputation: 2691
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, 07:20 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 15,948

Rep: Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279Reputation: 5279
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, 07:37 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 19,474

Rep: Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453Reputation: 3453
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, 07:42 AM   #5
blueray
Member
 
Registered: Feb 2020
Location: Bangladesh
Distribution: Debian, Ubuntu, Linux Mint
Posts: 114

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 07:43 AM.
 
  


Reply

Tags
grep, grep regex gnu, regex, regexp


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

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

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