sed 命令创建随机命名的文件
我最近编写了一个执行 sed 命令的脚本,以将名为“test.txt”的文件中所有出现的“string1”替换为“string2”。
它看起来像这样:
sed -i 's/string1/string2/g' test.txt
问题是,“string1”不一定存在于 test.txt 中。
我注意到执行一堆这些 sed 命令后,我得到了许多空文件,留在目录中,其名称如下所示:
“sed4l4DpD”
有谁知道为什么会这样,以及如何纠正它?
I recently wrote a script that does a sed command, to replace all the occurrences of "string1" with "string2" in a file named "test.txt".
It looks like this:
sed -i 's/string1/string2/g' test.txt
The catch is, "string1" does not necessarily exist in test.txt.
I notice after executing a bunch of these sed commands, I get a number of empty files, left behind in the directory, with names that look like this:
"sed4l4DpD"
Does anyone know why this might be, and how I can correct it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
-i
是给新/输出文件的后缀。此外,该命令还需要-e
。使用方法如下:
这将创建一个名为
test.txt2
的文件,它是test.txt
的备份替换该文件(而不是创建新副本 - 称为“就地”替换),将
-i
值更改为''
(即空白):EDIT II
这是实际的命令行输出从 Mac (Snow Leopard) 上可以看出我修改后的答案(删除了 -i 和后缀之间的空格)是正确的。
注意:在Linux服务器上,
-i
和后缀之间不能有空格。-i
is the suffix given to the new/output file. Also, you need-e
for the command.Here's how you use it:
This will create a file called
test.txt2
that is the backup oftest.txt
To replace the file (instead of creating a new copy - called an "in-place" substitution), change the
-i
value to''
(ie blank):EDIT II
Here's actual command line output from a Mac (Snow Leopard) that show that my modified answer (removed space from between the -i and the suffix) is correct.
NOTE: On a linux server, there must be no space between it
-i
and the suffix.我无法通过快速测试(使用 GNU sed 4.2.1)重现这一点 - 但 strace 确实显示 sed 创建了一个名为 sedJd9Cuy 的文件,然后将其重命名为 tmp code>(在命令行上命名的文件)。
在 sed 创建临时文件之后和能够重命名它之前,看起来好像出了问题。
我最好的猜测是您的文件系统空间不足;您可以创建一个新的空文件,但无法写入它。
df .
说什么?编辑:
我仍然不知道是什么导致了这个问题,但解决它应该不会太困难。
而不是
尝试这样的事情:
sed 创建然后重命名文本文件以替换原始文件的方式出了问题。上面的命令使用 sed 作为简单的输入输出过滤器,并单独创建和重命名临时文件。
I wasn't able to reproduce this with a quick test (using GNU sed 4.2.1) -- but strace did show sed creating a file called
sedJd9Cuy
and then renaming it totmp
(the file named on the command line).It looks like something is going wrong after sed creates the temporary file and before it's able to rename it.
My best guess is that you've run out of room in the filesystem; you're able to create a new empty file, but unable to write to it.
What does
df .
say?EDIT:
I still don't know what's causing the problem, but it shouldn't be too difficult to work around it.
Rather than
try something like this:
Something is going wrong with the way sed creates and then renames a text file to replace your original file. The above command uses sed as a simple input-output filter and creates and renames the temporary file separately.
因此,经过昨晚的大量测试,事实证明 sed 在尝试操作空字符串时正在创建这些文件。我获取“$string1”参数数组的方式是通过 grep 命令,该命令似乎格式错误。我想要从 grep 中得到的是包含“此处文本'.'”类型的所有行。
例如,文件中的字符串“
Text here 'ABC.DEF'
”应该被 grep 捕获,然后字符串的ABC.DEF
部分将被捕获。替换为ABC_DEF
。不幸的是,我使用的 grep 会捕获“Text here ''
”类型的行(即,'' 之间没有任何内容)。当稍后,脚本尝试使用此空字符串执行 sed 替换时,会创建随机文件(可能是因为 sed 已终止)。感谢您为理解 sed 工作原理提供的所有帮助。
So after much testing last night, it turns out that sed was creating these files when trying to operate on an empty string. The way i was getting the array of "$string1" arguments was through a grep command, which seems to be malformed. What I wanted from the grep was all lines containing something of the type "Text here '.'".
For example the string, "
Text here 'ABC.DEF'
" in a file, should have been caught by grep, then theABC.DEF
portion of the string, would be substituted byABC_DEF
. Unfortunately the grep I was using would catch lines of the type "Text here ''
" (that is, nothing between the ''). When later on, the script attempted to perform a sed replacement using this empty string, the random file was created (probably because sed died).Thanks for all your help in understanding how sed works.
如果你这样做的话会更好:
cat large_file | sed 's/string1/string2/g' > file_filterred
Its better if you do it in this way:
cat large_file | sed 's/string1/string2/g' > file_filtred