The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how can awk match multi pattern in a string sleepy_11 UNIX for Advanced & Expert Users 5 03-16-2008 11:40 PM
Compare string to a pattern jerryte Shell Programming and Scripting 2 01-25-2008 08:37 PM
Advance string pattern search Please sainj Shell Programming and Scripting 2 10-30-2007 10:56 AM
how to get the last part of a string followed by a pattern bluemoon1 Shell Programming and Scripting 4 09-08-2007 11:10 AM
locating a string pattern in a tree amit4g Shell Programming and Scripting 2 05-24-2007 07:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 08-08-2007
Deanne Deanne is offline
Registered User
  
 

Join Date: May 2007
Posts: 31
Question how to know if a string contains a certain pattern

say i want to know if string1 contains string2.
thanks a lot!
  #2 (permalink)  
Old 08-08-2007
firdousamir firdousamir is offline
Registered User
  
 

Join Date: Dec 2006
Posts: 25
echo $string1 | grep -c $string2
  #3 (permalink)  
Old 08-08-2007
Deanne Deanne is offline
Registered User
  
 

Join Date: May 2007
Posts: 31
thank you!
  #4 (permalink)  
Old 08-08-2007
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Code:
a=bigfish
b=fish
Code:
echo $b  | awk -v var=$a '{ if ( match(var, $0) && length < length(var) ) { print "yes" } else { print "no" } }'
  #5 (permalink)  
Old 08-08-2007
Klashxx's Avatar
Klashxx Klashxx is offline Forum Advisor  
HP-UX/Linux/Oracle
  
 

Join Date: Feb 2006
Location: Almerķa, Spain
Posts: 393
Awk without parsing:
Code:
string1="holacaracola"                                              
string2="hola"                                                      
echo $string1|awk '{if (match($0,/'"${string2}"'/)){print "MATCH"}}'
MATCH
  #6 (permalink)  
Old 08-08-2007
Deanne Deanne is offline
Registered User
  
 

Join Date: May 2007
Posts: 31
Matrixmadhan, Klashxx, I am very bad at awk and sed. And, im not sure if these will work with csh? I tried them both and got:
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
  #7 (permalink)  
Old 08-08-2007
reborg's Avatar
reborg reborg is online now Forum Staff  
Administrator
  
 

Join Date: Mar 2005
Location: Ireland
Posts: 4,209
In csh it would be ( for example )
Code:
set a=bigfish
set b=fish
echo $b  | awk -v var=$a '{ if ( match(var, $0) && length < length(var) ) { print "yes" } else { print "no" } }'
however this would be simpler:

Code:
set a=bigfish
set b=fish

echo $a | awk -v b="$b" '$0 ~ b { print "MATCH" }'
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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




All times are GMT -4. The time now is 07:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0