awk script for contains search


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script for contains search
# 1  
Old 07-09-2014
awk script for contains search

I need help in awk script to do contains search and my requirement is below

I need to check if the value in each column is present in any other column and print it. And in some columns these value could be existing with comma as delimiter.

Sample data
Code:
ID|Description|Column1|Column2|Column3|Column4|
1|ABC DEF GHJ JJJ|ABC|DEF|GHJ|JJJ|
2|AHJ VGH SDHKJHK GKK|AHJ|VGH|SDHKJHK|GKK,AHJ,VGH|
3|123 HGJ KLL|123|HGJ|KLL||
4|345 BNN NMNMN SHDJ|345|BNN|NMNMN|SHDJ,345 BNN|

In the above, AHJ is present in Column 1 and Column 4 and similarly 345 & BNN in Column 1 & 4



Thanks

Last edited by Scrutinizer; 07-09-2014 at 06:52 AM.. Reason: sample data was not clear; CODE TAGS
# 2  
Old 07-09-2014
Provide the expected output
# 3  
Old 07-09-2014
With some assumption, try

Code:
$ awk -F'|' '{ for(i=1;i<=NF;i++){  for(j in A){ if($i ~ j){ print; delete A; next} }  p=$i; gsub(/[[:space:]]+/,"|",p); A[p] } delete A  }' file

# 4  
Old 07-09-2014
Output should have ID, column, repetitive value like
Code:
1|Column 1|ABC|
1|Column 4|ABC|
4|Column 1|345|
4|Column 4|345|
4|Column 1|BNN|
4|Column 4|BNN|


Last edited by Scrutinizer; 07-10-2014 at 05:19 AM.. Reason: CODE tags, formatting
# 5  
Old 07-09-2014
Why don't you print DEF nor GHJ nor JJJ in row 1, and similar in following rows? And, column 1 is the ID, so does not match any other value...
Please respecify carefully!
# 6  
Old 07-10-2014
Thanks RudiC.

Sorry, my earlier output was wrong. Here is the correct one.

Code:
ID|Column|Value|
2|Column 1|AHJ|
2|Column 4|AHJ|
4|Column 1|345|
4|Column 4|345|
4|Column 2|BNN|
4|Column 4|BNN|

And I want to find & print repetitive values (matched ones) from Column1 onwards. Do not want to include ID & Description in this check.

Last edited by Scrutinizer; 07-10-2014 at 05:19 AM.. Reason: CODE tags
# 7  
Old 07-10-2014
Code:
awk 'BEGIN{FS = OFS = "|"}
  NR == 1 {split($0, h);
    print "ID", "Column", "Value";
    next}
  {split($2, a, " |,");
  for(x in a)
    {f = 0; for(i=3; i<=NF; i++)
        {if($i ~ "(^|,| )" a[x] "(,| |$)" && f)
          {print $1, h[j], a[x];
          print $1, h[i], a[x];
          f = 0;
          continue}
        if($i ~ "(^|,| )" a[x] "(,| |$)")
          {f = 1; j = i}}}}' file

output:
Code:
ID|Column|Value
2|Column1|AHJ
2|Column4|AHJ
2|Column2|VGH
2|Column4|VGH
4|Column1|345
4|Column4|345
4|Column2|BNN
4|Column4|BNN

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep/awk using a begin search pattern and end search pattern

I have this fileA TEST FILE ABC this file contains ABC; TEST FILE DGHT this file contains DGHT; TEST FILE 123 this file contains ABC, this file contains DEF, this file contains XYZ, this file contains KLM ; I want to have a fileZ that has only (begin search pattern for will be... (2 Replies)
Discussion started by: vbabz
2 Replies

2. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

3. Shell Programming and Scripting

awk script to search output for a value and print

GOODNUMBERS="1 2 3 4 5 6 3 3 34 34 5 66 12" BADNUMBERS="7 3 12 5 66" for eachnum in `echo ${GOODNUMBERS}` do echo ${BADNUMBERS} | gawk -v threshold=${eachnum} '$1 != threshold' done what im trying to do with the above is, i want to print numbers that are in the GOODNUMBERS... (10 Replies)
Discussion started by: SkySmart
10 Replies

4. Shell Programming and Scripting

Search several string and convert into a single line for each search string using awk command AIX?.

I need to search the file using strings "Request Type" , " Request Method" , "Response Type" and by using result set find the xml tags and convert into a single line?. below are the scenarios. Cat test Nov 10, 2012 5:17:53 AM INFO: Request Type Line 1.... (5 Replies)
Discussion started by: laknar
5 Replies

5. Shell Programming and Scripting

Problem passing a search pattern to AWK inside a script loop

Learning, stumbling! My progress in shell scripting is slow. Now I have this doubt: I have the following file (users.txt): AU0909,on AU0309,off AU0209,on AU0109,off And this file (userson.txt) AU0909 AU0209 AU0109 AU0309 I just want to set those users on userson.txt to "off" in... (14 Replies)
Discussion started by: quinestor
14 Replies

6. Shell Programming and Scripting

awk search

i guys, i have a bash script , and it works, but i need an awk file, and i can't convert this code like: #!/bin/awk -f ..... my script #!/bin/bash awk '/\<FIRST\>|\<SECOND\>|\<THIRD\>|\<ZERO\>/' DOC.txt thanks :) (4 Replies)
Discussion started by: felito
4 Replies

7. Shell Programming and Scripting

Awk Script: Nested search using different patter

Hi Friends. Please have a look at dummy file. I need to extract from this file: 1. Counts of event= 2. the 2nd coulmn is unique call id of this transaction. Based on that, i have to search for txstatus= . Note: Values of event, calltype and txstatus can be anything. I want to print... (1 Reply)
Discussion started by: itsmesanju
1 Replies

8. Shell Programming and Scripting

Using a script variable in awk search patterns

Hi all, In a script like : job_date=.... ls -l 2>/dev/null | awk -v var =$job_date ' /Name\.Version\.+\.xml$/ { How can i include a script variable job_date store in "var" in the pattern "/Name\.Version\.+\.xml$/" Thanks in advance (12 Replies)
Discussion started by: abhinav192
12 Replies

9. Shell Programming and Scripting

reverse search in awk script

Hi, I am new to awk. Actually I want to search a pattern A, when I get that line with pattern A then for one of the field of that line again I want search on that field (say pattern B)from start of the file. I am using awk. Is nested searching possible in awk? Please do the needful as... (4 Replies)
Discussion started by: id4forum
4 Replies

10. Shell Programming and Scripting

awk script to search an html file and output links

hello. i want to make an awk script to search an html file and output all the links (e.g .html, .htm, .jpg, .doc, .pdf, etc..) inside it. also, i want the links that will be output to be split into 3 groups (separated by an empty line), the first group with links to other webpages (.html .htm etc),... (8 Replies)
Discussion started by: kyris
8 Replies
Login or Register to Ask a Question