LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do I list 'WORDS' containing a specific character? awk,sed,grep? (http://www.linuxquestions.org/questions/linux-newbie-8/how-do-i-list-words-containing-a-specific-character-awk-sed-grep-4175422504/)

rohit.dhaval1 08-16-2012 06:14 PM

How do I list 'WORDS' containing a specific character? awk,sed,grep?
 
I have a large file containing thousands of lines, I want to list only strings containig "::".

File example:
Data::Compare 1.22 (1.2101 < 1.22)::
Data::Dumper::Concise 2.020 (1.100 < 2.020)
Hash::Merge 0.12 (0.10 < 0.12)::
Path::Class 0.18 (0.16 < 0.18)::
Service-now INC10056109 Install Perl Sort::Topological module Primary 0.02 5.8.8, 5.14.0 TESTING ::
String::Escape 0::
String::Escape 0::
Data::Compare 0::
----------------------
Expected output:
Data::Compare
Data::Dumper::Concise
Hash::Merge
Path::Class
Sort::Topological


How can I achieve it?

sycamorex 08-16-2012 06:17 PM

What about the line starting with Service? It does contain :: Or the one starting with Path?
What's wrong with it?
You need to be more specific

rohit.dhaval1 08-16-2012 06:25 PM

Quote:

Originally Posted by sycamorex (Post 4756065)
What about the line starting with Service? It does contain :: Or the one starting with Path?
What's wrong with it?
You need to be more specific

No matter how the line starts or ends, all I need is list of words containing '::' in it.

sycamorex 08-16-2012 06:31 PM

Try:
Code:

awk '$1 ~/::/ {print $1}' file

sycamorex 08-16-2012 06:40 PM

My solution above may work or may not. It's not exactly what I think you're after.

Is that what you want?
Code:

Data::Compare
Data:umper::Concise
Hash::Merge
Path::Class
Sort::Topological
String::Escape
String::Escape
Data::Compare


rohit.dhaval1 08-16-2012 06:45 PM

Many thanks.. That is exactly what I wanted. :)

sycamorex 08-16-2012 06:46 PM

What about that:

Code:

awk '{for (i=1;i<=NF;i++) if ($i ~/[a-z]::/) print $i}' file

sycamorex 08-16-2012 06:49 PM

Quote:

Originally Posted by rohit.dhaval1 (Post 4756084)
Many thanks.. That is exactly what I wanted. :)

Please note that the solution from post #4 will NOT give you the output from post #5
Try the solution from post #7

grail 08-16-2012 09:31 PM

How about a little gem (pun):
Code:

ruby -ne 'puts $_.scan(/(\w+(::\w+)+)/)[0][0]' file

rohit.dhaval1 08-21-2012 09:00 PM

thank you all

David the H. 08-22-2012 10:08 AM

Code:

$ grep -Eo '\b[^ ]+::[^ ]+\b' inputfile.txt
Data::Compare
Data:umper::Concise
Hash::Merge
Path::Class
Sort::Topological
String::Escape
String::Escape
Data::Compare

And please use ***[code][/code] tags*** around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, bolding, colors, or other fancy formatting.

If you go into the advanced editing box, there's also an option allowing you to turn off the smiley faces.


All times are GMT -5. The time now is 12:44 AM.