LinuxQuestions.org
Help answer threads with 0 replies.
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 12-25-2016, 06:51 PM   #1
slimcharles
LQ Newbie
 
Registered: Dec 2016
Distribution: Ubuntu 20.04 & Debian 11
Posts: 13

Rep: Reputation: Disabled
Regular expression on apt-cache output


Hi,
I want to easily read apt-cache search output.
So I apply regular expression inside a function for apt-cache search inside .bashrc.

Code:
searchgrep() {
    sudo apt-cache search $1 | grep -E '.*?\ -\ '
}
alias search=searchgrep
If you type
Code:
search finance
then you get this

Sample Output:
erlang-percept - Erlang/OTP concurrency profiling tool
golang - Go programming language compiler - metapackage
golang-1.6 - Go programming language compiler - metapackage
golang-1.6-doc - Go programming language - documentation
golang-1.6-go - Go programming language compiler, linker, compiled stdlib

However all I want is that grep only marks the first part, software name part.
How should I edit the regular expression to look the example below?
Code:
grep -E '.*?\ -\ '
This is what I want to achieve:
erlang-percept - Erlang/OTP concurrency profiling tool
golang - Go programming language compiler - metapackage
golang-1.6 - Go programming language compiler - metapackage
golang-1.6-doc - Go programming language - documentation
golang-1.6-go - Go programming language compiler, linker, compiled stdlib

This is not a big problem but I am curious.
Thanks for your time.
 
Old 12-25-2016, 07:12 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,269

Rep: Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164
regex is generally greedy, as you discovered. One (simple) way to circumvent that is to more specifically specify what you are searching for - "non whitespace" for example rather than "any character" (dot).
 
Old 12-27-2016, 04:37 PM   #3
slimcharles
LQ Newbie
 
Registered: Dec 2016
Distribution: Ubuntu 20.04 & Debian 11
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
regex is generally greedy, as you discovered. One (simple) way to circumvent that is to more specifically specify what you are searching for - "non whitespace" for example rather than "any character" (dot).
Thanks for your reply. Can you give me more specific details, please? Whatever I did, I couldn't find a way.
 
Old 12-27-2016, 07:53 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,023

Rep: Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199Reputation: 3199
You want to look for the first thing you do not want, in your case it would be whitespace. Have a look into the [^...] construct.
 
Old 12-27-2016, 08:54 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,269

Rep: Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164Reputation: 4164
I would like to see a few of those attempts so we can assist.
 
Old 12-28-2016, 12:39 AM   #6
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
You can simply do
apt-cache search --names-only searchpattern
If that ain't enough i do what grail proposed ( "the [^...] construct" )
Most of the times one gets away with --names-only though.
 
1 members found this post helpful.
Old 12-28-2016, 04:21 PM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 2,956

Rep: Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266
*? is actually perl RE for minimum match.
Try
Code:
grep -P '.*? - '
 
Old 12-28-2016, 06:03 PM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by nodir View Post
You can simply do
apt-cache search --names-only searchpattern
Man, I've been chewing on "extra" output in apt search for literally years, and now I know why. Thanks!
If it were me? I'd
Code:
alias <myalias>="sudo apt-cache search --names-only"
in ~/.bashrc or equivalent.

and it spews:
Code:
mysearch finance
[sudo] password for <user>:
libfinance-bank-ie-permanenttsb-perl - perl interface to the PermanentTSB Open24 homebanking
libfinance-qif-perl - Parse and create Quicken Interchange Format files
libfinance-quote-perl - Perl module for retrieving stock quotes from a variety of sources
libfinance-quotehist-perl - Perl modules for fetching historical stock quotes from the web
libfinance-streamer-perl - Perl5 module with interface to Datek Streamer
libfinance-yahooquote-perl - Perl module for retrieving stock quotes from Yahoo! Finance
Have fun!
 
Old 01-03-2017, 06:45 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Distribution: Mint/MATE
Posts: 2,956

Rep: Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266Reputation: 1266
Quote:
Originally Posted by MadeInGermany View Post
*? is actually perl RE for minimum match.
Try
Code:
grep -P '.*? - '
Hmm, still does not work. Well, the man page sais "experimental".
Here is an original perl that returns the matching portion (like grep -o ).
Code:
sudo apt-cache search "$1" | perl -lne 'print $1 if /(.*?) - /'
Or, maybe you can use grep and match from the end?
Code:
sudo apt-cache search "$1" | grep ' - .*'
 
  


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
some incorrect output from "apt-cache unmet" kevinbenko Debian 2 05-18-2016 09:25 AM
How to get output of URL (RegExp) A.K.A Regular Expression Onion master Linux - General 6 04-14-2014 11:01 PM
Output of Regular expression sebelk Linux - General 3 09-03-2013 12:01 AM
[SOLVED] Output of apt-cache policy mahu_mahu Debian 2 05-10-2011 07:00 PM
regular expression Ammad Linux - General 5 08-01-2008 08:41 AM

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

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