Score:2

How to delete a range of lines in a text file?

us flag

I want to know how to quickly delete text with Bash. For example, I have a file named file.txt with a total of 500 lines.

I want to delete lines 80 to 146 - how do I do that?

What's is a terminal command that can do that?

Score:6
cn flag

If this is one time I would use vi or vim

vi {filename}
:86,146d
:wq

That last one saves and quits. Using sed:

sed '86,146d' {file}

Make a backup if unsure so you can revert.

Score:4
jp flag

With awk to print in terminal:

awk 'NR < 80 || NR > 146' file.txt

Or with gawk to edit file in-place:

gawk -i inplace 'NR < 80 || NR > 146' file.txt
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.