1

I have a bash script which calls a awk script to print each line from the file name_list.txt

#!/bin/bash

awk -f parse_list.awk name_list.txt

In the awk script, I am using getline with NR to print each record as below:

#!/usr/bin/awk

BEGIN {
  line = ""
}

NR != 0 {
  print NR
  getline line
  print line
}

Suppose name_list.txt is as shown below:

aaaaaaaaaaa   
bbbbbbbbbb  
cccccccccc 
ddddddddddd
eeeeeeeee
ffffffffff
gggggggg

When I execute this script, I was expecting the content of name_list.txt along with the line number to be printed on console, but when I execute it, getline and/or print skips one line, so the output is always.

1
bbbbbbbbbb  
3
ddddddddddd
5
ffffffffff
7
ffffffffff

Can anyone please tell me if I am using NR and getline properly. I need both the shell script and awk script, I don't want to combine them. Can anyone please help ?

2 Answers 2

3

getline reads in the next line. You don't need to use it. The current line is already in $0:

awk '{print NR, $0}' name_list.txt 
0

getline gets the next line of input. From the man page --

   getline var           Set var from next input record; set NR, FNR.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .