Help to print the line that share exactly same with column one content


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help to print the line that share exactly same with column one content
# 1  
Old 02-13-2014
Help to print the line that share exactly same with column one content

Input file :
Code:
AAAG TC
AACCCT AACCCT AACCCT AACCCT
TCTG TCTG TCTG AC AC TCTG TCTG AC AC AC
AC AC
AGTG AC
AGTG TCC

Desired output file :
Code:
AACCCT AACCCT AACCCT AACCCT
AC AC

I would like to print out the line that share exactly same as the first column data content.
Column one data is treated as the reference record while comparing with other column content.

Thanks for any advice.
# 2  
Old 02-13-2014
Try something like:
Code:
awk '{for(i=2; i<=NF; i++) if($i!=$1) next}1' file

Note that this would also match lines that have only one field, which may or may not be what you want..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-13-2014
Hi Scrutinizer,

I just have some question need your advice :
Input file:
Code:
AAAG TC
AACCCT AACCCT AACCCT AACCCT
TCTG TCTG TCTG AC AC TCTG TCTG AC AC AC
AC AC
AC AC
AT AT
AGTG AC
AGTG TCC
ATCT ATCT ATCT ATCT ATCT
AC AC

Command 1
Code:
awk '{for(i=2; i<=NF; i++) if($i!=$1) next}1' file
AACCCT AACCCT AACCCT AACCCT
AC AC
AC AC
AT AT
ATCT ATCT ATCT ATCT ATCT
AC AC

Command 2
Code:
awk '{for(i=2; i<=NF; i++) if($i==$1) next}1' file
AAAG TC
AGTG AC
AGTG TCC

From command 2, I just edit "!=" into "==" based on your command 1.
I just curious why command 2 won't print out "TCTG TCTG TCTG AC AC TCTG TCTG AC AC AC".

Thanks for any advice.

Last edited by Scrutinizer; 02-14-2014 at 03:44 AM.. Reason: changed - to =
# 4  
Old 02-13-2014
Lets look at the pseudo code for your 2 commands it might help you see what has happened:

1:Don't print if any field is different to field#1

2:Don't print if any field is the same as field#1
This User Gave Thanks to Chubler_XL For This Post:
# 5  
Old 02-13-2014
Hi,

I guess I understand the reason right now.
Many thanks Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Break a line content and print as column

Hi, I have urls in my input file like this (1 Reply)
Discussion started by: tmonk1
1 Replies

2. Shell Programming and Scripting

Print next line beside preceding line on column match

Hi, I have some data like below: John 254 Chris 254 Matt 123 Abe 123 Raj 487 Moh 487 How can i print it using awk to have: 254 John,Chris 123 Matt,Abe 487 Raj,Moh Thanks. (4 Replies)
Discussion started by: james2009
4 Replies

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

4. Shell Programming and Scripting

Print the column content based on the header

i have a input of csv file as below but the sequence of column get changed. I,e it is not necessary that name comes first then age and rest all, it may vary. name,age,marks,roll,section kevin,25,80,456,A Satch,23,56,789,B Meena,24,78,H245,C So i want to print that column entires which... (12 Replies)
Discussion started by: millan
12 Replies

5. Shell Programming and Scripting

Break a line content and print as column

Hi, I have urls in my input file like this http://unix.com/abc/def http://unix.com/kil/min I want to use the / as separator and print the last content as another column like this http://unix.com/abc/def def http://unix.com/kil/min min I was using awk -F option and then joining the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Shell Programming and Scripting

want to print the file content from the specific line

Hi All, I would like to print the content from the specific line of a file . For example... i have file abc.txt which has 100 lines of code ,from this file i would like to print the content from 20,19,18th line......like that Regards Srikanth (4 Replies)
Discussion started by: srikanthg
4 Replies

7. Shell Programming and Scripting

Help with data reformat if share share content

Input data: read1_data1 read1_data1 read2_data1 read3_data1 read4_data1 read4_data1 read4_data1 read5_data1 . . Desired output result: read1_data1 read1_data2 read2_data1 read3_data1 read4_data1 (3 Replies)
Discussion started by: perl_beginner
3 Replies

8. Shell Programming and Scripting

read file line by line print column wise

I have a .csv file which is seperated with (;) inputfile --------- ZZZZ;AAAA;BBB;CCCC;DDD;EEE; YYYY;BBBB;CCC;DDDD;EEE;FFF; ... ... reading file line by line till end of file. while reading each line output format should be . i need to print only specific columns let say 5th... (2 Replies)
Discussion started by: rocking77
2 Replies

9. Shell Programming and Scripting

How to add a new line between different column data content?

Input file: Germany 10 500 5000 Germany 20 500 5000 Germany 50 10 500 England 5 10 25 USA 30 25 55 USA 20 35 90 Japan 2 5 60 Singapore 50 30 90 Singapore 150 230 290 Output file: Germany 10 500 5000 Germany 20 500 5000 Germany 50 10 500 England 5 10 25 (7 Replies)
Discussion started by: patrick87
7 Replies

10. Shell Programming and Scripting

Perl Script - Print Content of next line

Good evening to you all perl experts I need your help I have a very simple script where I´m trying to print a line from status.dat file. The script will find the line containing "servicestatus", and I want to print the content of the next line. For example, the file contains this text:... (6 Replies)
Discussion started by: zarahel
6 Replies
Login or Register to Ask a Question