简体   繁体   English

如何在Linux中比较两列之间的条目?

[英]How can you compare entries between two columns in linux?

I am trying to figure out whether the first letter of an amino acid is the same as its letter code. 我试图弄清一个氨基酸的首字母是否与它的字母代码相同。

For example, Glycine begins with G and its letter code is also (G) On the other hand, Arginine begins with A but its letter code is (R) 例如,甘氨酸以G开头,其字母代码也为(G)。另一方面,精氨酸以A开头但其字母代码为(R)

I am trying to print out, as a result, the amino acids that have the same letter code and starting alphabet. 因此,我试图打印出具有相同字母代码和起始字母的氨基酸。

I have a CSV datafile in which the columns are delimited by ',' 我有一个CSV数据文件,其中的列由','分隔

Name,One letter code,Three letter code,Hydropathy,Charge,Abundance,DNA codon(s)
Arginine,R,Arg,hydrophilic,+,0.0514,CGT-CGC-CGA-CGG-AGA-AGG
Asparagine,N,Asn,hydrophilic,N,0.0447,AAT-AAC
Aspartate,D,Asp,hydrophilic,-,0.0528,GAT-GAC
Glutamate,E,Glu,hydrophilic,-,0.0635,GAA-GAG
Glutamine,Q,Gln,hydrophilic,N,0.0399,CAA-CAG
Lysine,K,Lys,hydrophilic,+,0.0593,AAA-AAG
Serine,S,Ser,hydrophilic,N,0.0715,TCT-TCC-TCA-TCG-AGT-AGC
Threonine,T,Thr,hydrophilic,N,0.0569,ACT-ACC-ACA-ACG

I believe the code below is one way to compare columns, but I am wondering how I can extract the first letter from the first column and compare that with the alphabet in the second column 我相信下面的代码是比较列的一种方法,但是我想知道如何从第一列中提取第一个字母并将其与第二列中的字母进行比较

awk '{ if ($1 == $2) { print $1; } }' < foo.txt

Could you please try following. 您可以尝试以下吗?

awk 'BEGIN{FS=","} substr($1,1,1) == $2' Input_file

Output will be as follows. 输出如下。

Serine,S,Ser,hydrophilic,N,0.0715,TCT-TCC-TCA-TCG-AGT-AGC
Threonine,T,Thr,hydrophilic,N,0.0569,ACT-ACC-ACA-ACG

Explanation: Adding explanation for above code. 说明:添加上述代码的说明。

awk '                     ##Starting awk program here.
BEGIN{                    ##Starting BEGIN section for awk here.
 FS=","                   ##Setting FS as comma here, field separator.
}                         ##Closing BLOCK for BEGIN here.
substr($1,1,1) == $2      ##Using substr function of awk to get sub string from line, substr(line/variable/field, starting point, ending point) is method for using it. Getting 1st letter of $1 and comparing it with $2 of current line, if TRUE then it will print current line.
' Input_file              ##Mentioning Input_file name here.

Simpler way using grep : 使用grep更简单方法:

$ grep -E '^(.)[^,]*,\1' input.csv 
Serine,S,Ser,hydrophilic,N,0.0715,TCT-TCC-TCA-TCG-AGT-AGC
Threonine,T,Thr,hydrophilic,N,0.0569,ACT-ACC-ACA-ACG

Same as RavinderSingh's expression, but field selector attribute is different. 与RavinderSingh的表达式相同,但字段选择器属性不同。

awk -F "," 'substr($1,1,1) == $2' InFile

Serine,S,Ser,hydrophilic,N,0.0715,TCT-TCC-TCA-TCG-AGT-AGC
Threonine,T,Thr,hydrophilic,N,0.0569,ACT-ACC-ACA-ACG

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM