Itai Ganot
Itai Ganot

Reputation: 6333

How to delete text until the end of each line after matching a pattern in Vim?

I have a file with the following content:

domproxy1.dom.company.com  Centos CentOS 6.5 production  S5520UR   7 days ago  Edit
domproxy10.dom.company.com Centos CentOS 6.5 production  S5520UR   7 days ago  Edit
domproxy11.dom.company.com Centos CentOS 6.5 production  X8DTU   7 days ago  Edit
domproxy15.dom.company.com Centos CentOS 6.6 production  X8DTU   8 days ago  Edit
domproxy2.dom.company.com  Centos CentOS 6.5 production  S5520UR   7 days ago  Edit
domproxy3.dom.company.com  Centos CentOS 6.5 production  S5520UR   7 days ago  Edit
domproxy4.dom.company.com  Centos CentOS 6.5 production  S5520UR   7 days ago  Edit
domproxy5.dom.company.com  Centos CentOS 6.5 production  S5520UR   7 days ago  Edit
domproxy6.dom.company.com  Centos CentOS 6.5 production  S5520UR   7 days ago  Edit
domproxy9.dom.company.com  Centos CentOS 6.5 production  S5520UR   7 days ago  Edit
domproxy29.dom.company.com Centos CentOS 6.6 production  S2600GZ   about 1 month ago Edit
orgcmsui03.nj.company.com  Centos CentOS 6.6 production  Virtual Mac...    6 days ago  Edit
domftp01.dom.company.com Centos CentOS 6.4 production  ProLiant DL...    40 minutes ago  Edit

I would like to replace the end of each line's (.com *) with (.com), that means to delete everything after the hostname in each line.

Inside vim, I've tried the following:

:%s/.com $/.com/g
:%s/.com*$/.com/g
:%s/.com .*/.com/g

As far I know this is what I'm doing:

:%s/ - search on all the file
.com .*/ - the search pattern
.com/ - the replace pattern
/g - global

But to no avail, what am I doing wrong?

Upvotes: 2

Views: 248

Answers (4)

Peter Rincker
Peter Rincker

Reputation: 45177

I like :normal for these kind of tasks:

:%norm f D

However we can do things a bit more *nix-y by using either awk or cut by filtering with :%!.

:%!cut -f1 -d' '
:%!awk '{print $1}'
:%!awk '$0=$1'

Substitutions using \zs:

:%s/\.com\zs.*

For more help see:

:h :normal
:h f
:h D
:h :range!
:h /\zs
:h magic

Upvotes: 2

VanLaser
VanLaser

Reputation: 1164

I'd do it like this:

:%s/\.com\zs\s.*/

Match dot 'com' whitespace (e.g. space or tab), remove everything after dot com.

Note: the dot is escaped, otherwise any character matches.

Upvotes: 1

Sagar Jain
Sagar Jain

Reputation: 7941

You can also use the power of g

:g/[.]com\>/normal nelD

or

:g/[.]com/normal nelD

as you desire.

It works as below:

In each line find .com and execute the command nelD in normal mode.

n find next match, e go to end of word, l move to right, Ddelete till end of line.

Upvotes: 1

Sagar Jain
Sagar Jain

Reputation: 7941

:%s/[.]com.*/.com

This will do exactly what you need.

%s Search entire file

/[.]com.* Find .com and whatever follows till the end of line

/.com Replace the searched pattern with .com

You don't have to use /g at the end as you have only one occurrence of .com in each line.

Use the below command if you don't want .company.com to be replaced with .com

:%s/[.]com\>.*/.com

Upvotes: 2

Related Questions