selecting record by matching in two columns values

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting selecting record by matching in two columns values
# 1  
Old 09-18-2011
Java selecting record by matching in two columns values

Hi Guys !

i want to search a record in file by matching two values in a record in two different columns

suppose i have 3 columns

and i want to select all those values from col1 for which in col3 has a specific value

e.g select all "john" from column1 where column 3 has a value of "20"

using grep and cut command


# 2  
Old 09-18-2011
Or awk?
awk '$1=="john" && $3==20'

# 3  
Old 09-18-2011
Hi Scrutinizer !

thanks for your help but is it not possible without awk? as i mentioned with grep and cut commands?


# 4  
Old 09-18-2011
Hi, you could make a concoction with cut and grep but I don't think that would be very efficient. You can also do something with grep alone, but that would not be as accurate, e.g.:
grep 'john .* 20'

(if the field separator is a space)
Is there a specific reason why you can't use awk?
# 5  
Old 09-18-2011

From the original post, I understand that he wants only the first field, although I'm not sure, so could be wrong. According with Scrutinizer, awk is one of the best tools for this job.

Here my approach:
$ cut -f1,3 infile | grep -E "john\s+20" | cut -f1

# 6  
Old 09-18-2011

Hi Scrutinizer !

i found the solution just wanted to share with you i used the back tracking technique

i first selected all the value = 20 from column 3 and then applied the cut on the first column like that...

but can you help me with what 's/ *\(.*\) \(.*\)/\2 \1/' is doing extactly?

grep '" 20 ' filename | cut -f1 -d' '| sort | uniq -c | sort -nr | sed 's/ *\(.*\) \(.*\)/\2  \1/' | column -t



---------- Post updated at 11:20 AM ---------- Previous update was at 11:11 AM ----------

Hi Scrutinizer

actually i'm doing something with the values i'm getting i know awk is more flexible but in my project i have not used awk and i not good at awk so that's why from futher difficulties i don't use awk for the time being

anyways thanks for your help and time

and one last question can we use column number in the grep command? as we use it in cut -f1? just wondering what will be the structure

Last edited by radoulov; 09-18-2011 at 02:13 PM.. Reason: Code tags!
# 7  
Old 09-18-2011
Hi, sed 's/ *\(.*\) \(.*\)/\2 \1/' flips the last field and any previous fields around...
Grep does not know fields, so you cannot use something like f1 in cut or $1 in awk...
This User Gave Thanks to Scrutinizer 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

Help with shell script: selecting rows that have the same values in two columns

Hello, everyone I am beginner for shell programming. I want to print all lines that have the same values in first two columns data: a b 1 2 a a 3 4 b b 5 6 a b 4 6 what I expected is : a a 3 4 b b 5 6 but I searched for one hour in... (2 Replies)
Discussion started by: nengcheng
2 Replies

2. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

3. Shell Programming and Scripting

Selecting lines having same values for first two columns

Hello to all. This is first post. Kindly excuse me if I do not adhere to any rules and regulations of this forum. I have a file containing some rows with three columns each per row(separeted by a space). There are certain rows for which first two columns have same value but the value in... (6 Replies)
Discussion started by: manojmalhotra13
6 Replies

4. Shell Programming and Scripting

Print columns matching to specific values

Hello Friends, I have a CDR file and i need to print out 2 columns with their field position which matches to some constant values, a part of input file CZ=1|CZA=1|DIAL=415483420001|EE=13|ESF=1|ET=|FF=0|9|MNC=99|MNP=9041|MTC=0|NID=2|NOA=international|ON=1| OutPut ... (3 Replies)
Discussion started by: EAGL€
3 Replies

5. Shell Programming and Scripting

Selecting lowest and highest values in columns 1 and 2, based on subsets in column 3

Hi, I have a file with the following columns: 361459 447394 CHL1 290282 290282 CHL1 361459 447394 CHL1 361459 447394 CHL1 178352861 178363529 AGA 178352861 178363529 AGA 178363657 178363657 AGA Essentially, using CHL1 as an example. For any line that has CHL1 in... (2 Replies)
Discussion started by: hubleo
2 Replies

6. Shell Programming and Scripting

[Solved] Need Help on Selecting a particular record

Hi Guys, Following is my Input file. Time : Wed Nov 16 02:32:09 2011 Type : INFO User : dsadm Message : Oracle_Connector_2: The connector generated the following TRUNCATE TABLE statement at runtime: TRUNCATE TABLE CANADA_INDUSTRY_OBJECTIVES. Time : Wed Nov 16... (4 Replies)
Discussion started by: mac4rfree
4 Replies

7. Shell Programming and Scripting

Copy values from columns matching in those in second file.

Hi All, I have two sets of files. Set 1: 100 text files with extension .txt with names like 1.txt, 2.txt, 3.txt until 100.txt Set 2: One big file with extension .dat The text files have some records in columns like this: 0.7316431 82628 0.7248189 82577 0.7248182 81369 0.7222999... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

8. Programming

Selecting array values

I have two arrays DIST(1:NCOF) and X(1:NX) Let NCOF = 5 and NX = 15, with DIST = and X = I want to create an array that puts a zero if DIST is outside the region in X, otherwise putting 1. In this example I should get RES = Using DIST = would give RES = The values in... (6 Replies)
Discussion started by: kristinu
6 Replies

9. Programming

selecting values of date

In a table, date is stored in a column as "2011-01-4". If I write query to get the dates > "2011-01-06" , then the date "2011-01-4" is also listed. The date stored in the column is a varchar datatype. So how can I make a query to not display the date "2011-01-4" ? Is there any solution ? Thank... (4 Replies)
Discussion started by: gameboy87
4 Replies

10. Shell Programming and Scripting

Selecting rows based on values in columns

Hi My pipe delimited .txt file contains rows with 10 columns. Can anyone advise how I output to file only those rows with the letters ‘ci' as the first 2 characters in the 3rd column ? Many thanks (4 Replies)
Discussion started by: malts18
4 Replies
Login or Register to Ask a Question