Remove lines with unique information in indicated columns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove lines with unique information in indicated columns
# 1  
Old 03-02-2014
Remove lines with unique information in indicated columns

Hi, I have the 3-column, tab-separated following data:

Code:
dot is-big 2 
dot is-round 3 
dot is-gray 4 
cat is-big 3 
hot in-summer 5

I want to remove all of those lines in which the values of Columns 1 and 2 are identical. In this way, the results would be as follows:

Code:
dot is-big 2 
cat is-big 3

Is there an awk / grep that code easily help me to solve this problem. My issue is isolating Cols. 1 and 2 and not considering the information in Col. 3 when trying to remove the unique lines.

Thanks!
# 2  
Old 03-02-2014
Can you please explain more...because, from the data you shown above, how do you get the result you provided for identical cols 1 & 2.
Anyways, if you want unique col 1 & 2
Code:
awk '! a[$1 $2]++' <inputfile>

This User Gave Thanks to SriniShoo For This Post:
# 3  
Old 03-02-2014
Thank you for your response.

Quote:
Originally Posted by SriniShoo
Can you please explain more...because, from the data you shown above, how do you get the result you provided for identical cols 1 & 2.
Anyways, if you want unique col 1 & 2
Code:
awk '! a[$1 $2]++' <inputfile>

I think maybe I did not describe well my problem. I don't want unique Cols1 and 2. I need to remove all unique Col 2. (regardless of what is in Col 1.), In the example I provided, you can see that the Col 2 that remains are duplicates while the Col 2 that were unique are discarded.

Last edited by owwow14; 03-02-2014 at 03:31 PM..
# 4  
Old 03-02-2014
Code:
awk '
NR==FNR {cnt[$2]++; next}
cnt[$2]>1
' infile infile

# 5  
Old 03-02-2014
Code:
awk '$2 in A{print A[$2] $0; A[$2]=x; next} {A[$2]=$0 ORS}' file


Last edited by Scrutinizer; 03-02-2014 at 05:00 PM..
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

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

2. Shell Programming and Scripting

Remove nullable columns in lines

Hi Every one, my requirement is to remove the null columns in line, comma delimiter used For example, A,11,20,30,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, B1,,,,,, gem,plum,kite,,,,gud,bad,,,,,,,,,,,,, B2,kiing,kong,height,,,,,,,,,,,,,,,,,,,,,,,,,rak,,,,,,,,,,,,, B1,,,,,,... (9 Replies)
Discussion started by: skpshell
9 Replies

3. UNIX for Dummies Questions & Answers

Print unique lines without sort or unique

I would like to print unique lines without sort or unique. Unfortunately the server I am working on does not have sort or unique. I have not been able to contact the administrator of the server to ask him to add it for several weeks. (7 Replies)
Discussion started by: cokedude
7 Replies

4. Shell Programming and Scripting

How to merge columns into lines, using unique keys?

I would really appreciate a sulution for this : invoice# client# 5929 231 4358 231 2185 231 6234 231 1166 464 1264 464 3432 464 1720 464 9747 464 1133 791 4930 791 5496 791 6291 791 8681 989 3023 989 (2 Replies)
Discussion started by: hemo21
2 Replies

5. UNIX for Dummies Questions & Answers

remove duplicate lines based on two columns and judging from a third one

hello all, I have an input file with four columns like this with a lot of lines and for example, line 1 and line 5 match because the first 4 characters match and the fourth column matches too. I want to keep the line that has the lowest number in the third column. So I discard line 5.... (5 Replies)
Discussion started by: TheTransporter
5 Replies

6. UNIX for Advanced & Expert Users

In a huge file, Delete duplicate lines leaving unique lines

Hi All, I have a very huge file (4GB) which has duplicate lines. I want to delete duplicate lines leaving unique lines. Sort, uniq, awk '!x++' are not working as its running out of buffer space. I dont know if this works : I want to read each line of the File in a For Loop, and want to... (16 Replies)
Discussion started by: krishnix
16 Replies

7. Shell Programming and Scripting

awk : extracting unique lines based on columns

Hi, snp.txt CHR_A SNP_A BP_A_st BP_A_End CHR_B BP_B SNP_B R2 p-SNP_A p-SNP_B 5 rs1988728 74904317 74904318 5 74960646 rs1427924 0.377333 0.000740085 0.013930081 5 ... (12 Replies)
Discussion started by: genehunter
12 Replies

8. Shell Programming and Scripting

Remove All Lines Between Two Unique Lines

Hi all! Im wondering if its possible to remove all lines between two lines. Im working with a document like this: data1 data2 <Remove> data3 data4 </Remove> data5 data6 I need it to end up like this if that possible: data1 data2 data5 data6 There are multiple instances of... (2 Replies)
Discussion started by: Grizzly
2 Replies

9. Shell Programming and Scripting

Remove lines, Sorted with Time based columns using AWK & SORT

Hi having a file as follows MediaErr.log 84 Server1 Policy1 Schedule1 master1 05/08/2008 02:12:16 84 Server1 Policy1 Schedule1 master1 05/08/2008 02:22:47 84 Server1 Policy1 Schedule1 master1 05/08/2008 03:41:26 84 Server1 Policy1 ... (1 Reply)
Discussion started by: karthikn7974
1 Replies

10. Shell Programming and Scripting

Remove lines with n columns

Hi folks - hope you are all well. I am trying to perform some pre-processing on a data file, to make sure it is in a valid format before performing a data upload. Each row of data in the file should consist of 10 comma delimited fields. Can anyone advise me of a sed/awk command that might... (2 Replies)
Discussion started by: Krispy
2 Replies
Login or Register to Ask a Question