LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-14-2013, 12:03 PM   #1
kirito
LQ Newbie
 
Registered: Aug 2013
Posts: 3

Rep: Reputation: Disabled
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.
 
Old 08-14-2013, 12:08 PM   #2
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209
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.
 
3 members found this post helpful.
Old 08-14-2013, 12:08 PM   #3
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,184

Rep: Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584
Edit:

Sycamorex and I posted the same answer at the same time.
 
Old 08-14-2013, 12:18 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,819
Blog Entries: 1

Rep: Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209Reputation: 1209
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

Last edited by sycamorex; 08-14-2013 at 12:36 PM. Reason: s/disto/distro/
 
Old 08-14-2013, 12:25 PM   #5
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,184

Rep: Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584
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.

Last edited by szboardstretcher; 08-14-2013 at 12:36 PM.
 
Old 08-14-2013, 01:00 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Arch
Posts: 3,078

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
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!)
 
1 members found this post helpful.
Old 08-14-2013, 01:03 PM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,184

Rep: Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584Reputation: 1584
Cool

Quote:
Originally Posted by ntubski View Post
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!

Last edited by szboardstretcher; 08-14-2013 at 01:04 PM.
 
Old 08-14-2013, 07:49 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,300

Rep: Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358Reputation: 2358
What is this 'VISUAL' of which you speak?
 
Old 08-15-2013, 06:07 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,438

Rep: Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842Reputation: 2842
Wink

Maybe he means:
Attached Thumbnails
Click image for larger version

Name:	visual.png
Views:	14
Size:	1.1 KB
ID:	13204  
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Knoppix on CD, Upon starting I get large menu with large text & blank submenus samdaria Linux - Newbie 1 06-06-2008 09:59 PM
extracting a chunk of text from a large text file lothario Linux - Software 3 02-28-2007 08:16 AM
extract text portions from html files linuxfond Programming 3 04-28-2004 11:00 AM


All times are GMT -5. The time now is 03:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration