2014-01-28 152 views
1

我有這個格式的文件數據低於替換 n「用」,

"253539","","Company1 Name 
","0","1" 
"255229","","Ramu ","0","1" 
"253548","","Mater Name/Id 
","0","1" 
"255229","","Ram Lakh","0","1" 
"253619","","CHild Name/Ids 
","0","1" 

給出的每一行已經\ n追加到它。

我如何可以替換\ n「到」,在shell腳本得到的輸出作爲

"253539","","Company1 Name/Id ","0","1" 
"255229","","Ramu ","0","1" 
"253548","","Mater Name/Id ","0","1" 
"255229","","Ram Lakh","0","1" 
"253619","","CHild Name/Id ","0","1" 

需要自動化的過程,以便在shell腳本編寫。

請幫忙。

回答

0

隨着awk你可以這樣做:

$ awk '/"$/ {print a$0; a=""; next} {a=$0}' file 
"253539","","Company1 Name ","0","1" 
"255229","","Ramu ","0","1" 
"253548","","Mater Name/Id ","0","1" 
"255229","","Ram Lakh","0","1" 
"253619","","CHild Name/Ids ","0","1" 

它的作用是在a存儲的情況下,它不會與"結束線。如果存在,它將打印存儲的行和當前行。

1

不知道有關在外殼很容易這樣做,但我做了一個扔掉的Perl腳本來做到這一點:

perl -e 'foreach (<>) { chop; print; print "\n" if /"$/ }' < mydata.csv 

它通過輸出只有在訂單中"結尾的換行符。

0

可以做類似

#!/bin/bash 

while read -r line; do 
[[ $line =~ '"'$ ]] && echo "$line" || echo -n "$line" 
done < file 

例如您的輸入文件

> ./abovescript 
"253539","","Company1 Name","0","1" 
"255229","","Ramu ","0","1" 
"253548","","Mater Name/Id","0","1" 
"255229","","Ram Lakh","0","1" 
"253619","","CHild Name/Ids","0","1" 
0

如果@ fedorqui的答案可以被接受,那麼這裏是更短的代碼。

awk '{printf $0 (/"$/?RS:X)}' file