-   Linux - Newbie (
-   -   searching letter patterns within words (

udiubu 05-16-2012 05:31 AM

searching letter patterns within words
Hi guys,

I have a long long list of nouns with frequency, so two columns:

alpha 45
beta 3
gamma 45

I need to find some particular patterns with the words, like for example "ph" in alpha, and copy the entire line (alpha 45).

When I want to look for the entire word, I generally use

awk '$1 == "alpha"{print $0}' infile.txt > outfile.txt

but how should I procede in this case?
Additionally, how to make the search case-insensitive?

I thank you very much for any possible suggestion!



kbp 05-16-2012 06:52 AM


cat myfile | grep -i ph
.. did you specifically want awk?

udiubu 05-16-2012 07:29 AM

Cheers! I actually realized that grep could solve it already.
Well, I wanted to use awk, because in a second step I wanted to copy the second column only.

Anyways, thanks a lot.



colucix 05-16-2012 07:41 AM

Using awk you can set the IGNORECASE built-in variable to a non-zero value and try something like this (using a regular expression instead of string comparison):

awk 'BEGIN{IGNORECASE=1}/ph/{print $2}' file
In alternative you can try character lists with both the upper- and lower-case letters, but it may result cumbersome sometimes:

awk '/[Pp][Hh]/{print $2}' file
Hope this helps.

kbp 05-16-2012 09:49 AM


cat myfile | grep -i ph | cut -d" " -f2
.. grabbing the second column :)

chrism01 05-16-2012 08:23 PM

You do realise grep take as a filename as a param ie no 'cat' reqd ...

kbp 05-17-2012 02:59 AM

Yep ..

All times are GMT -5. The time now is 06:41 AM.