简体   繁体   中英

Awk number comparsion in Bash

I'm trying to print records from line number from 10 to 15 from input file number-src. 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 'NR>=10 && NR<=15' number_src

Get the book Effective Awk Programming, by Arnold Robbins.

Two issues why your script is not working:

  • logical AND should be && .
  • use count as variable name, when referencing it, not $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.

For further information, please see the relevant documentation entries about boolean expressions and using variables .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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