0

I'm trying to delete pattern from a file.

Edge[15] = 415 
Edge[16] = 65 
Edge[17] = 835 
Edge[18] = 951 
Edge[19] = 999 
Edge[20] = 887 ...

How to delete everything before Numbers i.e till = , i.e i want just numbers and then wants to join using ,.

[415,65,835,...]

2
  • 7
    Have you tried anything so far? By showing your attempts and describing what parts you are having difficulty with, we get a better understanding of your proficiency and can give a more precise answer. As it stands, it just looks like you've posted a requirement and want someone to write your code for you. Commented Dec 4, 2015 at 11:47
  • Oh forgot about the post ... I can search for .*= and replace it with = /=... i.e delete all things till = . and then do the same and replace it with empty. then add using regular expression at end of line , and then %j . I wanted was a one liner . If possible... Commented Dec 4, 2015 at 17:27

2 Answers 2

2

Okay, so you're asked for a one-liner instead of the multiple steps you've given. There surely are many other ways, here's one:

:%s/.*=\s*\(\d\+\)\n/\1, / | normal! $xr]^OI[

This does the removal of the stuff before = and the joining of lines both with :substitute (adapt the :% range to suit your needs), by capturing only the number. I then use normal mode commands to get rid of the trailing comma and add the square brackets. (Type ^O as Ctrl-V Ctrl-O; Ctrl-Q CTRL-O on Windows.) If you have the surround.vim plugin, this could be further simplified. One could also use a second :.substitute to achieve the same.

Actually, I probably would have used visual blockwise mode, a special J mapping, and normal mode to achieve this.

Sign up to request clarification or add additional context in comments.

3 Comments

oh.. Need some time to digest it. Thanks.
Don't forget the excellent built-in :help; you'll learn tons from it!
Okay. Have just started to use vim hence little slow in catching. But surely will become dangerous in few weeks,
0

You can use two regex

:%s/.*\( \d\+\).*\n/\1,/g

:%s/^ \(.*\),.\{-}$/[\1]

Output:

[415, 65, 835, 951, 999, 887]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.