Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File UNIX替换文件中的特定行_File_Unix_Replace_Command - Fatal编程技术网

File UNIX替换文件中的特定行

File UNIX替换文件中的特定行,file,unix,replace,command,File,Unix,Replace,Command,我有大约5.5GB大小的文件。我想查看文件的特定行。假设行号100001,我想用我自己的文本替换这一行。如何使用Unix命令实现此操作。我无法在编辑器中查看该文件。我无法打开,那是一台远程机器 有人能分享一些想法来查看这一行并用其他文本替换它吗 谢谢:)如果您想在位修改行,并且替换数据的长度与被替换文本的长度相同,您可以使用dd来(小心地!)覆盖部分文件 # getting the byte offsets of the start and length of the line perl -ne

我有大约5.5GB大小的文件。我想查看文件的特定行。假设行号100001,我想用我自己的文本替换这一行。如何使用Unix命令实现此操作。我无法在编辑器中查看该文件。我无法打开,那是一台远程机器

有人能分享一些想法来查看这一行并用其他文本替换它吗


谢谢:)

如果您想在位修改行,并且替换数据的长度与被替换文本的长度相同,您可以使用
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

如果无法打开,你就没有希望,除非我误解了你的问题。如果你知道那一行是什么样子,你可以使用
sed
查找并替换文件的那一行。但是如果文件无法打开,
sed
将没有多大用处。可能因为文件大小而无法打开。另外它是远程的,所以可能只是通过SSH访问它。这个怎么样:sed-e“100001s/your line/new line/”?@Ruel:是的,我只是通过SSH访问@苏吉特:我会听从你的命令,让你认识这个人。谢谢:)