Operating on Lines by Number

Learn how to perform operations on lines in a file by their number.

Finding a line

We usually find a line with text or a regular expression, but we can also find it by its number. Let’s go back to the urls.txt file and explore this further.

Let’s comment on the first line of the file. We use the following command to target the first line and insert the comment character:

$ sed -e '1 {s/^/#/}' urls.txt

To comment on lines 2 through 4 of the file, we use the following command that specifies a range:

 ...