Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 03-30-2007, 01:31 PM   #1
LQ Newbie
Registered: Mar 2007
Posts: 7

Rep: Reputation: 0
vi question regarding removing part of a line

What command can I use to delete everything from the beginning of a line up to a specific word in the line? Let me add to that, I want to do it for an entire file, not just a fe lines in the file.

Last edited by RobHill; 03-30-2007 at 01:45 PM.
Old 03-30-2007, 01:35 PM   #2
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,821
Blog Entries: 15

Rep: Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664Reputation: 1664
If you knew how many "words" there were you could do #dw where # is the number of words before what you want to delete.

"The quick brown fox jumped over the lazy dog"

From start of line 5dw would leave you with:
"over the lazy dog"

It gets tricky sometimes figuring out what a "word" is. Usually it is items separated by white space however some characters are treated as words.

The quick brown (fox) jumped over the lazy dog

From start of line 5dw would leave you with:
") jumped over the lazy dog"
because it counted the "(" as a "word".
Old 03-30-2007, 03:42 PM   #3
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,771

Rep: Reputation: 468Reputation: 468Reputation: 468Reputation: 468Reputation: 468
Try that one, assuming the word is "foo" and you want to keep it:
:% s/^.*\<foo\>/foo/

Last edited by jlliagre; 03-30-2007 at 03:44 PM.


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
Sed and printing only part of a line jrdioko Linux - Software 12 05-17-2012 08:15 PM
shell script question: removing a line according to a duplicate "field" mattie_linux Linux - Newbie 1 01-12-2006 04:57 PM
Regex Question: Only print part of line that matches TheMeteorPolice Programming 5 01-12-2006 02:21 PM
part of line disappears when scrolling in Mozilla andrewstr Linux - Software 4 11-05-2003 07:38 PM
How to extract a part of a line by sed? J_Szucs Programming 2 02-15-2003 07:49 PM > Forums > Linux Forums > Linux - Newbie

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

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