0

I am processing my text file using awk. I have written the code below:

#!/bin/bash
l=1
while [ $l -lt 5 ]
do
echo $l
awk -v L=$l '/^BS[0-5]|^FG[2-7]/ && length<10 {i++}i==L {print}'
l=$(expr $l + 1)
done <input.txt

But, once i run the code I just get first awk output. Would you please let me know how can I fix this code?

2 Answers2

0

You do not read the lines of our file. Do:

while read line && [ $l -lt 5 ]
do
...
RaphaMex
  • 2,781
  • 1
  • 14
  • 30
0

The while loop is taking its input from the file input.txt. awk is inheriting its input from the while loop, and it reads all of the input on the first iteration of the loop. In the 2nd iteration, awk gets no data. If you want to read from that file on each iteration, pass input.txt as an argument to awk.

Better yet, skip the while loop and do the whole thing in awk:

awk  '/^BS[0-5]|^FG[2-7]/ && length<10 && i++ < 5' input.txt
William Pursell
  • 204,365
  • 48
  • 270
  • 300