Help with print if two columns is somehow similiar


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with print if two columns is somehow similiar
# 1  
Old 01-18-2016
Help with print if two columns is somehow similiar

Input File:
Code:
GO:0009437,GO:0006355,GO:0006351  GO:0009437
GO:0006777,GO:0032324  GO:0035433,GO:0015992,GO:0071422
GO:0009082,GO:0009097,GO:0006566  GO:0009082,GO:0006351
GO:0000160,GO:0045893,GO:0006351  GO:0006071,GO:0045892,GO:0006351
GO:0009244  GO:0009244
GO:0046417,GO:0009094,GO:0006571  GO:0046417,GO:0009094,GO:0006571
.
.

Desired output File:
Code:
GO:0009437,GO:0006355,GO:0006351  GO:0009437
GO:0009082,GO:0009097,GO:0006566  GO:0009082,GO:0006351
GO:0000160,GO:0045893,GO:0006351  GO:0006071,GO:0045892,GO:0006351
GO:0009244  GO:0009244
GO:0046417,GO:0009094,GO:0006571  GO:0046417,GO:0009094,GO:0006571
.
.

Command I try:
Code:
awk '$1~$2{print}' Input_File
GO:0009437,GO:0006355,GO:0006351  GO:0009437
GO:0009244  GO:0009244
GO:0046417,GO:0009094,GO:0006571  GO:0046417,GO:0009094,GO:0006571

I plan to print out whatever is "somehow similar" between Column 1 and Column 2.
"," is a separator to separate the elements between all record in Column 1 and Column 2.

Thanks for any advice.

Last edited by perl_beginner; 01-19-2016 at 04:59 AM..
# 2  
Old 01-18-2016
Would
Code:
awk '
        {n = split ($2, T, ",")
         for (; n>0; n--) if ($1 ~ T[n]) print
        }
' file
GO:0009437,GO:0006355,GO:0006351  GO:0009437
GO:0009082,GO:0009097,GO:0006566  GO:0009082,GO:0006351
GO:0000160,GO:0045893,GO:0006351  GO:0006071,GO:0045892,GO:0006351
GO:0009244  GO:0009244

do?
# 3  
Old 01-18-2016
Hi RudiC,

It shown "awk: ^ syntax error"?
Am I do anything wrong?

Thanks for advice.
# 4  
Old 01-18-2016
What OS and awk version do you use?
Quote:
Originally Posted by Don Cragun
If you are using a Solaris/SunOS system, use /usr/xpg4/bin/awk or nawk instead of awk .
# 5  
Old 01-18-2016
Hi, I'm using x86_64 and GNU Awk 3.1.7 Smilie
So sorry still quite fresh with Unix T_T
# 6  
Old 01-18-2016
OK, where exactly does the error point to?

---------- Post updated at 12:07 ---------- Previous update was at 12:06 ----------

And, x86_64 is not an OS known to me.
# 7  
Old 01-18-2016
Hi, I type the below command at my interface (Unix OS):
Code:
awk ' {n = split ($2, T, ",") for (; n>0; n--) if ($1 ~ T[n]) print } ' Input_File
awk:  {n = split ($2, T, ",") for (; n>0; n--) if ($1 ~ T[n]) print }
awk:                          ^ syntax error

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use "awk" to print columns from different files in separate columns?

Hi, I'm trying to copy and paste the sixth column from a bunch of files into a single file having each column pasted in separate columns (and not one after each other in just one column.) I tried this code but works only partially because it copied and pasted 50 rows of each column... (6 Replies)
Discussion started by: Frastra
6 Replies

2. UNIX for Dummies Questions & Answers

Hex dump into Wireshark or similiar

Hi Guy's I am trying to find a way of importing the raw hex data from a router dump into a wireshark trace for example. I have had a look at the text2pcap pages and cant seem to work it out. Does anyone have any expierence in this ? If it requires format changes whats the best way of doing... (3 Replies)
Discussion started by: mutley2202
3 Replies

3. Shell Programming and Scripting

Need a perl script similiar to grep -r 'word' /path/to/dir"

Hi , i am looking for a perl script to grep for a string in all files inside a directory . bash command . grep -r 'word' /path/to/dir Thanks, Nvil (3 Replies)
Discussion started by: nevil
3 Replies

4. Shell Programming and Scripting

File processing - have to get the count of similiar types

Input File: c_id=india ---some data-- c_id=US --some data--- c_id=UK --some data-- c_id=india --some data-- c_id=india --some data-- c_id=Russia --some data-- c_id=UK --some data-- c_id=US --some data-- c_id=Africa --some data (5 Replies)
Discussion started by: karumudi7
5 Replies

5. Shell Programming and Scripting

print columns with spaces

Hi All I've a file which is similar to the one given below column1 coulmn2 column3 column4 A B C D X Y F G H I would like to get it in this format A|B|C|D ||X|Y F||G|H Is... (4 Replies)
Discussion started by: Celvin VK
4 Replies

6. Shell Programming and Scripting

Print columns from each row

I have awk command to print column 8 awk '/select/ {print $8}' which will print column 8 But I need to print 3, 5 and 8 column in a row and each column should be de-limited by "\t" Hope anyone help me quickly. (2 Replies)
Discussion started by: elamurugu
2 Replies

7. Shell Programming and Scripting

How to print first 'n' columns?

Hello All, I have a file of 1000 columns. I need to print only the first 100 columns in to a new file. I have tried with the following code awk -F '{ for(i=0; i<=100; i++) {printf $i}' inputfile > outputfile But this is not working, I need the output file also of the same format of the... (4 Replies)
Discussion started by: Fredrick
4 Replies

8. Shell Programming and Scripting

cannot print the columns i want with awk.

hi friends! i have a script where a execute a veritas command, available_media wich retrieves me a list of tapes .lst then i execute cat /tmp/listtapes.lst | grep -v VL |sed '/^$/d'|awk -F, '{print $1, $3, $4, $9} ' > /tmp/media1.lst but it prints all the columns instead of the four... (3 Replies)
Discussion started by: pabloli150
3 Replies

9. UNIX for Dummies Questions & Answers

how do i compare and extract similiar data

I have 2 files. The first file contains user names in one column. The second, and considerably longer, file contains user names in the first column and corresponding full names in the second column. Currently these are in the .xls format. I'd like to be able to compare file1 with file2 and extract... (2 Replies)
Discussion started by: raptrmastr
2 Replies

10. UNIX for Dummies Questions & Answers

Similiar to active directory in Unix?

Hi all, If Windows server have an active directory and active folder/mapping(maybe in unix NFS). Is there any similiar fuctions in unix. Actually if we have a hundred client in unix/linux with unix server, I want to manage user client and access control easier as in windows. Thank you in... (5 Replies)
Discussion started by: blesets
5 Replies
Login or Register to Ask a Question