我想从多个CSV文件中提取一行感兴趣并输出单个文本文件(summary.txt)表格式。感兴趣的行是每个CSV文件中唯一包含冒号的行(":")。 CSV文件都具有以下名称结构: [phenotype]。[letter] _ [number] .csv
为此,我有以下嵌套for循环:
#!/bin/bash
# Working directory
DIR_DATA=/mydirectory/mydata
# Declare an array with all phenotypes
declare -a CHUNK
readarray CHUNK < /mydirectory/phenotypes.txt
# Loop through phenotypes
let i=0
while (( ${#CHUNK[@]} > i )); do
TMP=`echo ${CHUNK[i]} | tr -s " "`
# Copy phenotype name into output file
echo "$TMP" >> $DIR_DATA/summary.txt
# Copy line of interest (which contains the character “:”) into output file
for ALPHABET in a b c; do
for NUMBER in 1 2 3; do
# Copy interaction values
grep : $DIR_DATA/"$TMP"."$ALPHABET”_”$NUMBER”.csv >> $DIR_DATA/summary.txt
done
done
let i++
done
输出如下:
PHENOTYPE1
A_1 0.1 0.2 0.3 0.4
A_2 0.5 0.6 0.7 0.8
B_1 0.9 1.0 1.1 1.2
B_2 1.3 1.4 1.5 1.6
C_1 1.7 1.8 1.9 2.0
C_2 2.1 2.2 2.3 2.4
PHENOTYPE2
A_1 2.5 2.6 2.7 2.8
A_2 2.9 3.0 3.1 3.2
B_1 3.3 3.4 3.5 3.6
B_2 3.7 3.8 3.9 4.0
C_1 4.1 4.2 4.3 4.4
C_2 4.5 4.6 4.7 4.8
但所需的输出如下:
PHENOTYPE1 A_1 0.1 0.2 0.3 0.4
PHENOTYPE1 A_2 0.5 0.6 0.7 0.8
PHENOTYPE1 B_1 0.9 1.0 1.1 1.2
PHENOTYPE1 B_2 1.3 1.4 1.5 1.6
PHENOTYPE1 C_1 1.7 1.8 1.9 2.0
PHENOTYPE1 C_2 2.1 2.2 2.3 2.4
PHENOTYPE2 A_1 2.5 2.6 2.7 2.8
PHENOTYPE2 A_2 2.9 3.0 3.1 3.2
PHENOTYPE2 B_1 3.3 3.4 3.5 3.6
PHENOTYPE2 B_2 3.7 3.8 3.9 4.0
PHENOTYPE2 C_1 4.1 4.2 4.3 4.4
PHENOTYPE2 C_2 4.5 4.6 4.7 4.8
如何修改我的脚本以获得最后的输出结构?
答案 0 :(得分:1)
这应该有效 -
let i=0
while (( ${#CHUNK[@]} > i )); do
TMP=`echo ${CHUNK[i]} | tr -s " "`
# Copy line of interest (which contains the character “:”) into output file
for ALPHABET in a b c; do
for NUMBER in 1 2 3; do
# Copy interaction values
echo -n "$TMP " >> $DIR_DATA/summary.txt
grep : $DIR_DATA/"$TMP"."$ALPHABET”_”$NUMBER”.csv >> $DIR_DATA/summary.txt
done
done
let i++
done
echo -n
将文字放在同一行。