2014-10-02 76 views
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]] 

,這與腳本的其餘部分擰緊。
我不知道我在做什麼錯,並可以使用指針

+0

那麼你的腳本嗎?你的最終目標?我覺得awk是處理csv的更好方法。 – Kent 2014-10-02 21:30:43

回答

2

問題是您的csv文件具有DOS行尾。

我複製並粘貼您的數據和腳本,它產生的輸出:

Category,Feature,Description,app1,app2,app3,app4,app5,app6,app7 
[[Category]] 
[[Feature]] 
[[Description]] 
[[app1]] 
[[app2]] 
[[app3]] 
[[app4]] 
[[app5]] 
[[app6]] 
[[app7]] 

我然後轉換CSV文件到DOS行結束,並得到你看到了同樣的問題。

解決方案是使用dos2unix或類似的實用程序來修復文件,然後再讀取它。這是可以做到如下:

title=$(dos2unix <"$1" | head -n 1) # grab the first row 
echo $title 
IFS=',' read -a titline <<<"$title" # make it an array 
for ((i = 0 ; i < ${#titline[@]} ; i++)) do 
    echo "[[${titline[$i]}]]" 
done 

或者,你可以使用sed

title=$(head -n 1 "$1" | sed 's/\r//') # grab the first row 
echo $title 
IFS=',' read -a titline <<<"$title" # make it an array 
for ((i = 0 ; i < ${#titline[@]} ; i++)) do 
    echo "[[${titline[$i]}]]" 
done