rubing Posted August 7, 2008 Share Posted August 7, 2008 I am using grep to try and find a url in a text file. typically i only know a single pattern in the url (e.g. 'tresure.com') So, Grep does a great job of finding the line which contains the pattern (e.g. 'come to our site: www.buried-treasure.com') However, I would like to extract/parse just the url. from this line, that grep is detecting. Quote Link to comment https://forums.phpfreaks.com/topic/118536-grep-for-words/ Share on other sites More sharing options...
trq Posted August 7, 2008 Share Posted August 7, 2008 So are you saying this 'come to our site: www.buried-treasure.com' is your actual pattern? Or are you looking for any and all urls within a file? You need to be more specific. Quote Link to comment https://forums.phpfreaks.com/topic/118536-grep-for-words/#findComment-610275 Share on other sites More sharing options...
neylitalo Posted August 7, 2008 Share Posted August 7, 2008 grep treasure.com urls | sed -r -e "s/.*?(www.*treasure.com).*/\1/g" This is buggy, though. It only works if there is no http(s):// prefixing the URL, and only if there is a www in front of it. If someone can find a way to fix it, please do - I have trouble with multiple zero-or-one expressions. Quote Link to comment https://forums.phpfreaks.com/topic/118536-grep-for-words/#findComment-610412 Share on other sites More sharing options...
trq Posted August 7, 2008 Share Posted August 7, 2008 grep -E -o 'www\.(.*)-treasure\.com' foo Quote Link to comment https://forums.phpfreaks.com/topic/118536-grep-for-words/#findComment-610440 Share on other sites More sharing options...
rubing Posted August 7, 2008 Author Share Posted August 7, 2008 sorry thorpe. yes i am just looking to extract the url from the line of interest. I think I can work with what you wrote here. using sed is also seems a good idea, but would prefer quick and dirty since i've been told it has a steep learning curve. thx all! Quote Link to comment https://forums.phpfreaks.com/topic/118536-grep-for-words/#findComment-610844 Share on other sites More sharing options...
trq Posted August 7, 2008 Share Posted August 7, 2008 sorry thorpe. yes i am just looking to extract the url from the line of interest. It just seems that you already have most of what you want in your pattern. Can we see an example of an entire line and your pattern? Quote Link to comment https://forums.phpfreaks.com/topic/118536-grep-for-words/#findComment-610849 Share on other sites More sharing options...
effigy Posted August 7, 2008 Share Posted August 7, 2008 Try this: perl -n -e 'print "$1\n" if /(\S*?treasure\.com\S*(?<!\p{P}))/i' file_name Quote Link to comment https://forums.phpfreaks.com/topic/118536-grep-for-words/#findComment-610899 Share on other sites More sharing options...
rubing Posted August 7, 2008 Author Share Posted August 7, 2008 These are all really great ways!!! thx guys. I think the -o option on grep is what I was really after...it works fine. Quote Link to comment https://forums.phpfreaks.com/topic/118536-grep-for-words/#findComment-611128 Share on other sites More sharing options...
Ghulam Yaseen Posted October 13, 2008 Share Posted October 13, 2008 Hello ; Please try this out -------- cat file_name | grep key_word > file_name; cat file_name -------- I am using grep to try and find a url in a text file. typically i only know a single pattern in the url (e.g. 'tresure.com') So, Grep does a great job of finding the line which contains the pattern (e.g. 'come to our site: www.buried-treasure.com') However, I would like to extract/parse just the url. from this line, that grep is detecting. Quote Link to comment https://forums.phpfreaks.com/topic/118536-grep-for-words/#findComment-663638 Share on other sites More sharing options...
corbin Posted October 13, 2008 Share Posted October 13, 2008 You bumped this two months why? Quote Link to comment https://forums.phpfreaks.com/topic/118536-grep-for-words/#findComment-663661 Share on other sites More sharing options...
Ghulam Yaseen Posted October 14, 2008 Share Posted October 14, 2008 :)i recently became a member of this forum You bumped this two months why? Quote Link to comment https://forums.phpfreaks.com/topic/118536-grep-for-words/#findComment-664646 Share on other sites More sharing options...
trq Posted October 14, 2008 Share Posted October 14, 2008 :)i recently became a member of this forum You bumped this two months why? Welcome to the boards, however.... Running around bumping old solved threads is considered bad form on any forum. Please refrain from doing so. Quote Link to comment https://forums.phpfreaks.com/topic/118536-grep-for-words/#findComment-664801 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.