print or remove lines of a file
Show one line by its number, a range, skip the first ones, or take off the last.
Steps
-
sed -n '5p' file.txtline 5
-
sed -n '5,10p' file.txtlines 5 to 10
-
tail -n +2 file.txteverything from line 2: skip a header
-
head -n 20 file.txtthe first 20 lines
-
tail -n 20 file.txtthe last 20 lines
-
sed -i '$d' file.txtremove the last line
-
head -n -5 file.txteverything but the last 5 lines (GNU)
Commands used
sedheadtailawk
Each has a page in lore: lore sed opens it.
Related recipes
- add up numbers in a fileSum a column of numbers, from a file or a command's output.
- compare two foldersFind which files differ between two folders, or are only in one of them.
- count lines of codeHow many lines of code a project has, in total and by language.
- count lines, words and filesCount the lines of a file or of a command's output, the words, and the files in a folder.