To number the lines of a file in a shell script, you can use the nl
command, which is a utility that adds line numbers to the output of a command.
For example, to number the lines of a file named file.txt
, you can use the following command:
nl file.txt
This will output the contents of file.txt
, with each line numbered according to its position in the file.
You can also use the -w
option to specify the width of the line number field, and the -v
option to specify the starting line number. For example:
nl -w 4 -v 10 file.txt
This will output the contents of file.txt
, with each line numbered according to its position in the file, starting with line number 10
. The line number field will be 4 characters wide, with leading zeros added as needed.
To save the output of the nl
command to a new file, you can use the >
operator to redirect the output to a file. For example:
nl file.txt > numbered.txt
This will save the output of the nl
command to a new file named numbered.txt
.
For more information about how to use the nl
command to number the lines of a file in a shell script, you can consult the nl
documentation or use the man nl
command to see the manual page.