Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Generic User Avatar

Using UNIX sed, how can I delete any line...


  • Please log in to reply
1 reply to this topic

#1 pmidgette

pmidgette

  •  Avatar image
  • Members
  • 1 posts
  • OFFLINE
  •  
  • Local time:03:46 AM

Posted 30 May 2024 - 12:01 AM

Using UNIX sed, how can I delete any line in a file, that repeats on the following line and is followed by a slash.

For example:

   /n_srsq                                                                                                                                         delete

   /n_srsq/create                                                                                                                              delete

   /n_srsq/create/ddl

   /n_srsq/data                                                                                                                                 delete

   /n_srsq/data/BnJavaTest.class

   /n_srsq/data/BnJavaTest.java

   /n_srsq/data/DBA_PART_LIST.donot.delete

   /n_srsq/data/DBA_PART_TST_LIST.del

   /n_srsq/data/LOGS                                                                                                                      delete

   /n_srsq/data/LOGS/all_data_files.bad

   /n_srsq/data/LOGS/all_data_files.dat

   /n_srsq/data/LOGS/all_data_files_sftp.log

   /n_srsq/data/LOGS/chk_env.logs

   /n_srsq/data/LOGS/do_not_delete                                                                                              delete

   /n_srsq/data/LOGS/do_not_delete/BN_SQLPLUS_ORA_CHECKS.DAT



BC AdBot (Login to Remove)

 


#2 Thomas53

Thomas53

  •  Avatar image
  • Members
  • 13 posts
  • OFFLINE
  •  
  • Local time:03:46 AM

Posted 31 May 2024 - 12:36 AM

Open a terminal and run:

     sed '5d' input.txt

The number after sed refers to the line number you want to delete.

 

To delete multiple lines:

     sed '7d;12d;16d' input.txt

Note use of a semicolon to separate line numbers.

 

NOTE: Replace 'input.txt' in the above sample with the name of the file you want to edit.

 

Honestly, if your only deleting these few lines, your standard text editor works just as well and will only take a few seconds longer,. Now if there's many more the this then using sed will be a lot quicker.

 

There is a very good articles at:

phoenixnap.com on the use of the sed command if you want more information.


Edited by Thomas53, 31 May 2024 - 12:43 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users