Joh*_*nny 4 scripting shell-script text-processing arithmetic
我需要加速一个脚本,该脚本基本上确定每行的所有“列”是否相同,然后写入一个包含相同元素之一或“no_match”的新文件。该文件以逗号分隔,由大约 15,000 行组成,并包含不同数量的“列”。
例如:
1-69
4-59,4-59,4-59,4-61,4-61,4-61
1-46,1-46
4-59,4-59,4-59,4-61,4-61,4-61
6-1,6-1
5-51,5-51
4-59,4-59
Run Code Online (Sandbox Code Playgroud)
写入一个新文件:
1-69
no_match
1-46
no_match
6-1
5-51
4-59
Run Code Online (Sandbox Code Playgroud)
删除第二行和第四行,因为它们包含不同的列。
这是我远非优雅的脚本:
#!/bin/bash
ind=$1 #file in
num=`wc -l "$ind"|cut -d' ' -f1` #number of lines in 'file in'
echo "alleles" > same_alleles.txt #new file to write to
#loop over every line of 'file in'
for (( i =2; i <= "$num"; i++));do
#take first column of row being looped over (string to check match of other columns with)
match=`awk "FNR=="$i" {print}" "$ind"|cut -d, -f1`
#counts how many matches there are in the looped row
match_num=`awk "FNR=="$i" {print}" "$ind"|grep -o "$match"|wc -l|cut -d' ' -f1`
#counts number of commas in each looped row
comma_num=`awk "FNR=="$i" {print}" "$ind"|grep -o ","|wc -l|cut -d' ' -f1`
#number of columns in each row
tot_num=$((comma_num + 1))
#writes one of the identical elements if all contents of row are identical, or writes "no_match" otherwise
if [ "$tot_num" == "$match_num" ]; then
echo $match >> same_alleles.txt
else
echo "no_match" >> same_alleles.txt
fi
done
#END
Run Code Online (Sandbox Code Playgroud)
目前,脚本需要大约 11 分钟来完成所有约 15,000 行。我不太确定如何加快速度(老实说我很惊讶我什至可以让它工作)。任何时候被淘汰都会很棒。以下是可以使用的 100 行的较小摘录:
allele
4-39
1-46,1-46,1-46
4-39
4-4,4-4,4-4,4-4
3-23,3-23,3-23
3-21,3-21
4-34,4-34
3-33
4-4,4-4,4-4
4-59,4-59
3-23,3-23,3-23
1-45
1-46,1-46
3-23,3-23,3-23
4-61
1-8
3-7
4-4
4-59,4-59,4-59
1-18,1-18
3-21,3-21
3-23,3-23,3-23
3-23,3-23,3-23
3-30,3-30-3
4-39,4-39
4-61
2-70
4-38-2,4-38-2
1-69,1-69,1-69,1-69,1-69
1-69
4-59,4-59,4-59,4-61,4-61,4-61
1-46,1-46
4-59,4-59,4-59,4-61,4-61,4-61
6-1,6-1
5-51,5-51
4-59,4-59
1-18
3-7
1-69
4-30-4
4-39
1-69
1-69
4-39
3-23,3-23,3-23
4-39
2-5
3-30-3
4-59,4-59,4-59
3-21,3-21
4-59,4-59
3-9
4-59,4-59,4-59
4-31,4-31
1-46,1-46
1-46,1-46,1-46
5-51,5-51
3-48
4-31,4-31
3-7
4-61
4-59,4-59,4-59,4-61,4-61,4-61
4-38-2,4-38-2
3-21,3-21
1-69,1-69,1-69
3-23,3-23,3-23
4-59,4-59
3-48
3-48
1-46,1-46
3-23,3-23,3-23
3-30-3,3-30-3
1-46,1-46,1-46
3-64
3-73,3-73
4-4
1-18
3-7
1-46,1-46
1-3
4-61
2-70
4-59,4-59
5-51,5-51
3-49,3-49
4-4,4-4,4-4
4-31,4-31
1-69
1-69,1-69,1-69
4-39
3-21,3-21
3-33
3-9
3-48
4-59,4-59
4-59,4-59
4-39,4-39
3-21,3-21
1-18
Run Code Online (Sandbox Code Playgroud)
我的脚本需要大约 7 秒才能完成。
$ awk -F, '{ for (i=2; i<=NF; ++i) if ($i != $1) { print "no_match"; next } print $1 }' file
1-69
no_match
1-46
no_match
6-1
5-51
4-59
Run Code Online (Sandbox Code Playgroud)
对不起,我什至没有看你的代码,事情太多了。当您发现自己awk在循环体中对同一数据调用了3 次时,您将不得不寻找其他方法来更有效地执行此操作。此外,如果你参与awk,你并不需要grep,并cut为awk将能够轻松地做他们的任务(未在这种情况下,虽然需要)。
awk上面的脚本一次读取一个逗号分隔的行,并将每个字段与第一个字段进行比较。如果任何测试失败,no_match则打印该字符串并且脚本继续下一行。如果循环结束(没有发现不匹配),则打印第一个字段。
作为脚本:
#!/usr/bin/awk -f
BEGIN { FS = "," }
{
for (i=2; i<=NF; ++i)
if ($i != $1) {
print "no_match"
next
}
print $1
}
Run Code Online (Sandbox Code Playgroud)
FS是输入字段分隔符,也可以使用-F命令行上的选项进行设置。awk将拆分此字符上的每一行以创建字段。NF 是当前记录中的字段数(“行上的列数”)。$i指当前记录中的第 i:th 字段,其中i可能是变量或常量(如$1)。有关的:
干变体:
#!/usr/bin/awk -f
BEGIN { FS = "," }
{
output = $1
for (i=2; i<=NF; ++i)
if ($i != output) {
output = "no_match"
break
}
print output
}
Run Code Online (Sandbox Code Playgroud)