1
我從從excel中導出的.csv文件中提取數據。從bash中的字符串修剪新行字符
CSV文件看起來是這樣的:
Category,Feature,Description,app1,app2,app3,app4,app5,app6,app7
Employee Relations,Employee Management,,y,y,n,y,y,y,n
Employee Relations,Employee Availability,,y,y,n,n,y,y,n
問題是結束字符換行的方式獲得。採取下面的代碼:
title=$(head -n 1 $1) # grab the first row
title=`echo $title | sed -e 's/^ *//' -e 's/ *$//'` # me trying to fix. I think this is the issue
echo $title #this seems to look right
IFS=',' read -a titline <<<"$title" # make it an array
for ((i = 0 ; i < ${#titline[@]} ; i++)) do
echo "[[${titline[$i]}]]"
done
打印出:
Category,Feature,Description,app1,app2,app3,app4,app5,app6,app7
[[Category]]
[[Feature]]
[[Description]]
[[app1]]
[[app2]]
[[app3]]
[[app4]]
[[app5]]
[[app6]]
]]app7
正如你所看到的最後一行應該是:
[[app7]]
,這與腳本的其餘部分擰緊。
我不知道我在做什麼錯,並可以使用指針
那麼你的腳本嗎?你的最終目標?我覺得awk是處理csv的更好方法。 – Kent 2014-10-02 21:30:43