LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 06-10-2016, 11:34 PM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
Question Extracting pathnames from a string morass


Can't grep pathnames out of string
I have a string:
Code:
tmp=Only in /home/a/.config/cairo-dock/third-party/Clear/icons: pcmanfm.svg Only in /home/a/.config/cairo-dock/third-party/Clear/icons: system-file-manager.svg Only in /home/a/.config/cairo-dock/third-party/Clear/icons: thunar.svg Only in /home/a/.config/cairo-dock/third-party/Clear/icons: thunderbird.svg Only in /home/a/.config/cairo-dock/third-party/Clear/icons: utilities-system-monitor.svg Only in /home/a/.config/cairo-dock/third-party/Clear/icons: utilities-terminal.svg
I would like to extract anything that is a path. "grep" comes to mind and I utilize the pattern that any 'path' will start with a "/". My attempts below:


Code:
$ echo $tmp | grep /
$ echo $tmp | grep ^/
$ echo $tmp | grep -w ^/
$ echo $tmp | grep -w /
$ echo $tmp | grep -w "/"
$ echo $tmp | grep -w '\</'
$ echo $tmp | grep -w '\:\>'
$ echo $tmp | grep -w ':\>'
$ echo $tmp | grep -w '\</*:\>'
$ echo $tmp | grep '\</*:\>'
Results
{fail}

Unfortunately the above attempts yeild no results. It was interesting that when I tried
Code:
$ echo $tmp | grep /
it printed out the entire string, which is why I used -w quite often.

Lastly I wondered how the above commands could be printing out to standard error, so I tried:

Code:
$ echo $tmp 2>&1 | grep /
$ echo $tmp | grep / 2>&1
$ echo $tmp | grep -w / 2>&1
Alas, still I couldn't extract the path.

Can the paths in the above string be extracted? Should I use a 'for in 'echo list'; do * ; done' loop?
 
Old 06-11-2016, 12:42 AM   #2
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Code:
$ echo $tmp | grep -oE '(/[^/: ]+)+'
/home/a/.config/cairo-dock/third-party/Clear/icons
/home/a/.config/cairo-dock/third-party/Clear/icons
/home/a/.config/cairo-dock/third-party/Clear/icons
/home/a/.config/cairo-dock/third-party/Clear/icons
/home/a/.config/cairo-dock/third-party/Clear/icons
/home/a/.config/cairo-dock/third-party/Clear/icons
PS: Not sure about part after colon -- is that belong to a path?

Last edited by firstfire; 06-11-2016 at 12:45 AM.
 
1 members found this post helpful.
Old 06-11-2016, 01:25 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I went slightly simpler:
Code:
$ grep -oE '/[^:]+' <<< "$tmp"
/home/a/.config/cairo-dock/third-party/Clear/icons
/home/a/.config/cairo-dock/third-party/Clear/icons
/home/a/.config/cairo-dock/third-party/Clear/icons
/home/a/.config/cairo-dock/third-party/Clear/icons
/home/a/.config/cairo-dock/third-party/Clear/icons
/home/a/.config/cairo-dock/third-party/Clear/icons
I presume the original setting of the variable is simply missing the quotes around the string
 
1 members found this post helpful.
Old 06-12-2016, 08:58 AM   #4
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Talking [SOLVED] grep -oE '(/[^/: ]+)+' PERFECT!!

Quote:
Originally Posted by firstfire View Post
Code:
$ echo $tmp | grep -oE '(/[^/: ]+)+'
Thanks firstfire, you delivered an answer with pinpoint accuracy!!

Any online learning sources you recommend for learning grep?
 
Old 06-12-2016, 09:23 AM   #5
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hi.

I believe grail's solution is cleaner for this particular task.

Any online tutorial on regular expressions will do.
I use man grep, info grep, ditto for sed, awk and perl.
Just type
Code:
info grep regular expressions
into your terminal and read.
 
1 members found this post helpful.
Old 06-12-2016, 09:24 AM   #6
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Talking I really find this useful!

Quote:
Originally Posted by grail View Post
I went slightly simpler:
Code:
$ grep -oE '/[^:]+' <<< "$tmp"
Actually this yields:
Code:
$ grep -oE '/[^:]+' <<< "$tmp"
/home/a/.config/cairo-dock
/home/a/.config/cairo-dock
/home/a/.config/cairo-dock/current_theme and /media/a/REFIND/recent/AC/bckup/Install
...
/home/a/.config/cairo-dock/third-party and /media/a/REFIND/recent/AC/bckup/Install/3_UbuntuLXDE14.04/home/a/.config/cairo-dock/third-party
The "and" above between two paths isn't desirable, Of course this could be a typo, bash version incompatibility or maybe I misread it since the following command yields choice results (merely insert a space between ":" and "]") :
Code:
$ grep -oE '/[^: ]+' <<< "$tmp"
I found the ending <<< "$tmp" interesting and your statement's brevity gave this command a lighter feeling. THANKS!
 
Old 06-12-2016, 10:40 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I had no space in my original due to none in the example
 
1 members found this post helpful.
  


Reply

Tags
extract, grep, pipe, string, word



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
Extracting integers from a string teresevo1 Programming 3 11-08-2010 08:24 PM
Extracting a Number from a String nilly16 Programming 15 05-26-2009 07:37 AM
C: Extracting part of a string trevorv Programming 3 08-29-2007 04:36 PM
String extracting / string operation Xeratul Linux - General 24 02-13-2007 02:54 PM
extracting more than one value from a string ganninu Programming 16 12-10-2003 03:26 AM

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

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