0

About the awk command to show a range of specific lines the following tutorial is valuable

See the Another use of NR built-in variables (Display Line From 3 to 6) section

Therefore if a range of lines is desired the following code works fine

awk 'NR==1, NR==7 {print NR,$0}' somefile.txt

Until here it is ok, now consider the following scenario, it works too

awk 'NR==7, NR==7 {print NR,$0}' somefile.txt

Yes, NR is the same as 7. It only shows the line 7

The goal is through a shell script show the current line, it line by line.

To accomplish this goal, the following attempt code was created but it does not work

CURRENT_LINE=7
echo "CURRENT_LINE: $CURRENT_LINE"
awk 'NR==$CURRENT_LINE, NR==$CURRENT_LINE {print NR,$0}' somefile.txt

In this case awk shows nothing.

Even with (see that ' was replaced by ")

awk "NR==$CURRENT_LINE, NR==$CURRENT_LINE {print NR,$0}" somefile.txt

fails with:

CURRENT_LINE: 7
awk: cmd. line:1: NR==7, NR==7 {print NR,./Dictionary.sh}
awk: cmd. line:1:                        ^ syntax error
awk: cmd. line:1: NR==7, NR==7 {print NR,./Dictionary.sh}
awk: cmd. line:1:                          ^ unterminated regexp
awk: cmd. line:1: NR==7, NR==7 {print NR,./Dictionary.sh}
awk: cmd. line:1:                                               ^ unexpected newline or end of string

Therefore is mandatory use '' and not ""

Therefore What is missing? and How to accomplish this goal?

Manuel Jordan
  • 1,414
  • 9
  • 33
  • In addition to the duplicate answer shown, you can use both types of quote to expand the shell $values but not misinterpret the awk $0 syntax. `awk "NR==$CURRENT_LINE, NR==$CURRENT_LINE "'{print NR,$0}' somefile.txt` – Paul_Pedant Feb 19 '23 at 23:02

0 Answers0