LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 05-16-2012, 04:31 AM   #1
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Rep: Reputation: Disabled
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
 
Old 05-16-2012, 05:52 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,716

Rep: Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634
Code:
cat myfile | grep -i ph
.. did you specifically want awk?
 
1 members found this post helpful.
Old 05-16-2012, 06:29 AM   #3
udiubu
Member
 
Registered: Oct 2011
Posts: 54

Original Poster
Rep: Reputation: Disabled
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
 
Old 05-16-2012, 06:41 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,361

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
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.
 
Old 05-16-2012, 08:49 AM   #5
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,716

Rep: Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634
Code:
cat myfile | grep -i ph | cut -d" " -f2
.. grabbing the second column
 
1 members found this post helpful.
Old 05-16-2012, 07:23 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
You do realise grep take as a filename as a param ie no 'cat' reqd ...
 
Old 05-17-2012, 01:59 AM   #7
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,716

Rep: Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634Reputation: 634
Yep ..
 
  


Reply

Tags
awk, pattern, string


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Choosing words based on letter count danielbmartin Programming 16 02-15-2012 03:07 PM
searching multiple patterns through Vi editor barunparichha Linux - Software 10 05-12-2009 08:39 PM
searching for redundant patterns adidassharma Programming 5 09-26-2008 12:35 AM
Select only words with a letter Coimbra Linux - Newbie 6 07-04-2007 08:45 AM
Searching patterns from file MichaelVaughn Programming 1 04-06-2004 11:18 AM


All times are GMT -5. The time now is 02:15 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration