while read -r line
will run through each line in a file. How can I have it run through specific lines in a file, for example, lines "1-20", then "30-100"?
Asked
Active
Viewed 2,891 times
2 Answers
5
One option would be to use sed
to get the desired lines:
while read -r line; do
echo "$line"
done < <(sed -n '1,20p; 30,100p' inputfile)
Saying so would feed lines 1-20, 30-100 from the inputfile
to read
.

devnull
- 118,548
- 33
- 236
- 227
3
@devnull's sed command does the job. Another alternative is using awk since it avoids doing the read and you can do the processing in awk itself:
awk '(NR>=1 && NR<=20) || (NR>=30 && NR<=100) {print "processing $0"}' file

anubhava
- 761,203
- 64
- 569
- 643
-
what is the awk syntax for a) printing specific lines (e.g.: lines 43 and 51) or lines provided in some sort of list (e.g.: line numbers from [3,77,93,22]? – drevicko Apr 14 '15 at 01:38