linux - 如何比较linux中两列之间的条目?
问题描述
我试图弄清楚氨基酸的第一个字母是否与其字母代码相同。
例如,甘氨酸以 G 开头,其字母代码也是(G)另一方面,精氨酸以 A 开头,但其字母代码是(R)
结果,我试图打印出具有相同字母代码和起始字母的氨基酸。
我有一个 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
我相信下面的代码是比较列的一种方法,但我想知道如何从第一列中提取第一个字母并将其与第二列中的字母进行比较
awk '{ if ($1 == $2) { print $1; } }' < foo.txt
解决方案
请您尝试以下操作。
awk 'BEGIN{FS=","} substr($1,1,1) == $2' Input_file
输出如下。
Serine,S,Ser,hydrophilic,N,0.0715,TCT-TCC-TCA-TCG-AGT-AGC
Threonine,T,Thr,hydrophilic,N,0.0569,ACT-ACC-ACA-ACG
说明:为上述代码添加说明。
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.
推荐阅读
- scala - 什么是集合的非展平 Scala 向量:(1 到 2).flatMap((1 到 3).toSet.subsets(_))?
- excel - VBA 中的日期格式
- opengl - 使用 `GL_UNSIGNED_INT_24_8` 和 `glTexImage2D`
- javascript - WebLogic Server 共享 Javascript 库
- python - 检查字符串中的任何字符是否不在另一个字符串中
- c# - 无法使用我的自定义字体打开我的 RTF 文件
- c - 释放指向包含数组的结构的指针
- c++ - 函数模板参数包后跟模板参数和特化
- python - 如何在我的 api.py 中获取一些(自定义)用户数据?
- swift - SwiftUI 导出或共享文件