我有一个脚本,我需要变成一个循环,当我输入文件名时,脚本的工作正是我需要的(My Awk-fu非常弱,所以很好),如下例所示。
#!/bin/bash
awk -v FS='(<LastName>|<\/LastName>)' '{print $2}' 17822624.xml >> test.csv
awk -v FS='(<FirstName>|<\/FirstName>)' '{print $2}' 17822624.xml >> test.csv
awk -v FS='(<Id>|<\/Id>)' '{print $2}' 17822624.xml >> test.csv
awk -v FS='(<Dob>|<\/Dob>)' '{print $2}' 17822624.xml >> test.csv
awk -v FS='(<TestDate>|<\/TestDate>)' '{print $2}' 17822624.xml >> test.csv
#this awk command is different because their are multiple different values i need it to pull back
awk -F '<Value>|<\/Value>' '{for (i=2; i<=NF; i+=2) print $i}' 17822624.xml >> test.csv
cat test.csv | tr '\n' ',' > test2.csv
awk 'BEGIN{RS=ORS=','} {sub(/ ..:..:..$/,'')} !seen[$0]++' test2.csv > test3.csv
当我将其转换为for do done循环时,我遇到了问题。我认为它与单引号和双引号相关,而且与{}相关。但我运气不好,尝试了很多不同的事情。
这是我创建的循环
#!/bin/bash
#file location
XMLDIR='/home/amoore19/XML/00581-001/scores'
NEWXML='/home/amoore19/XML/00581-001'
#this gives me the filenames without the path
for file in `ls ${XMLDIR}/*.xml | xargs -n1 basename`
do
awk -v FS='(<LastName>|<\/LastName>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -v FS='(<FirstName>|<\/FirstName>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -v FS='(<Id>|<\/Id>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -v FS='(<Dob>|<\/Dob>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -v FS='(<TestDate>|<\/TestDate>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -F '<Value>|<\/Value>' '{for (i=2; i<=NF; i+=2) print $i}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
"${NEWXML}/${file}" | tr '\n' ',' > "${NEWXML}/1${file}"
awk "BEGIN{RS=ORS=,} {sub(/..:..:..$/,"")} !seen[$0]++" "${NEWXML}/1${file}" > "${NEWXML}/2${file}"
done
这是我得到的错误:我已经尝试将单引号切换为双倍而运气不太好
$ ./ifthen.sh
ls: /home/amoore19/XML/00581-001/scores/*.xml: No such file or directory
basename: missing operand
Try `basename --help' for more information.
就像我说顶级示例效果很好,但我需要能够遍历文件夹中的多个文件。
我知道这很难看,我知道awk可以自行完成整个过程,但我不知道如何创建它。有一天,我会,但现在这是我能做的最好的,我真的可以使用一些帮助/指导我的循环错误。
谢谢
更新
我在使用shellcheck后尽可能地更新了代码。仍然得到提到的错误。
新更新
所以我认为我发现了我的问题,但我不明白为什么会出现问题?我做了一个新的bash脚本,简单地对目录执行cd,它仍然说它不存在。如果我这样做只是/ home / amoore19 / XML它可以工作,但文件夹名称为00581-001的东西引起了我的问题...任何想法?
最后更新 我删除了00581-001文件夹并重新创建它并将文件重新移入,我不再收到错误...我猜它不知何故变得腐败了? (我在娱乐时偶然增加了0)
这是当前的工作脚本:
#!/bin/bash
#file location
XMLDIR='/home/amoore19/XML/000581-001/scores'
NEWXML='/home/amoore19/XML/000581-001'
#this gives me the filenames without the path
for file in `ls ${XMLDIR}/*.xml | xargs -n1 basename`
do
awk -v FS='(<LastName>|<\/LastName>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -v FS='(<FirstName>|<\/FirstName>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -v FS='(<Id>|<\/Id>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -v FS='(<Dob>|<\/Dob>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -v FS='(<TestDate>|<\/TestDate>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -F '<Value>|<\/Value>' '{for (i=2; i<=NF; i+=2) print $i}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
"${NEWXML}/${file}" | tr '\n' ',' > "${NEWXML}/1${file}"
awk "BEGIN{RS=ORS=,} {sub(/..:..:..$/,"")} !seen[$0]++" "${NEWXML}/1${file}" > "${NEWXML}/2${file}"
done
谢谢大家的帮助,shellcheck帮助我知道我的脚本没有错,然后其他人都说该目录不存在(尽管我知道它确实存在)帮助我确定文件夹
有问题答案 0 :(得分:1)
您可能在该目录中没有任何XML文件,因此shell扩展只是${XMLDIR}/*.xml
。您可以尝试使用find
代替:
find ${XMLDIR}/*.xml -exec `basename` | while read file; do
...
...
done
答案 1 :(得分:0)
原来代码是无效的(必须纠正一些引用问题),但是当我试图在bash脚本中使用它时,该文件夹已损坏。这是包含目录变量的正确双引号的工作代码。
#!/bin/bash
#file location
XMLDIR='/home/amoore19/XML/00581-001/scores'
NEWXML='/home/amoore19/XML/00581-001'
#this gives me the filenames without the path
for file in `ls ${XMLDIR}/*.xml | xargs -n1 basename`
do
awk -v FS='(<LastName>|<\/LastName>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -v FS='(<FirstName>|<\/FirstName>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -v FS='(<Id>|<\/Id>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -v FS='(<Dob>|<\/Dob>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -v FS='(<TestDate>|<\/TestDate>)' '{print $2}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
awk -F '<Value>|<\/Value>' '{for (i=2; i<=NF; i+=2) print $i}' "${XMLDIR}/${file}" >> "${NEWXML}/${file}"
"${NEWXML}/${file}" | tr '\n' ',' > "${NEWXML}/1${file}"
awk "BEGIN{RS=ORS=,} {sub(/..:..:..$/,"")} !seen[$0]++" "${NEWXML}/1${file}" > "${NEWXML}/2${file}"
done