Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Search Forums:



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 11-23-2009
Registered User
 

Join Date: Nov 2007
Posts: 153
Thanks: 29
Thanked 0 Times in 0 Posts
Occurrence of two strings in a line of a file

Hi all,
is there a way to find the occurrence of two strings in a line of a file?

e.g. I have

Code:
    XXXX     yyyyy    zzzz 111
    XXXX     yyyyy    zzzz 222
    XXXX     yyyyy    zzzz 333
    XXXX     yyyyy    zzzz 444

and want to find in one shot

Code:
    XXXX     yyyyy    zzzz 222

Thank you,
Sarah

Last edited by Yogesh Sawant; 02-26-2010 at 12:25 PM.. Reason: added code tags
Sponsored Links
    #2  
Old 11-23-2009
dennis.jacob's Avatar
Registered User
 

Join Date: Feb 2007
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 10 Times in 9 Posts
Try:

Code:
awk '/XXXX/ && /222/' < filename

Sponsored Links
    #3  
Old 11-23-2009
Registered User
 

Join Date: Nov 2007
Posts: 153
Thanks: 29
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by dennis.jacob View Post
Try:

Code:
awk '/XXXX/ && /222/' < filename

can this be made case insensitive (on both strings)? maybe with grep?
    #4  
Old 11-23-2009
dennis.jacob's Avatar
Registered User
 

Join Date: Feb 2007
Location: Singapore/Cochin
Posts: 871
Thanks: 0
Thanked 10 Times in 9 Posts
Quote:
Originally Posted by f_o_555 View Post
can this be made case insensitive (on both strings)? maybe with grep?

Try:


Code:
awk 'toupper($0) ~ /XXXX/ && /222/'  < file

Sponsored Links
    #5  
Old 11-23-2009
Registered User
 

Join Date: Nov 2007
Posts: 153
Thanks: 29
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by dennis.jacob View Post
Try:


Code:
awk 'toupper($0) ~ /XXXX/ && /222/'  < file

...mmhhh not working... it seems I get no match

could I have, instead of XXXX and 222, two variable in array?
I tried something like

OCCURRENCE=`awk '/blah/ && /${STRING_NAME[1]}/' tmp.tmp1| wc -l`

but it does not work (it does if explicitly use the STRING_NAME value)
Thank you,

Last edited by f_o_555; 11-23-2009 at 10:15 AM..
Sponsored Links
    #6  
Old 11-23-2009
Registered User
 

Join Date: Aug 2009
Posts: 132
Thanks: 2
Thanked 10 Times in 10 Posts

Code:
grep -Ei ^X{4}.*2{3}$ your_file

Sponsored Links
    #7  
Old 11-24-2009
Registered User
 

Join Date: Nov 2007
Posts: 153
Thanks: 29
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by daPeach View Post
Code:
grep -Ei ^X{4}.*2{3}$ your_file

How can I put in two arbitrary strings $STRING1 and $STRING2?

grep -Ei $STRING1.$STRING2 doesn't work
Sponsored Links
Closed Thread

Tags
awk, grep

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Line number of an occurrence using grep f_o_555 UNIX for Dummies Questions & Answers 5 11-24-2009 05:37 AM
Concatenate strings line by line hagdanan Shell Programming and Scripting 6 12-12-2008 03:42 AM
Insert two strings at the beginning and at the end of each line of a file Linux-fueled Shell Programming and Scripting 3 10-14-2008 12:26 PM
Third line occurrence replacement sirokket16 Shell Programming and Scripting 5 07-03-2008 12:04 PM
Finding nth occurrence in line and replacing it stresing UNIX for Dummies Questions & Answers 5 02-01-2008 03:56 AM



All times are GMT -4. The time now is 03:17 AM.