2017-06-16 85 views
0

我試圖用vim打開巨大的文件來編輯它的頭部。我只想在腦海中添加一條線。但是打開它是一個問題。 任何編輯頭部的好方法?如何編輯linux shell中巨大的txt文件的頭部?

+1

檢查這有助於https://superuser.com/a/ 121424/728737 – Abhi

+1

同類問題:htt ps://stackoverflow.com/questions/9533679/how-to-insert-a-text-at-the-beginning-of-a-file – Gov

+2

將行放在一個新的文件,並使用shell重定向追加現有的一個也可以追加('>>')。這樣你就不必在編輯器中打開文件了。 – arkascha

回答

1

使用bashsponge

cat <(echo "This is the new line #1") bigfile | sponge bigfile 
+0

-bash:sponge:command not found – yanachen

+0

@yanachen,在我的答案中點擊URL,或者安裝'moreutils'軟件包,(*即*在* Debian/Ubuntu /等等*運行'apt install moretutils'。) – agc

0

既然你不給輸入文件,我會假設輸入爲後面的,並添加測試字符串「head added !!」到每一行的頭部。

$ cat test 
line 1 
line 2 
line 3 

$ awk 'NR==1{$0="tested line 1\n"$0}1' test 
tested line 1 
line 1 
line 2 
line 3 

修改"tested line 1"在命令awk 'NR==1{$0="tested line 1\n"$0}1'到自己的輸入測試

1
sed '1 i\New HEADER' bigfile 
New HEADER 
line 1 
line 2 
line 3 

使用sed -i標誌進行修改的文件中持續。

0

創建頭文件只命名爲在同一目錄下的1.txt和地方文件作爲原始文件說,例如,你的文件名作爲2.txt

使用cat命令爲:

貓的1.txt 2.txt> 3.txt

這將增加從1.txt的標題和內容,從2.txt到3.txt