LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   searching letter patterns within words (https://www.linuxquestions.org/questions/linux-newbie-8/searching-letter-patterns-within-words-945250/)

udiubu 05-16-2012 04: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!

Sincerely,

Udiubu

kbp 05-16-2012 05:52 AM

Code:

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

udiubu 05-16-2012 06: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.

Best,

Emiliano

colucix 05-16-2012 06: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):
Code:

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:
Code:

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

kbp 05-16-2012 08:49 AM

Code:

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

chrism01 05-16-2012 07:23 PM

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

kbp 05-17-2012 01:59 AM

Yep ..


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