linux_sed&tr_将所有空白符(空格/回车换行符/制表符/..)替换为指定字符/多个连续的空格转为一个空格

linux_sed&tr_将所有空白符(空格/回车换行符/制表符/…)替换为指定字符/多个连续的空格转为一个空格

references

将文本转换为大写

$ echo 'this is a test' | tr a-z A-Z
THIS IS A TEST

测试文本

  • 将下面的文本写入到文件file

    • > file<<eof
      getpid() returns the process ID (PID) of the calling process.
      (This is often used by routines that generate unique temporary
      filenames.)
      getppid() returns the process ID of the parent of the calling
      process. This will be either the ID of the process that created
      this process using fork(), or, if that process has already
      terminated, the ID of the process to which this process has been
      reparented (either init(1) or a "subreaper" process defined via
      the prctl(2) PR_SET_CHILD_SUBREAPER operation).
      eof
  • ​ 使用cat -A检查空白字符的分布($,^)

    • └─[0] <> cat file -A
      getpid() returns the process ID (PID) of the calling process.$
      (This is often used by routines that generate unique temporary$
      filenames.)$
      $
      getppid() returns the process ID of the parent of the calling$
      process. This will be either the ID of the process that created$
      this process using fork(), or, if that process has already$
      terminated, the ID of the process to which this process has been$
      reparented (either init(1) or a "subreaper" process defined via$
      the prctl(2) PR_SET_CHILD_SUBREAPER operation).$
  • 文件2:写入到ff

    • > ff<<eof
      line1 end
      line2 end
      123
      456
      eof
  • 空白符分部情况

    • ┌─[cxxu@CxxuWin11] - [~] - [2022-04-24 02:17:01]
      └─[0] <> cat ff -A
      line1 end$
      line2^Iend$
      123$
      456$

将所有空白字符串(长度>0)的地方替换为一个空格

这种情况经常在复制pdf中的文本/man手册中的文本时需做的处理

sed&tr

本方案综合利用了sed按行处理的专业性(正则表达式的完整支持)和tr(对于多行文本间的字符操作的简单易行的优势)

  • sed -e 's/\s\+/ /g' file|tr "\n" ' '(此处没有使用扩展的正则-E选项,将+视为数量匹配符时需要\转义之)
  • 或者 cat file|sed -E 's/\s+/ /g'|tr '\n' ' '(使用扩展的正则表达式)
┌─[cxxu@CxxuWin11] - [~] - [2022-04-24 02:11:51]
└─[0] <> cat file|sed -E 's/\s+/ /g'|tr '\n' ' '
getpid() returns the process ID (PID) of the calling process. (This is often used by routines that generate unique temporary filenames.) getppid() returns the process ID of the parent of the calling process. This will be either the ID of the process that created this process using fork(), or, if that process has already terminated, the ID of the process to which this process has been reparented (either init(1) or a "subreaper" process defined via the prctl(2) PR_SET_CHILD_SUBREAPER operation).
posted @   xuchaoxin1375  阅读(56)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示