matching group of words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting matching group of words
# 8  
Old 09-24-2011
Thank you so much Shamrock. Your idea is really simple and easy to understand. And the usage of function gsub is also great, I was not aware of it before.

But, there is a small problem in the code. The output I got is :
Code:
 
gp1 : None
gp2 : None
gp3 : grp1 grp4

and I couldn't figure out why? Please help!
# 9  
Old 09-24-2011
What platform are you on...
# 10  
Old 09-25-2011
I am on Debian GNU/Linux 5.0.8 (lenny). What platform are you using?

---------- Post updated at 03:53 PM ---------- Previous update was at 07:52 AM ----------

I observed a strange change as when I moved the first line to the end,
Code:
 
<File1> 
gp2 : syt2 kgk iti op2 
gp3 : ppy yt5 itt sky utw yry
gp1 : xyz xys3 syt2 ssx itt kty

it worked perfect and gave the required output. But, I didn't understand Why?
Any clues?
# 11  
Old 09-25-2011
Quote:
Originally Posted by mira
I am on Debian GNU/Linux 5.0.8 (lenny). What platform are you using?
It gives correct output on aix/hpux but when i ran it on redhat it didnt which is strange...i will dig into it and see why its happening...perhaps a bug in gnu's awk.

---------- Post updated at 11:31 AM ---------- Previous update was at 11:06 AM ----------

Quote:
Originally Posted by mira
I observed a strange change as when I moved the first line to the end,
Code:
 
<File1> 
gp2 : syt2 kgk iti op2 
gp3 : ppy yt5 itt sky utw yry
gp1 : xyz xys3 syt2 ssx itt kty

it worked perfect and gave the required output. But, I didn't understand Why?
Any clues?
It seems related to the assignment of the field separator FS as it gives correct output on rhel if FS is assigned outside the main block...
Code:
awk -F" : " '{
   if (FILENAME == "file1") x[$1] = $2
   if (FILENAME == "file2") y[$1] = $2
} END {
   for (i in x) {
      for (j in y) {
         n = split(y[j], a, " ")
         for (p = 1; p <= n; p++)
            s += gsub(a[p], a[p], x[i])
         if (n == s) z[i] = z[i] ? z[i]" "j : i FS j
         s = 0
      }
      if (!z[i]) z[i] = i" : None"
   }
   for (i in z) print z[i]
}' file1 file2

So does making this change work on your debian box without changing the order of lines in file1...
These 2 Users Gave Thanks to shamrock For This Post:
# 12  
Old 09-25-2011
Yes, its working perfect now Smilie Thanks a lot!
Awk programming is great for file processing and simpler than other languages but the versions and flavours cause trouble sometimes and it becomes hard to find out why? but Thanks again for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print ALL matching words in a string

Hi. str=" {aaID=z_701; time=2012-10-08 00:00:00.000}; {aaID=S_300; time=2012-10-08 00:00:00.000}]}; ansokningsunderlag={anmaln......} {aaID=x_500; time=2012-10-08 00:00:00.000}]}; ansokningsunderlag={anmaln......}" I want to print: z_701 S_300 x_500 if I use : echo $str | sed -n... (4 Replies)
Discussion started by: freddan25
4 Replies

2. Shell Programming and Scripting

regular expression matching whole words

Hi Consider the file this is a good line when running grep '\b(good|great|excellent)\b' file5 I expect it to match the line but it doesn't... what am i doing wrong?? (ultimately this regex will be in a awk script- just using grep to test it) Thanks, Storms (5 Replies)
Discussion started by: Storms
5 Replies

3. Shell Programming and Scripting

Get group of consecutive uppercase words using gawk

Hi I'd like to extract, from a text file, the strings starting with "The Thing" and only composed of words with a capital first letter and apostrophes, like for example: "The Thing I Only" from "those are the The Thing I Only go for whatever." or "The Thing That Are Like Men's Eyewear" ... (7 Replies)
Discussion started by: louisJ
7 Replies

4. Shell Programming and Scripting

How to move a group of words before another group of words

Hi I have a file containing lines with several consecutive words starting with a capital letter (i.e. Zuvaia Flex), followed by "de The New Foul", and I would like to put "The New Foul" before the group with capital letters and delete "de" From the line: Le short femme Zuvaia Flex de The... (2 Replies)
Discussion started by: louisJ
2 Replies

5. Shell Programming and Scripting

Adding numbers matching with words

Hi All, I have a file which looks like this: abc 1 abc 2 abc 3 abc 4 abc 5 bcd 1 bcd 3 bcd 3 bcd 5 cde 7 This file is just a miniature version of what I really have. Original file is some 1 million lines long. I have tried to come up with the code for what I wish to accomplish... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

6. Shell Programming and Scripting

Print only matching words

Hi All, I have searched the forum and tried to print only matching(pattern) words from the file, but its printing entire line. I tried with grep -w. I am on sunsolaris. Eg: cat file A|A|F1|F2|A|F3|A A|F10|F11|F14|A| F20|A|F21|A|F25 I have to search for F (F followed by numbers) and ... (5 Replies)
Discussion started by: gsjdrr
5 Replies

7. Shell Programming and Scripting

Matching words in Perl

Hi, I have an array in which one column can contain any statement. From multiple rows of that column I want to match the statement like "Execution Started." If that row contains "Execution started." then only I have to fetch other data of other columns of that particular row. I dont want... (2 Replies)
Discussion started by: monika
2 Replies

8. Shell Programming and Scripting

How to from grep command from a file which contains matching words?

Hi all I have a file with below content (content is variable whenever new product is launched). I need form a grep command like this egrep "Unknown product|Invalid symboland so on" How to do it using a script? Unknown product Invalid symbol No ILX exch found exceeds maximum size AFX... (4 Replies)
Discussion started by: johnl
4 Replies

9. UNIX for Advanced & Expert Users

matching words using regular expressions

following file is taken as input aaa bbb ccc ddd eee ffff grep -w aaa <filename> gives proper output. grep \<\(aaa\).*\> filename :- should give output, since aaa is at begining, however i dosen't get any ouput. Any discrepancy. machine details:- Linux anaconda... (1 Reply)
Discussion started by: bishweshwar
1 Replies

10. Programming

getting file words as pattern matching

Sir, I want to check for the repation of a user address in a file i used || as my delimiter and want to check repetaip0n of the address that is mailid and then i have to use IMAP and all. How can i do this... I am in linux ...and my file is linux file. ... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies
Login or Register to Ask a Question