I have found myself using Vim as my Linux editor of choice. It is not dependent on X11 being available and I can edit pretty much anything fairly quickly. I have been using a Redhat flavored linux(Redhat,Fedora, Centos) for quite sometime and because I couldn’t guarantee that Emacs would be installed I just got used to using Vi.
When I first attempted to use Vi after using Emacs from my college days, I was initially very frustrated with modes and key sequences. But after a very short time I found I could edit files very rapidly and make changes quite easily.
Quick key list:
| Esc |
The escape key is the way to get out of editor mode and clear |
| Ctrl-c |
will exit the editor mode |
| :q! |
Exits without saving |
| u |
Undo |
| cw |
changes the following word |
| dd |
deletes the line |
| D |
deletes the line to the end |
| J |
removes the end of the line |
| :e or :edit |
reload the file from disk |
1) The thing that I do quite often is to replace all Ctrl-M (Windows Returns) within a file. (Yes, I could do this with a call to dos2unix, but this is a Vi list). I found the following key sequence works quite well.
:%s/Ctrl-v<enter>//g
breaking this sequence down
2) Another sequence I use quite often is to reformat source code.
:1 <enter> <shift-v> <shift>-g =
breaking the sequence down
:1<enter> – states go to the first line of the file
<shift>-v – Turns on visual line mode which selects the entire current line
<shift>-g -Makes the cursor go to the last line in the file and with the last command
selects the entire file
= – Tells the editor to reformat the selection area
This page will be updated as I figure out more useful commands. This is only a small amount of the power of VI!
I have had to process data files from customers that have come in as delimited files. The files are usually in CSV formats and the delimiters might change depending on the content of the files. Sometimes the delimiters are usually tabs”t”, pipes “|”, comma “,” or semi-colon “;”. A useful tool for processing this type of file is to use awk.
Example command line:
awk -F 't' '{OFS="|"; if (NR != 1) print $3,$2,$1}'
The ‘-F’ argument is followed by the character that you have the file separated on. The example above uses a tab escape sequence ‘t’ but you could have any character. One potential problem is if the data contains the same character that you are attempting to split on. As in a comma separated list using commas within a long text sequence.
The Second sequence of quoted text above starts the processing of the file. The if statement uses the NF keyword which stands for Number of Row. This tells the script that it should not fire for the first row. The print statement allows you to pick and choose the columns(numbered from 1) to use or allows you to reorder them.
The ‘OFS’ part of the command line that helps with the print statement describes the Output Field Separator. In the case above it is using a pipe character ‘|’ to separate the text as it is being printed out. This allows you comma separate the fields that you want instead of having to use a more sophisticated printf statement.
The input file is of the form
Label,name,address
And the output is going to be of the form:
address,name,Label
I spend a lot of time copying and moving files between folders. More often than not, when I click on a file in Explorer, I want to copy or move it to another folder. That means I spend a good deal of time dragging files around or copying and pasting them.
But with a Registry entry, you can save yourself time: you can add Copy To Folder and Move To Folder options to the right-click context menu. When you choose one of the options from the menu, you get a dialog that will let you choose the location to copy or move the file to, and then send the file there.
To add the option, run the Registry Editor (regedit) and go to
HKEY_CLASSES_ROOTAllFilesystemObjectsshellexContextMenuHandlers.shellex
This tells you it’s a shell extension key that lets you customize the user shell or the interface. Create a new key called “Copy To”.
Set the value to
{C2FBB630-2971-11d1-A18C-00C04FD75D13}.
Create another new key called “Move To”. Set the value to
{C2FBB631-2971-11d1-A18C-00C04FD75D13}.
Exit the Registry.
The changes should take effect immediately. The Copy To Folder and Move To Folder options will appear. When you right-click on a file and choose one of the options, you’ll be able to move or copy the file using a dialog box like the
So I didn’t have a table of dates set up, but I needed to get a list of dates going
back a certain number of days. I was looking at programming up a date function but
decided that a little SQL could do just what I wanted. I used the following query
to generate my list:
select (current_date-21) +s.a as date from generate_series(0,21) as s(A);
This returned a list of dates:
2012-06-01
2012-06-02
2012-06-03
2012-06-04
2012-06-05
2012-06-06
2012-06-07
2012-06-08
2012-06-09
2012-06-10
2012-06-11
2012-06-12
2012-06-13
2012-06-14
2012-06-15
2012-06-16
2012-06-17
2012-06-18
2012-06-19
2012-06-20
2012-06-21
2012-06-22
This dealt with end of month issues and end of year issues without having to generate a ton
of code to do the same thing.
Tags: Programming, SQL
Here is my basic script for handling command line arguments in Python(pre version 2.7 when optparse
was deprecated). The optparse library is very useful in not only handling the command line
arguments, but setting up your help menu as well.
You will need to create an OptionParser and the usage argument lets you print the usage statement
for the script. Then you can add options, add groups for options or collect the left over
parameters as a secondary command or list of files. I use this method often as I create scripts
that help automate build processes.
#!/usr/bin/python
import sys, optparse
# This creates an OptionParser object with the usage string.
op = optparse.OptionParser(usage=" %prog [options]")
# This will add an option -m or --mode which takes sets the mode variable as
# True or False
op.add_option("-m","--mode",help="Specify the mode for the command",
action="store_true", dest="mode",default=False)
# This parses the options and places the selected options above in the _options
# object and the rest in arguments.
_options,arguments = op.parse_args()
# Checks if no arguments are passed and if so, runs the print_help method of
# the OptionParser object
if len(sys.argv) == 1:
op.print_help()
# This was just to show what happened when you did or didn't pass the
# --mode argument.
if _options.mode:
print "Mode is on"
else:
print "Mode is off"
This method is currently deprecated as of version 2.7. I will write a follow-up post that uses
the newer argparse routine to highlight any differences.
Tags: Python, scripting
When I am writing build automation scripts, there are times that I have to insure that the previous command completed
with no errors. I have found that ERRORLEVEL gets filled in with the return value from a command.
dir %1
if (%ERRORLEVEL%) == (0) echo Found File: %1
if (%ERRORLEVEL%) == (1) echo Missing File: %1
So if you save the previous in a file and run the command with one (1) argument, it will display the output of the dir command
but also print either “Found file: ” or “Missing File: “.
Tags: DOS, scripting
So in 3DSMax it is sometimes useful to insert a background image usually when you are modelling
something and using the background image as a guide. There is no easy way to remove it. You
can reassign another image or set it to not display the background image, but this can lead to
problems if the file is moved or removed as it is still linked to your max file.
MaxScript to the rescue! You can use the MaxScript Listener(F11) to type in the following to
clear the background filename:
backgroundimagefilename=""
This will clear the entry for the filename. I have noticed that you do need to exit 3DSMax to
fully make the change complete.
Tags: 3D, 3DSMax
I was writing a basic guessing game and when I tried to take user input I was
getting the following:
Code:
guess = input()
d
Error:
Traceback (most recent call last):
File "", line 1, in
File "", line 1, in
NameError: name 'd' is not defined
The problem is actually with the version of Python that I was using. If you
are using Python 2.x, you need to use the raw_input() function. And with 3.x
you can use the input() function.
Tags: Python, scripting
There comes a time when you are writing a program and you need to branch based on input to a program.
This is the format of the if/else statement in Python.
if <some true/false statement> :
<some statements that run if the if statement is true>
else:
<some other statements that run if the if statement are false>
There may be times when you have more than one value set to test against. In that case you can write a many if/else
statements but you can chain the if/else statements with an elif. So the chain becomes if/elif/else.
if x < 5:
<some statements that run if the if statement is true>
elif x > 5 and x < 10:
<some statements that run if the elif statement is true>
else:
<if none of the if checks are true, run the statements in the else case.>
Tags: Python, scripting
I was toying with a prime number finder and was creating a set of if/else statements for a horribly failed attempt
and wanted to skip to the next number on any of the if statements that were true. I didn’t want to put an empty print
statement as that would disrupt the output I was expecting from the program. I found the pass statement.
Here is an example from my not very successful attempt at finding primes:
if inp > 2 and inp % 2 == 0:
pass
elif inp > 3 and inp % 3 == 0:
pass
elif inp > 7 and inp % 7 == 0:
pass
elif inp > 9 and inp % 9 == 0:
pass
else:
print inp, " is a prime"
Yes, this does not accurately find the primes, but it would skip any number that is divisible by 2,3,5,7, or 9. And
anything that does not match this will be listed as a prime, this is incorrect and would be made to add new elif
statements as each prime was found.
Tags: Python, scripting