can awk built-in "match" be exact??


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users can awk built-in "match" be exact??
# 1  
Old 01-14-2009
can awk built-in "match" be exact??

hello everybody,

as explained in the title, here is what I want:

str1="name1 name2 name3"
str2="name1"
str3="name"

I know that match(str1,str2) will return 1, but I want that match(str1,str3) returns 0 (when it also returns 1...)

Is there a way to get that exact matching process done in a awk script??

cheers,
Tanguy
# 2  
Old 01-14-2009
What do you mean by "exact"? str2 and str3 are both found in str1, but neither is an exact match.
# 3  
Old 01-14-2009
good point otheus...

well by that I mean that I'd like to compare each "field" of str1 (all fields being separated by a coma) and test if they match str3 (exactly). So basically I want to compare name1 to name, then name2 to name, then name3 to name... and check if they are exactly the same (in which case name1 compared to name is a false)

hum... not so clear, is it?
But maybe I'm simply not doing things the easier way...
# 4  
Old 01-14-2009
It's clear now. Can you use "nawk" or GNU awk (gawk)?
# 5  
Old 01-14-2009
yes I can use gawk if it makes things easier
# 6  
Old 01-14-2009
First, use the split() function to split up the "str1" into an array. Then, search the array using "==". So:
Code:
split(str1,ary,",");
for (s in ary) 
  if (s == str2 || s == str3) { found=s; break; }
print found

# 7  
Old 01-14-2009
excellent!

I had already tried that "split" thing in a couple of less elegant ways and with no success, so many thanks for that ;-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search file containing ps results for a match "my.cnf" and then for a second match . "ok:" and

I need to find two matches in the output from ps. I am searching with ps -ef |grep mysql for: my.cnf /bin/sh /usr/bin/mysqld_safe --defaults-file=/data/mysql/master/agis_core/etc/my.cnf after this match I want to search back and match the hostname which is x number of lines back, above the... (2 Replies)
Discussion started by: bash_in_my_head
2 Replies

2. UNIX for Advanced & Expert Users

AIX - io info get from "libperfstat" not match "iostat"

Hi, everyone. I need to write a program to get io info based on libperfstat. But the "write time" of a disk is just half of the value get from iostat. I'm confused and can't explain. Help please. How I calculate "write service time per sec": In iostat: write service... (0 Replies)
Discussion started by: jackliang
0 Replies

3. UNIX for Dummies Questions & Answers

Explanation on problem "match" function awk

Hello Unix experts, If I could get any explanations on why the code below doesn't work it would be great ! My input looks like that ("|" delimited): Saaaaabbbbbccccc|ok Sdddddfffffggggg|ok The goal is, if $2 is "ok", to remove everything before the pattern given in the match function... (5 Replies)
Discussion started by: lucasvs
5 Replies

4. Shell Programming and Scripting

"AND" string match using awk

Hiya, Using awk (as I already have an embedded calculation in the command) reading in a file of many thousands of lines I would like to extract only the lines with M values where field one is less than 62: Part example of my input file is: ... 89,63,AAY0772,M 38,66,AAY0772,f... (3 Replies)
Discussion started by: gafoleyo73
3 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

grep regex, match exact string which includes "/" anywhere on line.

I have a file that contains the 2 following lines (from /proc/mounts) /dev/sdc1 /mnt/backup2 xfs rw,relatime,attr2,noquota 0 0 /dev/sdb1 /mnt/backup xfs rw,relatime,attr2,noquota 0 0 I need to match the string in the second column exactly so that only one result is returned, e.g. > grep... (2 Replies)
Discussion started by: jelloir
2 Replies

7. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

8. Shell Programming and Scripting

awk statement to match all lines starting with "#"

Looking for awk statement that will match all lines starting with "# " if ( $1 == \^"#" ) Input file: # of the server. If you would like to set these, please take out the # pound (#) sign in front of one or all severities and set it equal to # severity desired. For example, FATAL=3 #... (2 Replies)
Discussion started by: Arsenalman
2 Replies

9. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

10. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question