sed 命令创建随机命名的文件 [英] sed command creates randomly named files

查看:67
本文介绍了sed 命令创建随机命名的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近编写了一个执行 sed 命令的脚本,将名为test.txt"的文件中所有出现的string1"替换为string2".

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".

看起来像这样:

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

问题是,test.txt 中不一定存在string1".

The catch is, "string1" does not necessarily exist in test.txt.

我注意到在执行了一堆这些 sed 命令后,我得到了许多空文件,留在目录中,名称如下所示:

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"

"sed4l4DpD"

有谁知道为什么会这样,我该如何纠正?

Does anyone know why this might be, and how I can correct it?

推荐答案

所以经过昨晚的大量测试,结果是 sed 在尝试对空字符串进行操作时创建了这些文件.我获取$string1"参数数组的方式是通过一个似乎格式错误的 grep 命令.我从 grep 中想要的是所有包含Text here '.'"类型的行.

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 '.'".

例如,文件中的字符串Text here 'ABC.DEF'"应该已经被grep捕获,然后是ABC.DEF部分字符串,将替换为 ABC_DEF.不幸的是,我使用的 grep 会捕获Text here ''"类型的行(即,'' 之间没有任何内容).后来,脚本尝试使用这个空字符串执行 sed 替换,随机文件被创建(可能是因为 sed 死了).

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).

感谢您帮助理解 sed 的工作原理.

Thanks for all your help in understanding how sed works.

这篇关于sed 命令创建随机命名的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆