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:簡直不能再愛自己了╮(╯▽╰)╭···哈哈哈···讓我嘚瑟一下···

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