我正在编写一个脚本来将较大的文件解析为可用数据。我将附上下面的脚本。该脚本没有按照我想要的方式运行,我认为这可能是我使用的 if/then 语句,但是当我将其取消时,它揭示了真正的问题。这就是我现在遇到的错误,这使得真正的问题看起来像是某种引号。
FirstName,LastName,Company,Address,City,County,State,ZIP,Phone,Fax,Email,Web
FinalProject.sh: 36: FinalProject.sh: Syntax error: Unterminated quoted string
虽然我从技术上理解语法错误的含义,但我找不到问题出在哪里,并且我已经尝试修复它大约半个小时。帮助将不胜感激!
#!/bin/bash
OLDIFS=${IFS}
IFS=$'\n'
head -1 50000_a.csv | sed 's/"//g'
for LINE in 'grep WV 50000_a.csv | sed 's/"//g' | awk -F, '{print $(NF-11)"," \
$(NF-10)"," \
$(NF-9)"," \
$(NF-8)"," \
$(NF-7)"," \
$(NF-6)"," \
$(NF-5)"," \
$(NF-4)"," \
$(NF-3)"," \
$(NF-2)"," \
$(NF-1)"," \
$NF \
}''
do
FNAME='echo ${LINE} | awk -F, '{print $1}''
LNAME='echo ${LINE} | awk -F, '{print $2}''
COMPANY='echo ${LINE} | awk -F, '{print $3}''
ADDRESS='echo ${LINE} | awk -F, '{print $4}''
CITY='echo ${LINE} | awk -F, '{print $5}''
COUNTY='echo ${LINE} | awk -F, '{print $6}''
STATE='echo ${LINE} | awk -F, '{print $7}''
ZIP='echo ${LINE} | awk -F, '{print $8}''
PHONE='echo ${LINE} | awk -F, '{print $9}''
FAX='echo ${LINE} | awk -F, '{print $10}''
EMAIL='echo ${LINE} | awk -F, '{print $11}''
WEB='echo ${LINE} | awk -F, '{print $12}''
done
答案1
一般来说,您使用单引号'...'
来包裹“命令执行”,它应该用反引号`...`
或(更好)包裹起来$(...)
。
for LINE in 'grep
这使得以:开头并以以下结尾的 行}''
失败。
进行尽可能少的更改,代码变为:
#!/bin/bash
OLDIFS=${IFS}
IFS=$'\n'
head -1 50000_a.csv | sed 's/"//g'
for LINE in `grep WV 50000_a.csv | sed 's/"//g' | awk -F, '{print $(NF-11) "," \
$(NF-10)"," \
$(NF-9)"," \
$(NF-8)"," \
$(NF-7)"," \
$(NF-6)"," \
$(NF-5)"," \
$(NF-4)"," \
$(NF-3)"," \
$(NF-2)"," \
$(NF-1)"," \
$NF \
}'`
do
FNAME=`echo ${LINE} | awk -F, '{print $1}'`
LNAME=`echo ${LINE} | awk -F, '{print $2}'`
COMPANY=`echo ${LINE} | awk -F, '{print $3}'`
ADDRESS=`echo ${LINE} | awk -F, '{print $4}'`
CITY=`echo ${LINE} | awk -F, '{print $5}'`
COUNTY=`echo ${LINE} | awk -F, '{print $6}'`
STATE=`echo ${LINE} | awk -F, '{print $7}'`
ZIP=`echo ${LINE} | awk -F, '{print $8}'`
PHONE=`echo ${LINE} | awk -F, '{print $9}'`
FAX=`echo ${LINE} | awk -F, '{print $10}'`
EMAIL=`echo ${LINE} | awk -F, '{print $11}'`
WEB=`echo ${LINE} | awk -F, '{print $12}'`
done
然而,尝试这样做是错误的使用 for 循环处理文件, 你应该用于read
获取输出grep
:
#!/bin/bash
OLDIFS=${IFS}
IFS=$'\n'
head -1 50000_a.csv | sed 's/"//g'
while IFS=$'\n' read -r LINE
do
FNAME="$(echo ${LINE} | awk -F, '{print $1}')"
LNAME="$(echo ${LINE} | awk -F, '{print $2}')"
COMPANY="$(echo ${LINE} | awk -F, '{print $3}')"
ADDRESS="$(echo ${LINE} | awk -F, '{print $4}')"
CITY="$(echo ${LINE} | awk -F, '{print $5}')"
COUNTY="$(echo ${LINE} | awk -F, '{print $6}')"
STATE="$(echo ${LINE} | awk -F, '{print $7}')"
ZIP="$(echo ${LINE} | awk -F, '{print $8}')"
PHONE="$(echo ${LINE} | awk -F, '{print $9}')"
FAX="$(echo ${LINE} | awk -F, '{print $10}')"
EMAIL="$(echo ${LINE} | awk -F, '{print $11}')"
WEB="$(echo ${LINE} | awk -F, '{print $12}')"
done < <(
grep WV 50000_a.csv | sed 's/"//g' | awk -F, '{print $(NF-11) "," \
$(NF-10)"," \
$(NF-9)"," \
$(NF-8)"," \
$(NF-7)"," \
$(NF-6)"," \
$(NF-5)"," \
$(NF-4)"," \
$(NF-3)"," \
$(NF-2)"," \
$(NF-1)"," \
$NF \
}'
)