1和{print}之间有什么明显的区别? [英] awk difference between 1 and {print}?

查看:49
本文介绍了1和{print}之间有什么明显的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对每个文件进行一次更改的评论使我觉得 1 {print} 的awk相等.但事实并非如此.

  awk'/^ \ S/{core = 0}/^ _ core/{core = 1}!core 1'views.view.who_s_online.yml | headuuid:50715f68-3b13-4a15-8455-853110fd1d8blangcode:zh状态:真实依赖项:模块:-用户_核:default_config_hash:DWLCIpl8ku4NbiI9t3GgDeuW13KSOy2l1zho7ReP_Bgid:who_s_online标签:谁的在线屏蔽" 

比较(这就是我想要的):

  awk'/^ \ S/{core = 0}/^ _ core/{core = 1}!core {print}'views.view.who_s_online.yml | headuuid:50715f68-3b13-4a15-8455-853110fd1d8blangcode:zh状态:真实依赖项:模块:-用户id:who_s_online标签:谁的在线屏蔽"模块:用户描述:显示最近活动用户的用户名,以及活动用户总数." 

解决方案

awk程序的结构是一系列条件和操作:

 条件{操作} 

condition 的默认值为 1 (true),因此总是发生没有条件的动作:

  {action}#等效于1 {动作} 

默认操作是 print ,因此通常您会在awk脚本中看到 1 而不是 {print} .

但是,在您的脚本中,您的条件是!core 1 .这将使 core 的值取反,将其强制为字符串,并与字符串"1" 连接.非空字符串始终为true,因此将打印每条记录.

如果只想打印 core 为false的记录,则可以单独使用!core 作为条件.

This comment on awk change once per file made me think 1 and {print} are equal in awk. But it is not.

awk '/^\S/ {core=0} /^_core/ {core=1} !core 1' views.view.who_s_online.yml|head
uuid: 50715f68-3b13-4a15-8455-853110fd1d8b
langcode: en
status: true
dependencies:
  module:
    - user
_core:
  default_config_hash: DWLCIpl8ku4NbiI9t3GgDeuW13KSOy2l1zho7ReP_Bg
id: who_s_online
label: 'Who''s online block'

Compare to (and this is what I wanted btw):

awk '/^\S/ {core=0} /^_core/ {core=1} !core {print}' views.view.who_s_online.yml|head
uuid: 50715f68-3b13-4a15-8455-853110fd1d8b
langcode: en
status: true
dependencies:
  module:
    - user
id: who_s_online
label: 'Who''s online block'
module: user
description: 'Shows the user names of the most recently active users, and the total number of active users.'

解决方案

The structure of an awk program is a series of conditions and actions:

condition { action }

The default value of condition is 1 (true), so actions without a condition always happen:

{ action } # is equivalent to
1 { action }

The default action is print, so quite often you will see a 1 in an awk script instead of { print }.

However, in your script, your condition is !core 1. This will negate the value of core, coerce it to a string and concatenate with the string "1". A non-empty string is always true, so every record will be printed.

If you want to only print records where core is false, then you can use !core as a condition by itself.

这篇关于1和{print}之间有什么明显的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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