使用第二个pdfgrep find作为变量

时间:2019-02-16 17:13:08

标签: bash grep

我正在使用pdfgrep在pdf中查找某些信息,然后使用该信息重命名该文件。但是,对于日期,我需要使用与保存文件不同的方式来格式化它们。我打算将日期(月,日,年)的每个部分作为单独变量提取,以便可以使用if语句重新格式化它们。有没有办法将第二个发现的数字存储为bash中的变量?

shopt -s nullglob nocaseglob

for f in *.pdf; do
    id1=$(pdfgrep -i "ID #: " "$f" | grep -oE "[M][0-9-][0-9]+")
    id2=$(pdfgrep -i "Second ID: " "$f" | grep -oE "[V][0-9-][0-9]+")
    # Check id1 is found, else do nothing
    if [ ${#id1} ]; then
        mv "$f" "${id1}_${id2}.pdf"
    fi
done

终端中的命令结果:

$ pdfgrep -i "Date of Birth: " 2.pdf | grep -oE "[0-9]+"
3
23
1977

1 个答案:

答案 0 :(得分:0)

您可以使用<(...)进程替换将grep的输出重定向到三个read命令中,这些命令将值存储在单独的变量中。

{ read month; read day; read year; } < <(pdfgrep -i "Date of Birth: " 2.pdf | grep -oE "[0-9]+")

结果:

$ echo "$month-$day-$year"
3-23-1977