2010-12-02 98 views
0

我有約5.5GB大小的文件。我想查看文件的特定行。讓我們說行號100001,我想用我自己的文本替換該行。如何使用Unix命令來實現這個操作。我無法在編輯器中查看該文件。我無法打開,那是一臺遠程機器。UNIX替換文件中的特定行

任何人都可以分享一些想法,查看該行,並用其他文本替換它?

謝謝:)

+0

如果它不能被打開,你就沒有希望,除非我誤解了你的問題。 – Actorclavilis 2010-12-02 01:58:49

回答

1

如果要修改就地行和替換數據被替換的相同長度的文本,你可以使用dd到(小心!)覆蓋該文件的一部分。

# getting the byte offsets of the start and length of the line 
perl -ne '$s+=length; if ($.==100001) {print "$s + ",length,"\n"; exit}' bigfile 

# writing over the existing data 
echo 'new line' | dd of=bigfile bs=1 seek=$start count=$length conv=notrunc 

如果替換數據是不同的長度,並且它不在文件的最後,則別無選擇,只能重寫該文件。這需要有足夠的磁盤空間來保留bigfile及其副本!

# The old file is renamed to bigfile.bak; a new bigfile is written with changes. 
sed -i.bak -e '100001 c \ 
new line' bigfile