2013-11-22 23 views
2

我不擅長unix。如何在Unix中用~~替換新行和^ M字符

我有一個csv文件,我有多個列。其中,一列包含新行和^M字符。我需要~~全部更換它們兩個之間「(這是一種單細胞值),這樣我可以把單元格的值作爲單場下面是示例文件:

"id","notes" 
"N001","this is^M 
test. 

Again test 

" 
"N002","this is perfect" 
"N00345","this is 

having ^M 
problem" 

我需要這個文件中像:

"id","notes" 
"N001","this is~~test.~~~~Again test~~~~" 
"N002","this is perfect" 
"N00345","this is~~~~having ~~problem" 

使整個單元格值可以理解爲一個單一的字段值

我要在此要求,其中一個單元格中的數據包含"(雙引號)添加更多的情況在這裏。在這種情況下,我們可以識別endin g ",後面跟着逗號。以下是更新的情況下數據:

"id","notes" 
"N001","this is^M 
test. "Again test." 

Again test 

" 
"N002","this is perfect" 
"N00345","this is 

having ^M 
problem as it contains " 
test" 

我們可以保持"或刪除它。預期的輸出是:

"id","notes" 
"N001","this is~~test. "Again test."~~~~Again test~~~~" 
"N002","this is perfect" 
"N00345","this is ~~~~having ~~problem as it contains "~~test" 

回答

3

嘗試使用sed

sed -i -e 's/^M//g' -e '/"$/!{:a N; s/\n/~~/; /"$/b; ba}' file 

注:爲+ 中號使用運行命令

"id","notes" 
"N001","this is~~test.~~~~Again test~~~~" 
"N002","this is perfect" 
"N00345","this is~~~~having ~~problem" 

或者

文件內容輸入^M,類型按Ctrl + V其次Ctrl鍵dos2unix之後sed

dos2unix file 
sed -i '/"$/!{:a N; s/\n/~~/; /"$/b; ba}' file 

簡要說明

這裏的想法是在每行不與"

sed -i '    # -i specifies in-place relace i.e. modifies file itself 
    /"$/!{    # if a line doesn't contain end pattern, " at the end of a line, then do following 
    :a    # label 'a' for branching/looping 
     N;    # append the next line of input into the pattern space 
     s/\n/~~/;  # replace newline character '\n' with '~~' i.e. suppress new lines 
     /"$/b;   # if a line contains end pattern then branch out i.e. break the loop 
     ba    # branch to label 'a' i.e. this will create loop around label 'a' 
    }     
' file    # input file name 

結束參閱man sed進一步的細節

刪除換行符

編輯

有時候本身就包含「在其中的單元格數據。

運行命令更新的情況下,數據

"id","notes" 
"N001","this is~~test. "Again test."~~~~Again test~~~~" 
"N002","this is perfect" 
"N00345","this is~~~~having ~~problem as it contains "~~test" 

使用後使用sed

sed -i ':a N; s/\n/~~/; $s/"~~"/"\n"/g; ba' file 

文件內容perl一個班輪

perl -0777 -i -pe 's/\n/~~/g; s/"~~("|$)/"\n$1/g;' file 
+0

嗨,你的sed命令工作完美我猜。但是它在控制檯評估者上輸出的結果要比保存在同一個文件中。我使用-i選項嗎?請回復。非常感謝。 – user3021141

+0

@ user3021141是的,您需要使用'-i'選項進行就地替換。更新了我的答案。 – jkshah

+1

+1 for dos2unix – Mathew

0

使用tr

$ tr '<Ctrl>+m' '~' 
+0

上,你可以請提供詳細的解決方案。我不擅長unix – user3021141

2

你可以做到這一點使用sed命令

替換 '^ M' 單獨

sed -i 's|^M|~~|g' file_name 

編輯: 感謝給予評論。

添加一條語句來代替 '^ M和新線'

替換 '^ M和新線' **

sed -i ':a;N;$!ba;s|^M\n|~~|g' file_name 

要以控制檯,你應該得到 '^ M'按Cntrl+v+m一起

+1

仍然需要~~作爲報價之間的\ n – NeronLeVelu

0
sed 's/\^M/~~/;t nextline 
b 
: nextline 
N 
s/\n/~~/ 
s/^[^"]*\("[^"]*"\}\{1,\}[^"]*$ 
t 
b nextline 
" 

不只是改變^M而且報價之間的新的生產線。

^M是獲得Unix會話與CTRL + V後跟一個CTRL + M鍵盤