如何在所有文本文件中删除/添加空格? [英] How to remove/add spaces in all textfiles?

查看:52
本文介绍了如何在所有文本文件中删除/添加空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个看起来像这样的文件,例如test.in:

I have several files that look like these, e.g. test.in:

apple foo bar
hello world

我需要实现这个期望的输出,每个字符后一个空格:

I need to achieve this desired output, a space after every character:

a p p l e f o o b a r
h e l l o w o r l d

我可能会先删除所有空格,然后为每个字符添加空格,例如:

I though possibly i'll first remove all spaces and then add spaces to each character, as such:

sed 's/\s//g' test.in | sed -e 's/\(.\)/\1 /g'

但是还有其他方法吗?

推荐答案

您可以将两个 sed 命令合并为一个命令:

You can combine your two sed commands into a single command instead:

$ sed 's/\s//g;s/./& /g' test.in 
a p p l e f o o b a r 
h e l l o w o r l d 

注意使用 .& 而不是 \(.\)\1.

Note the use of . and & instead of \(.\) and \1.

在不支持 \s 指定匹配空格的系统上,您可以使用 [[::blank::]] 代替:

On systems that do not support \s to designate matching whitespace, you can use [[::blank::]] instead:

$ sed 's/[[:blank:]]//g;s/./& /g' test.in 
a p p l e f o o b a r 
h e l l o w o r l d 

这篇关于如何在所有文本文件中删除/添加空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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