Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim replace all characters up to first comma

I am trying to use VIM to replace all the characters up to the first comma in a large text file (10mb+)

I have something that looks like:

foo,bar,sun
apple,pear,goose
monkey,pig,baby

and I want it to look like:

bar,sun
pear,goose
pig,baby
like image 258
user3646369 Avatar asked Nov 20 '14 00:11

user3646369


4 Answers

The following should do it

:%s/^[^,]*,//

Explanation:

  • : Command mode
  • % Apply to every line
  • s Substitute
  • / match
    • ^ From the start of the line
    • [^,] Any character other than comma
    • * (See previous) Repeated or empty
    • , A comma
  • / replace
    • nothing
  • / finished

Alternatively you can use sed:

sed 's/^[^,]*,//' -i FILENAME

or

sed 's/^[^,]*,//' FILENAME > NEWFILENAME

Edit: minor formatting and explain ":"

like image 97
teambob Avatar answered Oct 31 '22 12:10

teambob


You can use

:%norm df,

to run the normal command df, on every line in the file. Which deletes from the beginning of the line up to and including the first comma.

Read :help :normal

like image 27
FDinoff Avatar answered Oct 31 '22 12:10

FDinoff


This should do it:

[esc]:%s:^[^,]*,::

edit: of course you can also use cut:

cut -d , -f 2- < mybigfile.txt > newfile.txt
like image 2
xbug Avatar answered Oct 31 '22 13:10

xbug


:%s/.\{-},//

This version uses a non-greedy quantifier \{-} which causes the preceding dot to be matched 0 or more times but as few as possible (hence it is non-greedy).

This is similar to using a *? in most other regular expression flavors.

like image 2
Eric Mathison Avatar answered Oct 31 '22 12:10

Eric Mathison