sed 命令创建随机命名的文件

发布于 2024-12-09 11:53:15 字数 295 浏览 0 评论 0原文

我最近编写了一个执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆梦 2024-12-16 11:53:16

-i 是给新/输出文件的后缀。此外,该命令还需要 -e
使用方法如下:

sed -i '2' -e 's/string1/string2/g' test.txt

这将创建一个名为 test.txt2 的文件,它是 test.txt 的备份

替换该文件(而不是创建新副本 - 称为“就地”替换),将 -i 值更改为 '' (即空白):

sed -i '' -e 's/string1/string2/g' test.txt

EDIT II

这是实际的命令行输出从 Mac (Snow Leopard) 上可以看出我修改后的答案(删除了 -i 和后缀之间的空格)是正确的。
注意:在Linux服务器上,-i和后缀之间不能有空格。

> echo "this is a test" > test.txt
> cat test.txt
this is a test
> sed -i '2' -e 's/a/a good/' test.txt 
> ls test*
test.txt    test.txt2
> cat test.txt
this is a good test
> cat test.txt2
this is a test
> sed -i '' -e 's/a/a really/' test.txt 
> ls test*
test.txt    test.txt2
> cat test.txt
this is a really good test

-i is the suffix given to the new/output file. Also, you need -e for the command.
Here's how you use it:

sed -i '2' -e 's/string1/string2/g' test.txt

This will create a file called test.txt2 that is the backup of test.txt

To replace the file (instead of creating a new copy - called an "in-place" substitution), change the -i value to '' (ie blank):

sed -i '' -e 's/string1/string2/g' test.txt

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.

> echo "this is a test" > test.txt
> cat test.txt
this is a test
> sed -i '2' -e 's/a/a good/' test.txt 
> ls test*
test.txt    test.txt2
> cat test.txt
this is a good test
> cat test.txt2
this is a test
> sed -i '' -e 's/a/a really/' test.txt 
> ls test*
test.txt    test.txt2
> cat test.txt
this is a really good test
昵称有卵用 2024-12-16 11:53:16

我无法通过快速测试(使用 GNU sed 4.2.1)重现这一点 - 但 strace 确实显示 sed 创建了一个名为 sedJd9Cuy 的文件,然后将其重命名为 tmp code>(在命令行上命名的文件)。

在 sed 创建临时文件之后和能够重命名它之前,看起来好像出了问题。

我最好的猜测是您的文件系统空间不足;您可以创建一个新的空文件,但无法写入它。

df . 说什么?

编辑

我仍然不知道是什么导致了这个问题,但解决它应该不会太困难。

而不是

sed -i 's/string1/string2/g' test.txt

尝试这样的事情:

sed 's/string1/string2/g' test.txt > test.txt.$ && mv -f test.txt.$ test.txt

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 to tmp (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

sed -i 's/string1/string2/g' test.txt

try something like this:

sed 's/string1/string2/g' test.txt > test.txt.$ && mv -f test.txt.$ test.txt

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.

因为看清所以看轻 2024-12-16 11:53:16

因此,经过昨晚的大量测试,事实证明 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 the ABC.DEF portion of the string, would be substituted by ABC_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.

陈独秀 2024-12-16 11:53:16

如果你这样做的话会更好:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文