Grep based on specific columns.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep based on specific columns.
# 1  
Old 03-26-2009
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.

Code:
cut -d'|' -f 2 FileName.txt | grep 'AB'

But now it will return only the 2nd column. I need the whole row.

Kindly help me.

Last edited by Yogesh Sawant; 03-27-2009 at 05:29 AM.. Reason: added code tags
# 2  
Old 03-26-2009
then try this Smilie
Code:
grep "^[^|]*|AB" infile

or
Code:
awk -F"|" '$2 ~ /AB/{print}' infile

This User Gave Thanks to vidyadhar85 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add new columns based on existing columns

Hi all, I am kind of stuck with printing my desired output. Please help me if you know how it can work. My input file(tab separated): NW_0068.1 41,16 100,900 NW_0699.1 4,2,19 200,700,80 My Output file (desired): NW_0068.1 41,16 100,900 100 - 141 NW_0068.1 41,16 100,900 ... (3 Replies)
Discussion started by: sam_2921
3 Replies

2. UNIX for Dummies Questions & Answers

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? while read line; do awk -v var="$line" '{for(i=1;i<NF;i++) if ($NF==$var) break; print $var FS $NF' }'... (3 Replies)
Discussion started by: senhia83
3 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

Creating subset of a file based on specific columns

Hello Unix experts, I need a help to create a subset file. I know with cut comand, its very easy to select many different columns, or threshold. But here I have a bit problem as in my data file is big. And I don't want to identify the column numbers or names manually. I am trying to find any... (7 Replies)
Discussion started by: smitra
7 Replies

5. 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

6. UNIX for Dummies Questions & Answers

Grep from specific column in one file based on another file [Please help!]

Hey Guys, to keep it simple, I have an idFile: 1006006 1006008 1006011 1007002 ...... and famFile: 1006 1006001 1006016 1006017 1 1006 1006006 1006016 1006017 1 1006 1006007 0 0 2 1006 1006008 1006007 1006006 2 1006 1006010 1006016 1006017 2 1006 1006011 1006016 1006017 1 1006... (2 Replies)
Discussion started by: Zoho
2 Replies

7. Shell Programming and Scripting

Extracting specific lines of data from a file and related lines of data based on a grep value range?

Hi, I have one file, say file 1, that has data like below where 19900107 is the date, 19900107 12 144 129 0.7380047 19900108 12 168 129 0.3149017 19900109 12 192 129 3.2766666E-02 ... (3 Replies)
Discussion started by: Wynner
3 Replies

8. Shell Programming and Scripting

grep and cat based on specific mod date

I'm trying to figure out how to open and copy all contents of files last modded on aug 14 to one single text file. Also, I'm trying to do this in one command string. I have ls -l -R | grep "Aug 1" but all this does is print the -l info with Aug 1 in it. how can I modify this so that ls... (3 Replies)
Discussion started by: mmixology
3 Replies

9. 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

10. UNIX for Advanced & Expert Users

Grep all the columns based on a particular column

This is the data file that I have 21879, 0, 22, 58, 388 0, -1, 300, 1219172589765, 1708, 0, 200, 21891, 0, 0, 33, 309 0, -1, 300, 1219172591478, 1768, 0, 200, 22505, 0, 0, 33, 339 0, -1, 300, 1219172593251, 1738, 0, 200, 21888, 0, 1, 33, 308 0, -1, 300, 1219172594995, 633, 0, 200, 24878,... (2 Replies)
Discussion started by: pmallur
2 Replies
Login or Register to Ask a Question