shell脚本实例-交叉证认

1、介绍
  给一个老师写的交叉证认的脚本,人生中第一个脚本值得纪念一下。
2、功能
  txt文件中某列数据格式为xxx1+xxx2,其中xxx1和xxx2均为数字,找出这个文件的行与行之间xxx1相等(也可以有其他关系)并且xxx2相等(同样可以有其他关系)的行并输出。
3、具体内容

#!/bin/bash
echo "cross_match begin!"
#cut the second line and save as "line2.txt"
awk -F '\t'  '{print $2}' /home/scidb/catalog/dr2_param.txt > line2.txt
#replace "+" to "-" in line2
sed -i "s/+/-/g" line2.txt
#cut the "line2.txt" line and save the first field as "line2_1.txt"
cat line2.txt | cut -d '-' -f 1 > line2_1.txt
#cut the "line2.txt" line and save the second field as "line2_1.txt"
cat line2.txt | cut -d '-' -f 2 > line2_2.txt
#paste line2_1 and line2_2 to the source file, and save as new.txt
paste line2_1.txt line2_2.txt /home/scidb/catalog/dr2_param.txt > new.txt
#find the same field_1 in the new.txt"
gawk '{if(a[$1]=="") a[$1]=$0;else if(a[$1]!=""&&flag[$1]=="") {flag[$1]++;printf("%s\n%s\n", a[$1],$0);} else print $0}' new.txt > cross_match_1.txt
#find the same field_2 in the new.txt"
gawk '{if(a[$2]=="") a[$2]=$0;else if(a[$2]!=""&&flag[$2]=="") {flag[$2]++;printf("%s\n%s\n", a[$2],$0);} else print $0}' new.txt > cross_match_2.txt
echo "cross_match end!"

  里面都有注释,都是我一点一点敲上去的,因为虚拟机中没下输入法所以全是英文,仔细看看就能知道是什么。
4、运行步骤
a) chmod +x ./test.sh 使脚本具有执行权限
b) ./cross_match.sh 执行脚本
5、文件解释
运行脚本会产生一系列中间结果,为了处理方便,将他们都存入文档中
a) line2.txt 文本中所需要处理的列,此处为位置
b) line2_1.txt 所要处理列的前半部分,此处为赤经
c) line2_2.txt 所要处理列的后半部分,此处为赤纬
d) new.txt 将赤经赤纬插入为源文件的前两列
e) cross_match_1.txt 赤经符合要求的行
f) cross_match_1.txt 赤纬符合要求的行
6、 注意事项:每次使用脚本时需要更改所要处理的文件路径

PS:简直不能再爱自己了╮(╯▽╰)╭···哈哈哈···让我嘚瑟一下···

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章