Print lines containing same values in a group


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print lines containing same values in a group
# 1  
Old 03-07-2012
Print lines containing same values in a group

Hi,

I have a table like this:

Name A1 A2 A3 B1 B2 B3
111 abc abc abc cbc cbc cbc
222 acv acv n_n bbc bbc bbc
333 bvv bvv bvv cBx ccx ccx
444 ttk ttk ttk kke kke kke
555 mcr mcr mcr mcr mcr mcr
The 1st column is just names of the rows. Columns A1, A2, A3 belongs to group A and columns B1, B2, B3 to B. I need to extract lines containing same element in the columns of each group or both groups.

So desired output is

Name A1 A2 A3 B1 B2 B3
111 abc abc abc cbc cbc cbc
444 ttk ttk ttk kke kke kke
555 mcr mcr mcr mcr mcr mcr

Thanks in advanceSmilieSmilie
# 2  
Old 03-07-2012
You mean, rows where all the columns have something in common between each other? So kke would be related to ttk, but bvv wouldn't be related to cxx?
# 3  
Old 03-07-2012
Try:
Code:
awk 'NR==1 || $2==$3 && $3==$4 && $5==$6 && $6==$7' infile

-or less robust-
Code:
sed -n '1p;/[^ ]*\( [^ ]*\)\1\1\( [^ ]*\)\2\2/p' infile


Last edited by Scrutinizer; 03-07-2012 at 04:44 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-07-2012
Quote:
Originally Posted by Corona688
You mean, rows where all the columns have something in common between each other? So kke would be related to ttk, but bvv wouldn't be related to cxx?
THanks for the reply. Sorry I was unable to explain properly in the first post.

The idea is that in each row, each group must contain three exactly same type of element. For example, one group should contain exactly same type of element in all three columns of a row (such as ttk ttk ttk in A ). It can not contain different elements (such as ttk ttk Ttk in A or cBx ccx ccx in B). In the same row, the other group must also contain same type of element in all the three columns (kke kke kke); Doesnt matter if its the same element as that of group A ( like mcr mcr mcr mcr mcr mcr).

Hope I havent confused you moreSmilie

---------- Post updated at 05:10 PM ---------- Previous update was at 04:40 PM ----------

Quote:
Originally Posted by Scrutinizer
Try:
Code:
awk 'NR==1 || $2==$3 && $3==$4 && $5==$6 && $6==$7' infile

-or less robust-
Code:
sed -n '1p;/[^ ]*\( [^ ]*\)\1\1\( [^ ]*\)\2\2/p' infile

thanks a lot. you rock. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Replace Stub Values In One Group Of Files With Actual Values From Another Group Of Files

I have two directories of files (new-config-files and old-config-files): new-config-files/this-db/config.inc.php new-config-files/that-db/config.inc.php new-config-files/old-db/config.inc.php new-config-files/new-db/config.inc.php new-config-files/random-database/config.inc.php etc. ... (4 Replies)
Discussion started by: spacegoose
4 Replies

2. UNIX for Beginners Questions & Answers

Print lines based upon unique values in Nth field

For some reason I am having difficulty performing what should be a fairly easy task. I would like to print lines of a file that have a unique value in the first field. For example, I have a large data-set with the following excerpt: PS003,001 MZMWR/ L-DWD// * PS003,001... (4 Replies)
Discussion started by: jvoot
4 Replies

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

4. Shell Programming and Scripting

Print values within groups of lines with awk

Hello to all, I'm trying to print the value corresponding to the words A, B, C, D, E. These words could appear sometimes and sometimes not inside each group of lines. Each group of lines begins with "ZYX". My issue with current code is that should print values for 3 groups and only is... (6 Replies)
Discussion started by: Ophiuchus
6 Replies

5. Shell Programming and Scripting

awk print values between consecutive lines

I have a file in below format: file01.txt TERM TERM TERM ABC 12315 68.53 12042013 165144 ABC 12315 62.12 12042013 165145 ABC 12315 122.36 12052013 165146 ABC 12315 582.18 12052013 165147 ABC 12316 2.36 12052013 165141 ABC 12316 ... (8 Replies)
Discussion started by: alex2005
8 Replies

6. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

7. UNIX for Dummies Questions & Answers

Only print lines with 3 numeric values

Hey guys & gals, I am hoping for some advice on a sed or awk command that will allow to only print lines from a file that contain 3 numeric values. From previous searches here I saw that ygemici used the sed command to remove lines containing more than 3 numeric values ; however how... (3 Replies)
Discussion started by: TAPE
3 Replies

8. Shell Programming and Scripting

Sort the file contents in each group....print the group title as well

I've this file and need to sort the data in each group File would look like this ... cat file1.txt Reason : ABC 12345-0023 32123-5400 32442-5333 Reason : DEF 42523-3453 23345-3311 Reason : HIJ 454553-0001 I would like to sort each group on the last 4 fileds and print them... (11 Replies)
Discussion started by: prash184u
11 Replies

9. Shell Programming and Scripting

print lines AFTER lines cointaining a regexp (or print every first and fourth line)

Hi all, This should be very easy but I can't figure it out... I have a file that looks like this: @SRR057408.1 FW8Y5CK02R652T length=34 AGCAGTGGTATCAACGCAGAGTAAGCAGTGGTAT +SRR057408.1 FW8Y5CK02R652T length=34 FIIHFF6666?=:88@@@BBD:::?@ABBAAA>8 @SRR057408.2 FW8Y5CK02TBMHV length=52... (1 Reply)
Discussion started by: kmkocot
1 Replies

10. Shell Programming and Scripting

awk help required to group output and print a part of group line and original line

Hi, Need awk help to group and print lines to format the output as shown below INPUT FORMAT set echo on set heading on set spool on /* SCHEMA1 */ CREATE TABLE T1; /* SCHEMA1 */ CREATE TABLE T2; /* SCHEMA1 */ CREATE TABLE T3; /* SCHEMA1 */ CREATE TABLE T4; /* SCHEMA1 */ CREATE TABLE T5;... (5 Replies)
Discussion started by: rajan_san
5 Replies
Login or Register to Ask a Question