How do I get rid of just one character?
November 11, 2008 7:06 AM   RSS feed for this thread Subscribe

I need a way to remove one character from a fixed width file, and put a zero in another spot to compensate. How would I get this done?

I've got this file, full of record sets like this: http://pastebin.ca/1253309

In the RDG record, you see the 0000017549 from positions 34-43. What I need is for the 9 to be removed, and a zero to be put in the front of the reading to look like 0000001754 . It's always going to be the 43rd character that is lost, and it's always going to be a zero to be put added in at position 34. Would the hive mind please help a person out?
posted by deezil to computers & internet (9 comments total) 1 user marked this as a favorite
You want to do this once or on thousands of files?
posted by kamelhoecker at 7:22 AM on November 11, 2008


if I only needed to do this once, I'd do it in vim, by recording a macro, then replaying it. for your case, the necessary keystrokes are:

first, do a search for lines that you need to change - looks like they all start with RDG. so, making sure you're at the top of the file /RDG

qq (to start recording a macro into the 'q' buffer)
0 (to make sure you're at the beginning of the line)
42l(to move to the 43rd character)
x (to delete the 43rd character)
0 (back to the beginning of the line)
31l (to move to the 32nd character)
i (enter insert mode)
0 (type a zero)
ESC (hit the escape key to leave insert mode)
q (to stop recording the macro)

then, you replay by hitting n (to go to the next line beginning with RDG) @q to redo the steps you recorded.

vim is avaliable for all the major platforms, and may already be installed. good luck.
posted by namewithoutwords at 7:29 AM on November 11, 2008 [1 favorite]


As namewithoutwords says, any editor that can record and playback keystrokes would work if it's only a few hundred times.

My favourite would be a quick python script:
outfile = file('outfile.txt','wt')
for ln in file('infile.txt','rt').readlines():
    if ln.startswith('RDG'):
        outfile.write( ln[0:35] + '0' + ln[35:42] + ln[44:] )
    else:
        outfile.write( ln )

posted by samj at 7:45 AM on November 11, 2008


It's just on one file for right now. This would be handy to keep for in the future in case the issue arises at another site that uses the same file setup. The vim script looks like a winner so far, let me try it out here a little later, and I'll get back to you fine MeFites.
posted by deezil at 8:02 AM on November 11, 2008


perl -pe 'substr($_, 33, 10) = 0 . substr($_, 33, 9) if /^RDG/' input.txt
posted by nicwolff at 8:29 AM on November 11, 2008 [1 favorite]


If you are less high tech than the guys above, just get Editplus.
Place yourself at the start of your line, hit record. Arrow along to position 43, delete your number, back up with your arrows to position 34, and pop in your zero. Hit "home" and arrow-down to position yourself at the start of the next line, and stop recording.

You will have saved the recording to the key-combo Alt-1, so just lean on the Alt-1 until you reach the end of the file. If it is a giiiiiiiiiiiiinormous file, you can set up another macro, on Alt-2, with say, ten instances of Alt-1.

Once you get the hang of Editplus there are far more sophisticated ways of doing this, but for a quicky-fix, this should take you less than 5 minutes. I've used it a lot in dataprocessing.
posted by Iteki at 8:52 AM on November 11, 2008


Be VERY careful with this one.
posted by thegmann at 1:20 AM on November 12, 2008


it would also be pretty easy to do in MS Excel, if its one file.
posted by softlord at 4:58 AM on November 12, 2008


Sorry it's taken so long to get back to y'all on this, but the vim solution worked. More comfortable in vim than anything, and I tried excel, but with the leading zeroes and the different line types, it was being sorta hateful. I just hated having to do this about 500 times for the file, but I'll be okay.

Thanks again, hive mind.
posted by deezil at 7:49 AM on November 19, 2008


« Older Why can't I see any of the ima...   |   I am several weeks away from l... Newer »
This thread is closed to new comments.