how to know if a string contains a certain pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to know if a string contains a certain pattern
# 8  
Old 08-08-2007
Quote:
Originally Posted by reborg
In csh it would be ( for example )

echo $b | awk -v var=$a '{ if ( match(var, $0) && length < length(var) ) { print "yes" } else { print "no" } }'



echo $a | awk -v b="$b" '$0 ~ b { print "MATCH" }'
Reborg, neither of them works... I still get the same errors:
awk: syntax error near line 1
awk: bailing out near line 1
# 9  
Old 08-08-2007
Try nawk instead of awk.
# 10  
Old 08-08-2007
The following worked!

echo $b | nawk -v var=$a '{ if ( match(var, $0) && length < length(var) ) { print "yes" } else { print "no" } }'

But this one didnt:

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

Error message was:

nawk: syntax error at source line 1
context is
$0 ~ b { print "MATCH >>> } <<<
nawk: illegal statement at source line 1
missing }

Really, I have to start studying awk (and nawk)...
# 11  
Old 08-09-2007
You are missing a quote on the second one.

"MATCH should be "MATCH"
# 12  
Old 08-09-2007
Yes, I missed the ".
Thanks guys!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PHP - Regex for matching string containing pattern but without pattern itself

The sample file: dept1: user1,user2,user3 dept2: user4,user5,user6 dept3: user7,user8,user9 I want to match by '/^dept2.*/' but don't want to have substring 'dept2:' in output. How to compose such regex? (8 Replies)
Discussion started by: urello
8 Replies

2. Shell Programming and Scripting

Replace a string pattern

Hi, I have a CSV with following type of data and would like to replace the timestamp information with 'null' string. Can you please suggest me on same? 8,1,'1','1',11,'2013-08-12 18:34:17.0','null',1,'2013-08-12 18:34:17.0','null','PROMOTIONAL','12','1','11','11',11,'0' Thanks for your... (10 Replies)
Discussion started by: bhupinder08
10 Replies

3. Shell Programming and Scripting

Search for a pattern in a String file and count the occurance of each pattern

I am trying to search a file for a patterns ERR- in a file and return a count for each of the error reported Input file is a free flowing file without any format example of output ERR-00001=5 .... ERR-01010=10 ..... ERR-99999=10 (4 Replies)
Discussion started by: swayam123
4 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

6. Shell Programming and Scripting

extract a string within a string using a pattern

hi all, i have a file name using the following pattern: PREFIX: AR SOURCE: LEGACY DATETIME: YYYYMMDD_HH24MISS SUFFIX: .txt sample filename: AR_LEGACY_20101104_105500.txt i want to extract the source which is LEGACY in this case. how do i do this using shell? thanks. (4 Replies)
Discussion started by: adshocker
4 Replies

7. Shell Programming and Scripting

Looking for pattern next to variable string

Hi, I need to help to find the way to get specific data from a log, an unknow string beside a string that is the same always, for example: Input file: ctmdefine -TASKTYPE COMMAND -TABLE PRUEBAS_BIMLAR -JOBNAME PRUEBAJOB12 \ -GROUP PRUEBAS_BIM2 -APPLICATION PRUEBAS_BIM2 -NODEGRP gtwtran... (4 Replies)
Discussion started by: mgcorona
4 Replies

8. UNIX for Dummies Questions & Answers

How to search for a pattern a string?

Hi all, I'm new in UNIX , so how to check if stringA is present within stringB ? Something similar to INSTR function in pl sql... Thanks a lot. (12 Replies)
Discussion started by: Leo_NN
12 Replies

9. Shell Programming and Scripting

Compare string to a pattern

I am new to unix and need to learn how to compare a variable $subject to a string pattern. If the variable has the word "Item" in it then it should be true. How do I do this? Currently I am using the Bourne shell but I can also use Korn or Bash. I come from a Rexx background where strings are... (2 Replies)
Discussion started by: jerryte
2 Replies

10. Shell Programming and Scripting

how to get the last part of a string followed by a pattern

assuming "cat" is the pattern, string (regardless length) asdadfcat4 I need to get 4 for eirtrjkkkcat678- I'd get 678 (in b-shell) Thanks in advance!!! (4 Replies)
Discussion started by: bluemoon1
4 Replies
Login or Register to Ask a Question