Vim command to move certain words with a pattern to a new text file
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Linux Mint 12, FreeBSD, Ubuntu 12.10, Mac OS X
Posts: 83
Rep:
Vim command to move certain words with a pattern to a new text file
Does anyone know the command to extract only the words with a certain symbol in front of them from a text file. I know the command should look something like this: %s/\v(.*)(*@,\d)(.*)/\2 if I wanted to extract words only having @ in front of them. However, I’m getting an error “E59: invalid character after @" What is going wrong?
I am not sure your question is specific enough (better definition of "move" and "in front of"), and your "command" (regular expression) does not seem to make much sense, but I suspect that sed or awk would be more what you are looking for.
It would help f you could be a little more exact in what you need, and maybe give an example or two of what you want to extract and how it should appear in the new file.
Distribution: Linux Mint 12, FreeBSD, Ubuntu 12.10, Mac OS X
Posts: 83
Original Poster
Rep:
I'm trying to do this in Vim, which I neglected to directly mention in the post. Seems like it should be simple. What do you think is going wrong? So if the orignal file was a 400 page text file with 20 email addresses in it on random pages, and I only wanted to extract the 20 email addresses. How can I get Vim to just spit out the email addresses from that text file?
Distribution: Linux Mint 12, FreeBSD, Ubuntu 12.10, Mac OS X
Posts: 83
Original Poster
Rep:
Quote:
Originally Posted by astrogeek
I am not sure your question is specific enough (better definition of "move" and "in front of"), and your "command" (regular expression) does not seem to make much sense, but I suspect that sed or awk would be more what you are looking for.
It would help f you could be a little more exact in what you need, and maybe give an example or two of what you want to extract and how it should appear in the new file.
You were right! awk did a beautiful job of separating everything into a list in the terminal. I copied that list into a spreadsheet and filtered it. I will be looking into more of what awk can do. What a powerful little program! Thank you!
I know the command should look something like this: %s/\v(.*)(@,\d)(.*)/\2 if I wanted to extract words only having @ in front of them. However, I’m getting an error “E59: invalid character after @" What is going wrong?
An old thread and solved as well, but at least, I can say what was wrong with the pattern. In Vim regular expressions, \v means very magic, it allows using any non-alphanumeric RE metacharacters without preceding backslash. Non-alphanumeric is the key word here. Not only parentheses and braces, but really anything and everything: e.g. < and > instead of \< and \> for word boundaries, etc.
Now, \@ happens to be a metacharacter for lookarounds in Vim regular expressions. So, after \v Vim treats a bare @ as such metacharacter, but doesn't see the correct syntax for it: e.g. @= for positive lookahead, @! for negative lookahead, and so on. Thus, the error message.
BTW, Vim error codes are tags in Vim help. To find out what this error is about, type :h e59 from inside Vim.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.