Linux Bash将大文件拆分为小文件

Linux Bash将大文件拆分为小文件,linux,bash,file,shell,Linux,Bash,File,Shell,所以我想根据第8列将一个相当大的文件分成几个小文件。所以我写了这个脚本: #!/bin/bash run_command(){ eval ${1} wait } chInput=("1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "Z" "T" "G" "F" "A" "D" "P") sampInput=("heyA") for ((x=0;x<${#chInput

所以我想根据第8列将一个相当大的文件分成几个小文件。所以我写了这个脚本:

#!/bin/bash
run_command(){
eval ${1}
wait
}
chInput=("1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "Z" "T" "G" "F" "A" "D" "P")
sampInput=("heyA")

for ((x=0;x<${#chInput[@]};x++));do
com="awk -F'\t' '$8=="${chInput[x]}"' /home/location/"$sampInput"_This_P.txt > "$sampInput"Ch"${chInput[x]}".txt"
run_command "${com}"
done
但只是做

awk -F'\t' '$8==1' /home/location/heyA_This_P.txt > Ch1.txt
从命令行输入的命令确实有效


我能做些什么来解决这个问题?

最严重的问题是双引号<代码>$8将在分配变量时被某些内容(可能根本没有)替换。您可以尝试使用带有适当转义的单引号,但真正的解决方案可能是深呼吸并重新开始,而不必在变量中包含
eval
或Awk脚本

不管怎样,这种椒盐卷饼逻辑的目的是什么?你也许应该读一读,并把这些建议牢记在心

以下是解决问题的快速尝试:

#!/bin/bash

chInput=("1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "Z" "T" "G" "F" "A" "D" "P")
sampInput=("heyA")

for ((x=0;x<${#chInput[@]};x++));do
    awk -F'\t' '$8=="'"${chInput[x]}"'"' /home/location/"$sampInput"_This_P.txt > "$sampInput"Ch"${chInput[x]}".txt
done

与经典的Bourne shell兼容。)

我之所以尝试此操作,是因为我正在运行此文件与另一个文件之间的比较,并且由于文件的大小,程序挂起了此文件。我想四处玩玩,看看我是否可以采取懒散的方式来分割文件,而不是重写一些代码。我读了一些文章说它会起作用,一些文章说不会,我想亲自看看
#!/bin/bash

chInput=("1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" "13" "14" "15" "16" "17" "18" "19" "Z" "T" "G" "F" "A" "D" "P")
sampInput=("heyA")

for ((x=0;x<${#chInput[@]};x++));do
    awk -F'\t' '$8=="'"${chInput[x]}"'"' /home/location/"$sampInput"_This_P.txt > "$sampInput"Ch"${chInput[x]}".txt
done
for c in "1" "2" "3" "4" "5" "6" "7" "8" "9" "10" "11" "12" \
         "13" "14" "15" "16" "17" "18" "19" "Z" "T" "G" "F" \
         "A" "D" "P"
do
    awk -F'\t' '$8=="'"$c"'"' /home/location/"$sampInput"_This_P.txt > "${sampInput}Ch$c.txt"
done