首页 > 解决方案 > 正确使用带有变量的 printf

问题描述

我正在使用以下命令检索信息:

EXP=$(curl "https://www.ebi.ac.uk/ena/data/warehouse/filereport?accession=ERR146982&result=read_run&fields=study_accession" 2>/dev/null| tail -n 1)
DESC=$(curl "https://www.ebi.ac.uk/ena/data/view/${EXP}&display=xml" 2>/dev/null | grep "<DESCRIPTION>" | sed 's/     <DESCRIPTION>//g' | sed 's/<\/DESCRIPTION>//g')
printf "$line\t$DESC\n"

但是,我得到了错误printf: ')': invalid format character 和输出(此处缩短)ERR146988 Background: Observations that the airway microbiome is disturbed [...] Streptococcus being the most common genus (49.72

我如何正确地printf与变量结合使用,这些变量基本上可以取所有可能的值,包括特殊字符?

期望的输出:

ERR2319455    The American Gut project is the largest crowdsourced citizen science project to date. Fecal, oral, skin, and other body site samples collected from thousands of participants represent the largest human microbiome cohort in existence. Detailed health and lifestyle and diet data associated with each sample is enabling us to deeply examine associations between the human microbiome and factors such as diet (from vegan to near carnivore and everything in between), season, amount of sleep, and disease states such as IBD, diabetes, or autism spectrum disorder-as well as many other factors not listed here. The American Gut project also encompasses the British Gut and Australian Gut projects, widening the cohort beyond North America. As the project continues to grow, we will be able to identify significant associations that would not be possible with smaller, geographically and health/disease status-limited cohorts.

标签: bashprintf

解决方案


你要:

printf "$line\t%s\n" "${DESC}"

查看

help printf

顺便说一句,如果您安装了 xmllint,您可以使用 xpath 更好地获得描述:

curl "https://www.ebi.ac.uk/ena/data/view/${EXP}&display=xml" \
  | xmllint --xpath '//DESCRIPTION/text()' -

推荐阅读