Grep in specific columns


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep in specific columns
# 1  
Old 12-03-2014
Grep in specific columns

I am trying to search a list of strings from a file and display the string as well as the column in the search file it was found. I dont care about the row. what is wrong with my script?

Code:
while read line; do awk  -v var="$line" '{for(i=1;i<NF;i++) if ($NF==$var) break; print $var FS $NF' }' searchfile done < listfile

Example
Code:
listfile
23
34
45
86

searchfile
1 23 56 777
1 455 34 677
45 566 67 


output 

23 matches 2 col in some row
34 matches 3 col in some row
45 matches 1 col in some row
86 not found

# 2  
Old 12-03-2014
Code:
awk 'FNR==NR{sear[$0];next} {for(i=1;i<=NF;i++) if ($i in sear) printf("Found %s in column %d [row %d]\n", $i, i, FNR)}' listfile searchfile

This User Gave Thanks to vgersh99 For This Post:
# 3  
Old 12-03-2014
There's syntax errors and logical errors. Having corrected the syntax, it looks like
Code:
while read line; do awk  -v var="$line" '{for(i=1;i<NF;i++) if ($NF==var) break; print var FS $NF }' searchfile; done < listfile

The result shows the logical errors, e.g. $NF is always the last field in the respective line
Code:
23 777
23 677
23 67
34 777
34 677
34 67
45 777
45 677
45 67
86 777
86 677
86 67

Sth. like
Code:
{for(i=1;i<=NF;i++) if ($i==var) print var FS i }
23 2
34 3
45 1

would come closer; still the "not found" missing.

However, as an awk- only solution, try
Code:
awk     'FNR==NR        {T[$1];next}
                        {for (i in T)
                                for (j=1; j<=NF; j++)
                                        if (i == $j)    {print i " matches " j " col"
                                                         delete T[i]
                                                        }
                        }
         END            {for (i in T) print i " not found"}
        ' file1 file2
23 matches 2 col
34 matches 3 col
45 matches 1 col
86 not found

This User Gave Thanks to RudiC For This Post:
# 4  
Old 12-03-2014
You say "tell me the column" but your example is "tell me the count of matching columns".

You might do it all with bash "while read -a" to capture all fields in an array you can then interrogate for length and compare cells in a loop, preparing whatever sort of output you want.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge specific columns of two files

Hello, I have two tab delimited text files. Both files have the same number of rows but not necessarily the same number of columns. The column headers look like, File 1: f0order CVorder Name f0 RI_9 E99 E199 E299 E399 E499 E599 E699 E799 E899 E999 File 2:... (9 Replies)
Discussion started by: LMHmedchem
9 Replies

2. UNIX for Dummies Questions & Answers

Intersection by specific columns

Hi, I'd like to intersect two files by the 4th col of the first file and 6th col of the second file. This is the code I use: awk 'NR==FNR{A;next}$6 File1 File2 However, this is only outputting the second file lines. I'd like to have both lines in a single line separated by a tab. Thanks in... (25 Replies)
Discussion started by: a_bahreini
25 Replies

3. UNIX for Dummies Questions & Answers

Printing lines with specific strings at specific columns

Hi I have a file which is tab-delimited. Now, I'd like to print the lines which have "chr6" string in both first and second columns. Could anybody help? (3 Replies)
Discussion started by: a_bahreini
3 Replies

4. Shell Programming and Scripting

Can't figure out how to find specific characters in specific columns

I am trying to find a specific set of characters in a long file. I only want to find the characters in column 265 for 4 bytes. Is there a search for that? I tried cut but couldn't get it to work. Ex. I want to find '9999' in column 265 for 4 bytes. If it is in there, I want it to print... (12 Replies)
Discussion started by: Drenhead
12 Replies

5. Shell Programming and Scripting

Deleting specific columns

Hi group, Can you please tell how to delete specific columns from a file. I know something like awk -F, '{ print $1" "$2" "15 }' input.txt > output.txt will delete all other columns. But this is in a way to copy some particular columns. But is there any other way to select just some... (11 Replies)
Discussion started by: smitra
11 Replies

6. Homework & Coursework Questions

ls in specific columns

Hello, i need to get the ls output in 2 columns.1st column the directories and 2nd the files... Also each column must be sorted by time... For example if the >>ls command gives me this : /dir2 /dir /dir1 /dir3 file1 file2 I need to take this : /dir file1 /dir1 ... (15 Replies)
Discussion started by: giampoul
15 Replies

7. Shell Programming and Scripting

Replace specific columns in one file with columns in another file

HELLO! This is my first post here! By the way, I think it is great that people do this. My question: I have two files, one is a .dilm and one is a .txt. It is my understanding that the .dilm file can be treated as a .txt file. I wrote another program where I was able to manipulate it as if it... (3 Replies)
Discussion started by: mehdib
3 Replies

8. Shell Programming and Scripting

Mean of the specific columns

I have a input file that has some common values in 1st,2nd and 3rd columns. 4th and 5th are different. Now I would like to print the mean of the fourth column of similar values in 1st.2nd and 3rd columns along with all the values in 5th column. input NM_0 1.22 CR5 0.4 n_21663... (10 Replies)
Discussion started by: repinementer
10 Replies

9. Shell Programming and Scripting

Grep based on specific columns.

Hi, How can I grep a record for a value based on specific column. If I simply do a grep 'AB' FilenName.txt, I might end up getting the records returned whose part of value is 'AB'. But I want it specific to second column. cut -d'|' -f 2 FileName.txt | grep 'AB' But now it will return... (1 Reply)
Discussion started by: deepakwins
1 Replies

10. Shell Programming and Scripting

Replace specific columns

hi All, Thi sis very urgent. I have large files with pipe delimited. For example: 1.txt 1001024|120|9|-0.0|#| 1001025|120|9|#| 1001026|120|9|#| 1001032|120|2|-0.0|#| 1002026|110|9|#| 1002027|110|9|-0.0|#| 1002028|120|1|1.0|#| I need to replace the 4th filed if it is # by |-| my... (2 Replies)
Discussion started by: jisha
2 Replies
Login or Register to Ask a Question