linux - Ubuntu bash awk 奇怪的行为

标签 linux bash ubuntu awk

我正在尝试使用以下命令从 yml 文件中删除一个 block ,

awk '$1 == "tool:"{t=1}
   t==1 && $1 == "ports:"{t++; next}
   t==2 && /:[[:blank:]]*$/{t=0}
   t != 2' file.yml

对于下面的yml,

tool:
  image: tool.xxx.com/platform/app:dev
  log_driver: syslog
  restart: always
  ports:
    - "54325:80"
    - "543325:80"
  volume:
    - "a:b"

tool1:
  image: tool1.xxx.com/platform/app:dev
  log_driver: syslog
  restart: always
  ports:
    - "54325:80"
    - "543325:80"
  volume:
    - "a:b"

目标是从 tool 主 block 中删除 ports 子 block 。 这个答案来自https://stackoverflow.com/a/37256824/698072尝试时似乎完全有效here .但它正在从我的 Ubuntu 14.04 中的 ports 直到 yml 文件末尾删除所有内容(在 14.04 的多个系统中尝试了 sh 和 zsh)。

预期:

tool:                                                                                                                                                                                                                     
  image: tool.xxx.com/platform/app:dev                                                                                                                                                                                    
  log_driver: syslog                                                                                                                                                                                                      
  restart: always                                                                                                                                                                                                         
  volume:                                                                                                                                                                                                                 
    - "a:b"                                                                                                                                                                                                               

tool1:                                                                                                                                                                                                                    
  image: tool1.xxx.com/platform/app:dev                                                                                                                                                                                   
  log_driver: syslog                                                                                                                                                                                                      
  restart: always                                                                                                                                                                                                         
  ports:                                                                                                                                                                                                                  
    - "54325:80"                                                                                                                                                                                                          
    - "543325:80"                                                                                                                                                                                                         
  volume:                                                                                                                                                                                                                 
    - "a:b" 

输出:

tool:
  image: tool.xxx.com/platform/app:dev
  log_driver: syslog
  restart: always

对这种特殊情况的任何帮助都会非常好。

最佳答案

我在我之前的 awk 命令中做了一些更改,以处理您的 yml 具有 DOS 行结尾的可能性。

awk '{sub(/\r$/, "")}
     $1 == "tool:"{t=1}
     t==1 && $1 == "ports:"{t++; next}
     t==2 && /:\s*$/{t=0}
     t != 2' file.yml

看起来 Ubuntu awk 不支持 POSIX 类 [[:blank:]][[:space:]] 所以我们需要使用 \s 来匹配空格。

关于linux - Ubuntu bash awk 奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37259603/

相关文章:

linux - git错误: unable to access The requested URL

bash - 从名称取自另一个变量的变量中获取内容

c++ - 从 C++ 执行 bash 脚本并读取其输出 ubuntu sdk

linux - 在 Linux 上弹出 USB 设备

linux - vim过滤命令中断时执行命令

arrays - 在 for 循环中设置命令

linux - 如何在调用 shell 执行一串命令时运行 pkill?

macos - 安装后找不到 Gulp 命令

symfony - Composer 太老了

linux - 从 Haskell 检查 root 权限