将变量读入流程替换中使用的文件

时间:2017-03-20 16:03:29

标签: 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/NGS/API//analysis.txt`

awk提取$ filename

# extract folder for variable filename
filename=$(awk 'END{print}' /home/cmccabe/medex.logs/analysis.log)

analysis.log always the last line of this file

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)

单独执行awk

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

1 个答案:

答案 0 :(得分:1)

您的代码尝试在filename 之前阅读设置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")

该分配需要在循环外完成,并在开始之前完成。

{{1}}