2014-02-22 40 views
0

我有一些大的文件(大於2GB),並在文件中的文本格式如下:搜索並替換大文件的每行中的第一個分隔符?

2013/4/18;22:5:42.668;13266;10;13279;10 
2013/4/18;22:10:48.820;13271;10;13279;10 
2013/4/18;22:12:0.956;13266;10;13279;10 
2013/4/18;22:12:44.826;13266;10;13284;10 
... 

我想完成以下任務

- replace the 1st semi-colon ";" in each line to space character " " 
- replace the rest semi-colon ";" in each line to comma character "," 

輸出應該像下面那樣

2013/4/18 22:5:42.668,13266,10,13279,10 
2013/4/18 22:10:48.820,13271,10,13279,10 
2013/4/18 22:12:0.956,13266,10,13279,10 
2013/4/18 22:12:44.826,13266,10,13284,10 
... 

任何人都可以請告訴我怎麼做?

+0

你有emacs嗎?如果你有'sed',在emacs – Nullpointer

+0

'sed -e's;;//'-e's /; /,/ g''裏很容易。 –

+0

@JonathanLeffler窗戶有'sed'嗎? – Nullpointer

回答

0

雖然這不是一個正則表達式,但這會做你的工作! 這就是所謂的emacs keyborad宏!

這些是在emacs中打開此文件後需要按的鍵。

爲什麼我要求你定義你的宏,因爲一旦定義你可以隨時用任何文件,只需按一個或兩個鍵。

在Emacs打開文件,並開始按這些鍵:

alt-shift-, //go to the start of the file 
ctrl-x-( //start defining keyboard macro 
ctrl-s  //search for some thing 
;   //that something is your ; 
ctrl-b  //move one step back 
ctrl-d  //delete the ; 
[space-bar] //add space 
ctrl-[space-bar] //start selecting region 
ctrl-e  //select region till the end of line 
alt-shift-5 //replace all from region 
;   //replace ; 
,   //with , 
shift-1  //do this for all ; in region 
ctrl-e  //move to end of line 
ctrl-f  //go to start of next line 
ctrl-x-) //end macro 
//now-your-macro-is-defined 
ctrl-[space-bar] //again define the region 
alt-shift-.  //select all file (except first line) 
M-x-apply-macro-to-region //apply perviously defined macro to complete file! 

希望這有助於!

0

它可以很容易地在gVim for Windows完成。首先,我們用簡單的macro替換每行中的第一個分號。要進入宏,類型:

ggq1(or any letter)^f;r jq 

解釋:

  • gg:轉到文件的開頭
  • q1:開始錄製時,寄存器1或任何
  • ^:去一條線的開始
  • f;:去發生;右邊
  • r:與空間
  • j替換光標下的字符:去下一行
  • q:停止記錄

然後從第二行由:2,$:normal @1類型在命令模式中執行的宏,現在所有線的第一個分號被一個空格替換。之後,使用:%s/;/,/g替換其餘的分號。

0

這裏是一個Perl一個襯墊,沒有工作:

perl -api.back -e 's/;/ /; s/;/,/g;' in.txt 

原始文件是保存in.txt.back下和更換就地完成。

相關問題