简体   繁体   English

Bash中的Awk数字比较

[英]Awk number comparsion in Bash

I'm trying to print records from line number from 10 to 15 from input file number-src. 我正在尝试从输入文件号-src从10到15的行号打印记录。 I tried with below code but it prints all records irrespective of line number. 我尝试使用下面的代码,但无论行号如何,它都会打印所有记录。

awk '{
count++
if ( $count >= 10 ) AND ( $count <= 15 )
 {
  printf "\n"  $0
 }
}' number_src

awk is not bash just like C is not bash, they are separate tools/languages with their very own syntax and semantics: awk不是bash,就像C不是bash一样,它们是单独的工具/语言,具有自己的语法和语义:

awk 'NR>=10 && NR<=15' number_src

Get the book Effective Awk Programming, by Arnold Robbins. 获得Arnold Robbins的《有效的Awk编程》一书。

Two issues why your script is not working: 为什么脚本无法正常工作的两个问题:

  • logical AND should be && . 逻辑AND应为&&
  • use count as variable name, when referencing it, not $count . 在引用它时,请使用count作为变量名,而不是$count

Here is a working version: 这是一个工作版本:

awk '{
     count++
     if ( count >= 10  &&  count <= 15 )
     {
          print $0
     }
     }' numbers_src

As stated in the quickest answer, for your task NR is the awk -way to do the same task. 如最快答案中所述,对于您的任务, NR是执行相同任务的awk

For further information, please see the relevant documentation entries about boolean expressions and using variables . 有关更多信息,请参见有关布尔表达式使用变量的相关文档条目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM