LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to delete large portions of text on linux scripting? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-delete-large-portions-of-text-on-linux-scripting-4175473374/)

kirito 08-14-2013 12:03 PM

How to delete large portions of text on linux scripting?
 
Hi guys,

I have created a linux program using #!/bin/sh, but later I found out that I don't need around 2/3 of my program.

My program is quite long, and I need to delete around 500 lines or so in total. Now, I don't want to use "backspace" to delete that many lines, so could someone please tell me how to delete many lines at once? (for example, in microsoft word you would have to highlight the text with your mouse and press delete.)

Thanks!

P.S. I am using Cygwin.

sycamorex 08-14-2013 12:08 PM

Hi and welcome to LQ.

You could open the text in vim and delete it as follows:

- Press Esc to make sure you're in the command mode
- press: 100dd

It will delete 100 lines starting from the current line going downwards. You can repeat the command using a dot (.) or type it again. As always, make sure you have a backup before you start experimenting with it.

szboardstretcher 08-14-2013 12:08 PM

Edit:

Sycamorex and I posted the same answer at the same time.

sycamorex 08-14-2013 12:18 PM

We probably posted it at the same time but well... you posted it from Windows.... I did it from Slackware and as we all know, Slackware trumps everything (or not to start distro wars, it definitely trumps Windows). LOL

szboardstretcher 08-14-2013 12:25 PM

I agree. That's why im posting this from Lynx in Scientific Linux to get some street cred back.

If you know the lines numbers and you have sed, then you could delete the lines like this:

Code:

sed -i '100,600d' file.txt
Which is saying, in place, remove lines 100-600 on the file file.txt.

ntubski 08-14-2013 01:00 PM

If you're not worried about street cred, vim's visual mode makes this much easier since you won't have to count lines before starting.

- Press Esc to make sure you're in the command mode
- Navigate to beginning of text to be deleted
- Press v to enter visual mode (you'll see --VISUAL-- at the bottom of screen)
- Navigate to end of text to be deleted
- Press d

You can even navigate with arrow keys or pgup/pgdown (gasp!)

szboardstretcher 08-14-2013 01:03 PM

Quote:

Originally Posted by ntubski (Post 5009274)
If you're not worried about street cred, vim's visual mode makes this much easier since you won't have to count lines before starting.

- Press Esc to make sure you're in the command mode
- Navigate to beginning of text to be deleted
- Press v to enter visual mode (you'll see --VISUAL-- at the bottom of screen)
- Navigate to end of text to be deleted
- Press d

You can even navigate with arrow keys or pgup/pgdown (gasp!)

Abomination!

chrism01 08-14-2013 07:49 PM

What is this 'VISUAL' of which you speak? ;) :)

grail 08-15-2013 06:07 AM

1 Attachment(s)
Maybe he means:


All times are GMT -5. The time now is 11:36 PM.