使用 sed 删除 bash 中的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29760638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:56:46  来源:igfitidea点击:

remove white space in bash using sed

bashunixsedwhitespaceuniq

提问by Sal

I have a file that contains a number followed by a file path on each line for a large amount of files. So it looks like this:

我有一个文件,其中包含一个数字,每行都有一个文件路径,用于大量文件。所以它看起来像这样:

      7653 /home/usr123/file123456

But the problem with this is there is 6 empty white spaces before it which throws off the rest of my script. I have included the line which produces it below:

但问题是在它之前有 6 个空白空格,这会影响我的脚本的其余部分。我在下面包含了产生它的行:

cat temp | uniq -c | sed 's/  */ /g' > temp2

I have narrowed it down to being the uniqcommand that produces the unnecessary white space. I have tried to implement a sedcommand to delete the white space but for some reason it deletes all but one. How would I be able to modify my sedstatement or my uniqstatement to get rid of these white spaces? Any help would be greatly appreciated!

我已将其缩小为uniq产生不必要的空白的命令。我试图实现一个sed删除空白的命令,但由于某种原因它删除了除一个之外的所有内容。我怎样才能修改我的sed陈述或我的uniq陈述以摆脱这些空格?任何帮助将不胜感激!

回答by John1024

To remove all the leading blanks, use:

要删除所有前导空格,请使用:

sed 's/^ *//g'

To remove all leading white space of any kind:

要删除任何类型的所有前导空格:

sed 's/^[[:space:]]*//g'

The ^symbol matches only at the beginning of a line. Because of this, the above will leave alone any whitespace not at the beginning of the line.

^符号仅在行首匹配。正因为如此,上面的内容不会在行的开头留下任何空格。

回答by eewallace

Breaking down your current sed pattern 's/ */ /g': The search pattern, ' *', matches a single space, followed by zero or more spaces. The replacement pattern, ' ', is a single space. The result is to find any set of one or more spaces and replace it by a single space, hence removing all but one of the spaces. To make it remove all, you can just make your replacement pattern the empty string, '': sed 's/ *//g'

分解您当前的 sed 模式“s/ */ /g”:搜索模式“*”匹配单个空格,后跟零个或多个空格。替换模式“ ”是一个空格。结果是找到任何一组一个或多个空格并将其替换为单个空格,从而删除除一个空格之外的所有空格。要删除所有内容,您可以将替换模式设为空字符串, '': sed 's/ *//g'

Note, however, that the 'g' makes it global, so that will also remove the space between the number and the file path. If you just want to remove the leading space, you can just drop the g. You can also use the + quantifier, which means "at least one", instead of the *, which means "at least zero". That would leave you with: sed 's/ \+/'

但是请注意,'g' 使它成为全局的,因此这也将删除数字和文件路径之间的空格。如果您只想删除前导空格,则可以删除 g。您还可以使用 + 量词,意思是“至少一个”,而不是 *,意思是“至少零”。那会给你留下: sed 's/ \+/'

ETA: As John1024 points out, you can also use the ^ anchor to specify the beginning of the line, and the \s wildcard to specify any whitespace characters instead of just spaces: sed 's/\s\+//'

ETA:正如 John1024 指出的那样,您还可以使用 ^ 锚点来指定行的开头,并使用 \s 通配符来指定任何空白字符而不仅仅是空格:sed 's/\s\+//'