how to retrieve lines that the first 4 columns have different values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to retrieve lines that the first 4 columns have different values
# 1  
Old 07-22-2011
how to retrieve lines that the first 4 columns have different values

Hi, all:
I am not familiar with unix,and just started awk scripts. I want to retrieve lines that have the first 4 columns with different values. For example, the input is like this (tab delimited file with one header)


Code:
r1 A A A A x
r2 A B B A x
r3 B B B B x

the output should be (header is maintained):
Code:
r2 A B B A x

Is there an easy way to do it? Thanks

Last edited by radoulov; 07-22-2011 at 04:14 PM.. Reason: Code tags.
# 2  
Old 07-22-2011
Columns 3 and 4 in r2 A B B are the same, while you said that those columns should have different values. Can you clarify that?
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 07-22-2011
awk '!(($2 == $3) && ($2 == $4) && ($2 == $5))' < infile > outfile
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 07-22-2011
sorry, r1 , r2 just means first row, second row, not the actual field. Thanks!

---------- Post updated at 02:15 PM ---------- Previous update was at 02:13 PM ----------

Smilie Thanks! But if I want to test first 8 fields, i have to type 7 times of $2==$x? Any shortcut of that?
# 5  
Old 07-22-2011
Could you please post a bigger sample? I don't get the logic ... Smilie
Please post "realistic data" - remove r1, r2 if they are not part of the actual input.
# 6  
Old 07-22-2011
here is a bigger sample

Input file (first 5 rows)
Code:
   c1 c2 c3 c4 c5 c6 c7 c8 c9
r1 A   A   A  A  A  A  A  A   x
r2 B   B   A  A   B  B  A  M   x
r3 B   B   B  B   B   B  B  B   x
r4 A  M   B  B   B   B  B  B   y
r5 A  A   A  A  A   A  A   A   y

the output will be
Code:
    c1 c2 c3  c4 c5 c6...
r2 B   B   A  A   B  B  A  M   x
r4 A  M   B  B   B   B  B  B   y

does this example make sense? Thanks! Smilie

Last edited by radoulov; 07-22-2011 at 04:22 PM.. Reason: Code tags!
# 7  
Old 07-22-2011
Yes,
thanks!

Is the number of spaces between the letters fixed?
This User Gave Thanks to radoulov 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

How to print lines that have values in certain columns ?

Hi, everyone I have a dataset like this: A B C D A C C D E F G H F D K Y X A K K C Gsome of columns have no values in each line. I want to print all lines that have 1/2/3/4 values, export separately to four files. What I expected is like this: file1 Y file 2 A C X Afile 3... (3 Replies)
Discussion started by: nengcheng
3 Replies

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

3. Shell Programming and Scripting

Compare columns of two files and retrieve data

Hi guys, I need your help. I have two files: file1 1 3 5 file2 1,XX 2,AA 3,BB 4,CC 5,DD I would like to compare the first column and where they are equal to write that output in a new file: 1,XX 3,BB (7 Replies)
Discussion started by: apenkov
7 Replies

4. Programming

Sctp api name to retrieve the values of structure sctpassoctable

Hi i want a sctp (lksctp) api which can retrieve the values of the sctp structure "sctpAssocTable" It is sctpassoctable or sctpassocentry. SctpAssocEntry ::= SEQUENCE { sctpAssocId Unsigned32, sctpAssocRemHostName OCTET STRING, sctpAssocLocalPort ... (1 Reply)
Discussion started by: harioum
1 Replies

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

6. Shell Programming and Scripting

Retrieve values using soap moudle

Can someone tell me how to know the values used in soap api. I need to know what values are used in soap xml. Can we get all the values used in soap api. I know that we can get it by using SOAP::Lite module in perl. But it is like we are supplying keys and we are getting values. I want... (0 Replies)
Discussion started by: Anjan1
0 Replies

7. Shell Programming and Scripting

Retrieve multiple values for each iteration

Hi All, I tried to retrieve multiple values using for/foreach loop in each iteration. But couldn't get it. Please help me. Below are my try outs: #!/bin/ksh foreach {i,j,k} {5150 5540 5149 5640 5151 5920 5362 5965 5147 5895 5061} echo $i,$j,$k end Error: ./mulcns.ksh: foreach: not... (4 Replies)
Discussion started by: cns1710
4 Replies

8. UNIX for Dummies Questions & Answers

How to compare two columns and retrieve data

I am a newbie to Unix and slowly learning it. I have a large data set with 8 different columns. I want to compare two columns and retrieve data if the two columns have similar number. I have attached the example. There are two columns (S-Contig and N-Contig). I want to retrieve the data from... (7 Replies)
Discussion started by: bjorngill
7 Replies

9. UNIX for Dummies Questions & Answers

need to retrieve values of parameter

Hi, I am just new to this shell scripting wizard..i have a text file which contains content as below: Parameter=10; What should be the script which only fetches the value 10 not the Parameter. The idea is to get the logic behind getting the value alone not its parameter,.... (6 Replies)
Discussion started by: d8011
6 Replies

10. Shell Programming and Scripting

to retrieve unique values

Hi all, I have a 10.txt file. In this file 2 words are present by name Active and Inactive and these words are repeated 7000 times. I want to take the unique 2 words from this 7000 lines. Thanks Mahalakshmi.A (3 Replies)
Discussion started by: mahalakshmi
3 Replies
Login or Register to Ask a Question