linux - UNIX sed 命令不删除 cat 输出中的空格

标签 linux shell unix sed awk

我有一个文件如下。

Hi this is first line
    this is second line
    this is third line

预期是:

Hi this is first line
this is second line
this is third line

我用的是

cat file.txt | sed 's/ //g'

返回,

Hi this is first line
    this is second line
    this is third line

最佳答案

对于可移植的 sed 命令,使用这个:

sed 's/^[[:blank:]]*//' file

[[:blank:]] 匹配空格或制表符。

编辑:使用 awk 删除所有空格:

awk '{$1=$1}1' OFS= file

或 sed:

sed 's/[[:blank:]]*//g' file

关于linux - UNIX sed 命令不删除 cat 输出中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22204150/

相关文章:

linux - Linux Shell脚本中for循环的语法

linux - 如何强制linux在高(64位)地址空间中分配内存

linux - Gfortran 警告提示 "Wmaybe-uninitialized"

bash - 计算标注数字的大小总和?

shell - 适用于 Windows 和 Linux 的通用复制命令

linux - 如何在 shell 中按名称选择多个文件

具有 cp 命令功能的 C 程序

macos - 如何比较 (os x) bash 脚本中的 2 个(图像)文件,忽略最后更改/修改的日期 header ?

linux - 检查文件是否存在 cron 作业

c - Unix Fork 奇怪的行为