For Fedora OS

I'm still learning here, but I need help to insert a command for example "XXX" to say that I have a file that contains 1000 lines of single product names. I need to write a command to basically print specific lines, such as:

print line 50-100 from MyProductList.sh

This is just an example to give you an idea. inside the file will simply be 1000 lines of 1 worded products. One word on each line.

The result I need might look like this:

50. tea
51. coffe
52. orange
53. banana
54. etc
  • Hi and welcome. Can you edit your question and provide a sample of what the input file will look like? Is it sorted? Does it have any "gaps" in the numbering, or do the numbers run 1-N? Jul 20 '19 at 23:32
  • Hi Andy, done, I hope it makes more sense.
    – Malcolm
    Jul 20 '19 at 23:38

Output 5 lines, beginning from line 2:

$ tail <infile -n+2 | head -n5 >outfile

Output lines 2 to 7:

$ tail <infile -n+2 | head -n$((7-2)) >outfile

Output lines a to b:

$ a=2 ; b=7
$ tail <infile -n+$a | head -n$(($b-$a)) >outfile

If you have a file that contains a sequence of lines, each of which is numbered (in order):

$ cat words.txt
1. something
2. something else
1000. yet something else

And you want to print a range of lines, you can use sed:

$ sed -n '50,100p' words.txt
50. abandonee
51. abandoner
52. abandonment
98. abbacomes
99. abbacy
100. Abbadide

The -n says "don't print lines by default", the 50,100p means "for lines 50-100, print the line".

  • Hi Andy, thank you so much. I used your second command which works fine but I'm wondering whether this also prints in Alphabetical order by default or if we need additional commands for that.
    – Malcolm
    Jul 21 '19 at 13:47
  • I built my sample using words from a dictionary, so they're in alphabetical order, but that has no affect on the output. My command just prints line 50-100, independent of what's on the line. Jul 22 '19 at 14:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.