如何使用sed用\(space)替换空格? [英] How to replace space with \(space) using sed?

查看:56
本文介绍了如何使用sed用\(space)替换空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用sed将所有空格替换为X时,该命令有效,该命令为:

When I use sed to replace all the spaces with X, the command works, the command being:

sed 's/ /X/g' filelist.tmp

但是,当我尝试用\ space替换所有出现的space时,代码为:

However, when I try the same to replace all occurrences of space with \space, the code being :

sed 's/ /\ /g' filelist.tmp

它不起作用.我究竟做错了什么?请注意,我是Shell脚本的新手.

It doesn't work. What am I doing wrong? Note that I'm new to shell scripting.

推荐答案

添加另一个 \ ,即您需要输入 \ 文字:

Add another \ i.e. you need to make \ literal:

sed 's/ /\\ /g'

在空格前只有一个 \ \ 转义了以下空格;由于空格不是替换中的特殊字符,需要转义,因此按原样使用.

With only a single \ before space, the \ is escaping the following space; as the space is not a special character in replacement that needs escaping, it is being taken as is.

示例:

% sed 's/ /\\ /g' <<<'foo  bar  spam'
foo\ \ bar\ \ spam

这篇关于如何使用sed用\(space)替换空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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