Write a Perl program to display contents of the file in sorted order(ascending) based on line numbers given via command-line arguments. Note that the line numbers can be in any order and throw an error if the line number is large.
x y z
Filename.pl is the name of the file that contains the perl script
File_to_be_read.abc is the name of the file which is to be read. This file can be of any type. Ex- text, script, etc.
x y z are the line numbers which are to be printed.
Sort the line numbers excluding the first argument which is file name. Once sorted read whole content of the file into an array using single statement (my @file = <FNAME>). Now loop through the sorted line numbers and display the file content by passing line number as index to file array like (print “$file[$var-1]\n”;).
Example 1: Consider a file Hello.txt
Example 2: Reading the same script file
If the line number passed is not in the file:
- Perl | Line Action Commands in a Debugger
- Perl | Multi-line Strings | Here Document
- Perl | Use of Hash bang or Shebang line
- Perl | File I/O Functions
- Perl | File Upload in CGI
- Perl | File Locking
- Perl | Writing to a File
- Perl | eof - End of File Function
- Perl | Reading a CSV File
- Perl | Appending to a File
- Perl | Opening and Reading a File
- Perl | Useful File-handling functions
- Perl | File Test Operators
- Perl | File Handling Introduction
- Perl | Searching in a File using regex
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.