Bash 将变量读入进程替换中使用的文件
在下面的Bash 将变量读入进程替换中使用的文件,bash,awk,Bash,Awk,在下面的bash中,我试图使用下面的awk提取最后一个“非空行”,并将其存储在变量filename中,该变量稍后用于bash过程替换 while IFS= read -r line; do mapArray["${line%_*}"]="$line" done < <(tail -n +3 /home/cmccabe/Desktop/NGS/API/$filename/analysis.txt) can not read file at `/home/cmccabe/Desktop
bash
中,我试图使用下面的awk
提取最后一个“非空行”,并将其存储在变量filename
中,该变量稍后用于bash
过程替换
while IFS= read -r line; do
mapArray["${line%_*}"]="$line"
done < <(tail -n +3 /home/cmccabe/Desktop/NGS/API/$filename/analysis.txt)
can not read file at `/home/cmccabe/Desktop/NGS/API//analysis.txt`
analysis.log始终是此文件的最后一行
R_2017_03_10_15_11_27_user_S5-00580-35-Medexome
R_2017_03_14_10_35_42_user_S5-00580-36-Medexome
R_2017_03_14_13_13_34_user_S5-00580-37-Medexome
bash已尝试
while IFS= read -r line; do
mapArray["${line%_*}"]="$line"
filename=$(awk 'END{print}' /home/cmccabe/medex.logs/analysis.log)
done < <(tail -n +3 /home/cmccabe/Desktop/NGS/API/$filename/analysis.txt)
在设置
filename
之前,您的代码试图从API/$filename/analysis.txt
读取
该任务需要在循环之外完成,并在开始之前完成
declare -A mapArray ## needs to be associative for the below to work
filename=$(awk 'END{print}' /home/cmccabe/medex.logs/analysis.log)
while IFS= read -r line; do
mapArray["${line%_*}"]="$line"
done < <(tail -n +3 "/home/cmccabe/Desktop/NGS/API/$filename/analysis.txt")
declare-mapArray##必须是关联的,才能使下面的工作正常
filename=$(awk'END{print}'/home/cmccabe/medex.logs/analysis.log)
而IFS=读取-r行;做
mapArray[“${line%\u*}”]=“$line”
done<错误消息清楚地显示未设置文件名
(如果设置了,则设置为空字符串)。“始终是最后一行”?还是最后一条非空行?如果您可以使示例自包含,这会有所帮助。如果我执行awk
并查看输出,则会提取正确的行,但它似乎没有读入变量$filename
。谢谢:)。也就是说,如果您可以重现awk'END{print}'
发出一行空行输出,并向我们展示如何在此代码中创建,那么其他人将能够重现您的问题。例如,一次尝试(失败,因为awk
做了正确的事情):awk'END{print}<我建议您编辑问题,然后询问如何从文件中提取最后一行非空行(可能会删除所有与此无关的内容?如果我们知道问题出在提取文件名上,那么在问题中包含因错误使用该空文件名而产生的问题是没有意义的;请参阅)。
cat name.txt
R_2017_03_10_15_11_27_user_S5-00580-35-Medexome
R_2017_03_14_10_35_42_user_S5-00580-36-Medexome
R_2017_03_14_13_13_34_user_S5-00580-37-Medexome
awk 'END{print}' name.txt
R_2017_03_14_13_13_34_user_S5-00580-37-Medexome
declare -A mapArray ## needs to be associative for the below to work
filename=$(awk 'END{print}' /home/cmccabe/medex.logs/analysis.log)
while IFS= read -r line; do
mapArray["${line%_*}"]="$line"
done < <(tail -n +3 "/home/cmccabe/Desktop/NGS/API/$filename/analysis.txt")