简体   繁体   English

用下划线替换目录中所有文件中的空格

[英]Replace spaces in all files in a directory with underscores

I have found some similar questions here but not this specific one and I do not want to break all my files. 我在这里找到了一些类似的问题,但没有找到这个特定的问题,我也不想破坏我的所有文件。 I have a list of files and I simply need to replace all spaces with underscores. 我有一个文件列表,我只需要用下划线替换所有空格。 I know this is a sed command but I am not sure how to generically apply this to every file. 我知道这是一个sed命令,但是我不确定如何将其通用地应用于每个文件。

I do not want to rename the files, just modify them in place. 我不想重命名文件,只需就地修改它们即可。

Edit: To clarify, just in case it's not clear, I only want to replace whitespace within the files, file names should not be changed. 编辑:为澄清起见,以防万一,我只想替换文件中的空格,不应更改文件名。

find . -type f -exec sed -i -e 's/ /_/g' {} \;

find grabs all items in the directory (and subdirectories) that are files, and passes those filenames as arguments to the sed command using the {} \\; find获取目录(和子目录)中所有文件的所有项目,并使用{} \\;将这些文件名作为参数传递给sed命令{} \\; notation. 符号。 The sed command it appears you already understand. 似乎您已经了解了sed命令。

if you only want to search the current directory, and ignore subdirectories, you can use 如果只想搜索当前目录并忽略子目录,则可以使用

find . -maxdepth 1 -type f -exec sed -i -e 's/ /_/g' {} \;

This is a 2 part problem. 这是一个两部分的问题。 Step 1 is providing the proper sed command, 2 is providing the proper command to replace all files in a given directory. 步骤1提供正确的sed命令,步骤2提供正确的命令来替换给定目录中的所有文件。

Substitution in sed commands follows the form s/ItemToReplace/ItemToReplaceWith/pattern , where s stands for the substitution and pattern stands for how the operation should take place. sed命令中的替换格式为s/ItemToReplace/ItemToReplaceWith/pattern ,其中s表示替换,而pattern表示操作方式。 According to this super user post , in order to match whitespace characters you must use either \\s or [[:space:]] in your sed command. 根据此超级用户帖子 ,为了匹配空格字符,您必须在sed命令中使用\\s[[:space:]] The difference being the later is for POSIX compliance. 后者的区别在于POSIX合规性。 Lastly you need to specify a global operation which is simply /g at the end. 最后,您需要指定一个全局操作,其末尾只是/ g。 This simply replaces all spaces in a file with underscores. 这只是用下划线替换文件中的所有空格。 Substitution in sed commands follows the form s/ItemToReplace/ItemToReplaceWith/pattern , where s stands for the substitution and pattern stands for how the operation should take place. sed命令中的替换格式为s/ItemToReplace/ItemToReplaceWith/pattern ,其中s表示替换,而pattern表示操作方式。 According to this super user post , in order to match whitespace characters you must use either just a space in your sed command, \\s , or [[:space:]] . 根据此超级用户帖子 ,为了匹配空格字符,您必须在sed命令中仅使用空格\\s[[:space:]] The difference being the last 2 are for whitespace catching (tabs and spaces), with the last needed for POSIX compliance. 区别在于最后两个是空格捕获(制表符和空格),最后一个是POSIX合规性所需的。 Lastly you need to specify a global operation which is simply /g at the end. 最后,您需要指定一个全局操作,其末尾只是/ g。

Therefore, your sed command is 因此,您的sed命令是

sed s/ /_/g FileNameHere

However this only accomplishes half of your task. 但是,这只能完成一半的任务。 You also need to be able to do this for every file within a directory. 您还需要能够对目录中的每个文件执行此操作。 Unfortunately, wildcards won't save us in the sed command, as * > * would be ambiguous. 不幸的是,通配符不会将我们保存在sed命令中,因为* > *会模棱两可。 Your only solution is to iterate through each file and overwrite them individually. 您唯一的解决方案是遍历每个文件并分别覆盖它们。 For loops by default should come equipped with file iteration syntax , and when used with wildcards expands out to all files in a directory. 对于for循环,默认情况下应配备文件迭代语法 ,并且与通配符一起使用时,扩展到目录中的所有文件。 However sed's used in this manner appear to completely lose output when redirecting to a file. 但是,以这种方式使用sed时,在重定向到文件时似乎完全失去了输出。 To correct this, you must specify sed with the -i flag so it will edit its files. 要更正此问题,您必须使用-i标志指定sed,以便它将编辑其文件。 Whatever item you pass after the -i flag will be used to create a backup of the old files. -i标志之后传递的所有项目都将用于创建旧文件的备份。 If no extension is passed ( -i '' for instance), no backup will be created. 如果没有传递扩展名(例如-i '' ),则不会创建备份。

Therefore the final command should simply be 因此,最终命令应该只是

for i in *;do sed -i '' 's/ /_/g' $i;done

Which looks for all files in your current directory and echos the sed output to all files (Directories do get listed but no action occurs with them). 它会在当前目录中查找所有文件,并将sed输出回显到所有文件(目录确实会列出,但不会对其执行任何操作)。

好吧……由于我一直想让某件事运行,所以我找到了一种对我有用的方法:

for file in `ls`; do sed -i 's/ /_/g' $file; done

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

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