Archive for September, 2012

grep -e :searching multiple terms

Saturday, September 22nd, 2012

When you have to process log files, it can be very useful to highlight the terms you are seeking. One way to highlight a term is to use grep with the –color option. The drawback to using this method is that if you are chaining the grep command after another command then the last grep is all that would be highlighted.
I found that you could use the following to highlist all terms:

grep --color -e 'search|search2' 

So to break down the command line, first we have the grep command followed by the –color option which (if your terminal allows it) will highlight the search term within the screen results. This means that if you put this into a file, then the color highlighting will be lost. The ‘-e’ option states that the next argument is going to be a regular expression based argument.
The full syntax of regular expressions is beyond the scope of this article but basically is a pattern to match strings. The reason I am using a -e option is to support the ‘|’ option that allows me to search for more than one term. The reason that the pipe character is escaped is that otherwise it would be considered as a character to search for instead of a separator. This also will color either term within the output. So I am searching for search or search2 anywhere within the files that I pass in.

My Favorite Vi(m) commands

Thursday, September 20th, 2012

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!

Awk: Separating delimited files on the command line

Wednesday, September 12th, 2012

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

Windows: Add Copy-to and Move-to folder to context menu

Tuesday, September 4th, 2012

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