Simple, I think, VIM Insertion


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Simple, I think, VIM Insertion
# 1  
Old 06-05-2012
Simple, I think, VIM Insertion

Hey everyone, I recently had to learn how to use UNIX and VIM for an internship, and have run in to a little challenge today. I have lists of thousands of file names that represent CAS (Chemical Abstracts Service) numbers, but they are not formatted correctly. CAS numbers run in the form of xxxx-xx-x, where the last two sets are always 2 and 1 digits long, respectively, but the first set of digits varies in length. My file names are just strings of numbers at the moment. Currently I have been inserting the -'s manually, but I believe VIM can automate the process. To clarify; I have a long list of strings of numbers (in either a .txt or .csv format, whichever would be more helpful) and I would like to insert a - before the last and third to last character of every line. Can this be done with a relatively simple VI command, or would writing some sort of shell script be best? I have little to no experience with scripting, so any suggestions would be helpful. Thank you!
# 2  
Old 06-05-2012
It would be better if you posted sample input and desired output. Anyway try this in vim:
Code:
:%s/\([0-9][0-9]\)\([0-9]\)$/-\1-\2/

But I like Perl syntax more:
Code:
perl -pe 's/(\d\d)(\d)$/-$1-$2/' file

Much cleaner and more readable...
This User Gave Thanks to bartus11 For This Post:
# 3  
Old 06-06-2012
Bartus, I can't say I fully understand the command you suggested, but it did the trick wonderfully. Thank you! And in the future I will be sure to post the actual inputs and outputs I want, that slipped my mind.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Insertion Sort Error in C++

TD P { margin-bottom: 0in; }P { margin-bottom: 0.08in; }TT.cjk { font-family: "WenQuanYi Zen Hei Sharp",monospace; }A:link { } I am trying to run this program for Insertion Sort, But don't know that why I am getting following Error #include<iostream> int main(){ int i,j,key,n;... (4 Replies)
Discussion started by: liveproject101
4 Replies

2. Shell Programming and Scripting

Text file insertion via sed

I have 800+ html files that need to have a javascript added to them in the head. I can do the looping, setting filenames as variables, etc. but I cannot figure out how to insert my javascript file into the html. My javascript is in a file named jsinsert.txt It basically has this format:... (4 Replies)
Discussion started by: Trapper
4 Replies

3. Shell Programming and Scripting

Insertion into csv

I want to use bash to insert the variable $a into the cell A1 of a csv file mycsv.csv. How do I insert a variable into a specific cell in a csv file? (1 Reply)
Discussion started by: locoroco
1 Replies

4. Solaris

Very Importan - Vim Settings - Error while opening a File using vim

I downloaded vim.7.2 and compiled the vim source . Added the vim binary path to PATH (Because iam not the root of the box) when i load the file using vim it throws me an error Error detected while processing /home2/e3003091/.vimrc: line 2: E185: Cannot find color scheme darkblue line... (0 Replies)
Discussion started by: girija
0 Replies

5. Shell Programming and Scripting

Insertion in a file

Hi All, I want to insert a line just befor the lst line in a file. please can anyone advise. Cheers, Shazin (3 Replies)
Discussion started by: Shazin
3 Replies

6. UNIX for Dummies Questions & Answers

tip: Simple script won't run in cygwin - vim editor involved

I ran into this issue and thanks to various postings in various forums, was able to figure out the solution but didn't see one posting that laid the whole issue out cleanly. So thought the following might help others ... ------------------------------------------------------------------------... (2 Replies)
Discussion started by: oxysep
2 Replies

7. Shell Programming and Scripting

Insertion of Header record

A header record is to be inserted in the begining of a flat file without using extra file or new file. It should be inserted into same file. Advace thanks for all help... (7 Replies)
Discussion started by: shreekrishnagd
7 Replies

8. UNIX for Dummies Questions & Answers

insertion sort???

Hi, I was wondering if someone here could help me figure out what's wrong with this simple insertion sort shell script. This is the output I get when I try to run it: "23 43 22 15 63 43 23 11 10 2 ./insertion.sh: line 23: 23 43 22 15 63 43 23 11 10 2 And here's the script: ... (2 Replies)
Discussion started by: sogpop
2 Replies

9. Shell Programming and Scripting

automatic header insertion

hi Is there any way to automatically insert a predefined header into a file? It would include the file name, author name, date and a description, the description entered on the first line of vi. Thanks for any help Oliver (4 Replies)
Discussion started by: olimiles
4 Replies

10. UNIX for Advanced & Expert Users

Insertion of Leap Second

Hi All, We are running the HP-UX 11.11 and Linux AS 3.0. so, shall we need to make any changes for leap second i.e. insert the leap second on 1st Jan 2006 or does the system have some setup which would take care of this automatically. Please advise. Regards, Inder (2 Replies)
Discussion started by: isingh786
2 Replies
Login or Register to Ask a Question