【发布时间】:2017-04-17 17:09:06
【问题描述】:
使用下面的tab-delimitedfile,我试图验证标题行1,然后将该数字存储在变量$header 中,以便在几个if 语句中使用。如果 $header 等于 10 则 file has expected number of fields,但如果 $header 小于 10 file is missing header for: 并且缺少的标题字段打印在下面。 bash 似乎很接近,如果我单独使用awk,它似乎工作得很好,但我似乎无法在if 中使用它。谢谢你:)。
文件.txt
Index Chr Start End Ref Alt Freq Qual Score Input
1 1 1 100 C - 1 GOOD 10 .
2 2 20 200 A C .002 STRAND BIAS 2 .
3 2 270 400 - GG .036 GOOD 6 .
file2.txt
Index Chr Start End Ref Alt Freq Qual Score
1 1 1 100 C - 1 GOOD 10
2 2 20 200 A C .002 STRAND BIAS 2
3 2 270 400 - GG .036 GOOD 6
重击
for f in /home/cmccabe/Desktop/validate/*.txt; do
bname=`basename $f`
pref=${bname%%.txt}
header=$(awk -F'\t' '{print NF, "fields detected in file and they are:" ORS $0; exit}') $f >> ${pref}_output # detect header row in file and store in header and write to output
if [[ $header == "10" ]]; then # display results
echo "file has expected number of fields" # file is validated for headers
else
echo "file is missing header for:" # missing header field ...in file not-validated
echo "$header"
fi # close if.... else
done >> ${pref}_output
file.txt 的期望输出
file has expected number of fields
file1.txt 的期望输出
file is missing header for:
Input
【问题讨论】:
-
是 10 固定或应该根据标题记录而变化
-
10 是固定的,因为这是所需的标头字段的数量。如果
NF小于该值,则打印缺少的NF。谢谢你:)。 -
如果一个文件有 11 列或更多列怎么办?这是多大的问题?到目前为止,您已经表达了对 9 列或更少列的担忧,但您也可能拥有太多列。
标签: bash