AWK Command to find text in specific column


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AWK Command to find text in specific column
# 1  
Old 04-04-2010
AWK Command to find text in specific column

I'm new to scripting and would appreciate any help.

I have a list of over 20 words in File1 that I need to find in columns 10-15 of File2. I need the entire row of File2 that the File1 list matches.

I originally used a grep command which works, but provides File1 results that can be found anywhere in a line. Again, I need only results that fall in column 10-15 but need the entire row in the final results. I think this is an awk solve, but I am not sure. I don't think grep can search specific columns, only lines, and there is nothing that I have found to support it.

Small scaled example: grep -fFile1 File2 > File3

#File1
25544
31140
22253
12460

#File2
1 25544 65406456 65406540 65406540
1 16487 65406456 25544540 65406540
1 31140 65406456 65406540 65406540
1 22253 65406456 65406540 65406540
1 35111 31140456 65406540 65406540
1 12460 65406456 65406540 65406540
1 98798 65406456 165140540 65406540

#FILE3 <my results>
1 25544 65406456 65406540 65406540
1 16487 65406456 25544540 65406540 ---Don't want this line but get a return because 25544 is in the line but not desired column
1 31140 65406456 65406540 65406540
1 22253 65406456 65406540 65406540
1 35111 31140456 65406540 65406540 ---Don't want this line but get a return because 31140 is in the line but not desired column
1 12460 65406456 65406540 65406540


Any help is greatly appreciated.

Last edited by Chillin; 04-04-2010 at 02:06 AM..
# 2  
Old 04-04-2010
Based on your example, this will do what you want but i didn't understand what you mean by column 10-15

Code:
awk 'NR==FNR{arr[$1];next}$2 in arr{print}' file1 file2

# 3  
Old 04-04-2010
Sorry if I am not asking the question the right way or I confuse you. Thank you for the help, I will try this when I go in to work next.
By column 10-15 I mean that I only need file1 words that are found between columns 10-15 on any given line.
Column
12345678901234567890
_________025544
So 25544 must be here (between 10-15) and nowhere else. Unfortately if the number shows up in column 32-37 it will return a result and I don't want that.
These are satellite positions in two line element sets (TLE) which consists of all numbers, so essentially it is very possible to get File1 words anywhere in a File2.
I can't check this until Monday, will this solution still work?
# 4  
Old 04-04-2010
Hi
Have you testred Malcomex's code? Here i tested it for you, and it looked good to me,result:

Code:
nawk 'NR==FNR{arr[$1];next}$2 in arr{print}' f1.txt  f2.txt 
1 25544 65406456 65406540 65406540
1 31140 65406456 65406540 65406540
1 22253 65406456 65406540 65406540
1 12460 65406456 65406540 65406540

 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using awk to change a specific column and in a specific row

I am trying to change the number in bold to 2400 01,000300032,193631306,190619,0640,1,80,,2/ 02,193631306,000300032,1,190618,0640,CAD,2/ I'm not sure if sed or awk is the answer. I was going to use sed and do a character count up to that point, but that column directly before 0640 might... (8 Replies)
Discussion started by: juggernautjoee
8 Replies

2. Shell Programming and Scripting

Need a Linux command for find/replace column based on specific criteria.

I'm new to shell programming, I have a huge text file in the following format, where columns are separated by single space: ACA MEX 4O_ $98.00 $127.40 $166.60 0:00 0:00 0 ; ACA YUL TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ; ACA YYZ TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ; ADZ YUL TS_ $300.00... (3 Replies)
Discussion started by: transat
3 Replies

3. Shell Programming and Scripting

awk command to find total number of Special character in a column

How to find total number of special character in a column? I am using awk -f "," '$col_number "*$" {print $col_number}' file.csv|wc -l but its not giving correct output. It's giving output as 1 even though i give no special character? Please use code tags next time for your code and... (4 Replies)
Discussion started by: AjitKumar
4 Replies

4. Shell Programming and Scripting

awk or sed to find specific column from different files

Hi everybody, I have a folder with many files: Files with 8 columns: X 123 A B C D E F And files with 7 columns: X1234 A B C D E F I am trying to find a way to extract the 5th column when the files have eight columns, or the 4th column when the files have... (3 Replies)
Discussion started by: Tzole
3 Replies

5. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

Hi, I have a text file in the following format: Code: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 ... (2 Replies)
Discussion started by: evelibertine
2 Replies

6. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

I have a text file in the following format: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 13402 NA07019... (3 Replies)
Discussion started by: evelibertine
3 Replies

7. Shell Programming and Scripting

Assigning a specific format to a specific column in a text file using awk and printf

Hi, I have the following text file: 8 T1mapping_flip02 ok 128 108 30 1 665000-000008-000001.dcm 9 T1mapping_flip05 ok 128 108 30 1 665000-000009-000001.dcm 10 T1mapping_flip10 ok 128 108 30 1 665000-000010-000001.dcm 11 T1mapping_flip15 ok 128 108 30... (2 Replies)
Discussion started by: goodbenito
2 Replies

8. Shell Programming and Scripting

Need an awk for a global find/replace in a file, specific column

I am new to unix and awk/sed etc... using C-Shell. Basically, I have a fixed length file that has 4 different record types on it, H, D, V, W all in column 1. I need to change all the W's in column 1 to D's. in the entire file. The W's can be anywhere in the file and must remain in the same... (3 Replies)
Discussion started by: jclanc8
3 Replies

9. Shell Programming and Scripting

Insert a text from a specific row into a specific column using SED or AWK

Hi, I am having trouble converting a text file. I have been working for this whole day now, still i couldn't make it. Here is how the text file looks: _______________________________________________________ DEVICE STATUS INFORMATION FOR LOCATION 1: OPER STATES: Disabled E:Enabled ... (5 Replies)
Discussion started by: Issemael
5 Replies
Login or Register to Ask a Question