简体   繁体   English

sed操作仅适用于较小的文件

[英]Sed operations only works with smaller files

OS: Ubuntu 14.04 操作系统:Ubuntu 14.04

I have 12 large json files (2-4 gb each) that I want to perform different operations on. 我有12个大型json文件(每个2-4 GB),我想对其执行不同的操作。 I want to remove the first line, find "}," and replace it with "}" and remove all "]". 我要删除第一行,找到“}”,然后将其替换为“}”,然后删除所有“]”。

I am using sed to do the operations and my command is: 我正在使用sed进行操作,我的命令是:

sed -i.bak -e '1d' -e 's/},/}/g' -e '/]/d' file.json

When i run the command on a small file (12,7kb) it works fine. 当我在一个小文件(12.7kb)上运行命令时,它可以正常工作。 file.json contains the content with the changes and file.json.bak contains the original content. file.json包含具有更改的内容,而file.json.bak包含原始内容。

But when i run the command on my larger files the original file is emptied, eg file.json is empty and file.json.bak contains the original content. 但是,当我在较大的文件上运行命令时,原始文件将被清空,例如file.json为空,而file.json.bak包含原始内容。 The run time is also what I consider to be "to fast", about 2-3 seconds. 运行时间也是我认为“快”的时间,大约2-3秒。

What am I doing wrong here? 我在这里做错了什么?

Are you sure your input file contains newlines as recognized by the platform you are running your commands on? 您确定输入文件包含换行符,而换行符被您在其上运行命令的平台所识别? If it doesn't then deleting one line would delete the whole file. 如果没有,则删除一行将删除整个文件。 What does wc -l < file tell you? wc -l < file告诉您什么?

If it's not that then you probably don't have enough file space to duplicate the file so sed is doing something internally like 如果不是这样,那么您可能没有足够的文件空间来复制文件,因此sed在内部进行了类似的操作

mv file backup && sed '...' backup > file

but doesn't have space to create the new file after moving the original to backup. 但在将原始文件移至备份后没有空间来创建新文件。 Check your available file space and if you don't have enough and can't get more then you'll need to do something like: 检查可用的文件空间,如果您没有足够的空间并且无法获得更多空间,则需要执行以下操作:

while [ -s oldfile ]
do
    copy first N bytes of oldfile into tmpfile &&
    remove first N bytes from oldfile using real inplace editing &&
    sed 'script' tmpfile >> newfile &&
    rm -f tmpfile
done
mv newfile oldfile

See https://stackoverflow.com/a/17331179/1745001 for how to remove the first N bytes inplace from a file. 有关如何从文件中删除前N个字节的信息,请参见https://stackoverflow.com/a/17331179/1745001 Pick the largest value for N that does fit in your available space. 选择适合您的可用空间的N的最大值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM