How Do I Delete a Specific Line in Linux?
Contents

How do I remove lines from a file?

Use del to delete a line from a file where its position is known {#use-del)

  1. a_file = open(“sample.txt”, “r”) get list of lines.
  2. lines = a_file. readlines()
  3. a_file. close()
  4. del lines[1] delete lines.
  5. new_file = open(“sample.txt”, “w+”) write to file without line.
  6. for line in lines:
  7. new_file. write(line)
  8. new_file. close()

How do I remove an extra line in Unix?

Use the grep command to delete empty lines.

  1. Use the awk command to delete empty lines in the file. …
  2. Use the sed command to delete empty lines in the file. …
  3. Use the grep command to delete empty lines in the file.

28 апр. 2020 г.

Sophia Al-Mansoor

Sophia Al-Mansoor

Global Business & E-Commerce Reporter

Sophia analyzes international trade, startup ecosystems, retail transformation, and supply chain logistics for modern digital publications.

Share this article
Twitter Facebook Pinterest