Welcome to my website. I am always posting links to photo albums, art, technology and other creations. Everything that you will see on my numerous personal sites is powered by the formVistaTM Website Management Engine.

icon.linkedin.jpgicon.twitter.jpg

  • Subscribe to this RSS Feed
  • Vim Search and Replacing with Backreferences
    10/10/2014 12:06PM

    It is often helpful to write search and replace commands that save segments of the matched text to use in the replacement string.

    Source text:


     This is some text (we want to change) with a phone number (301) 555-1234.

    We want to remove the parenthesis, but only from the phone number string.


    In this example we have a text file that has a phone number in it and we want to remove the parenthesis that surround ONLY the area code in the phone number, plus the trailing space and replace it with the aread code plus a trailing "-".

    Following is the search and replace command (the forward slashes are bolded indicating that they are separators in the search command.  They will be omitted in the full explanation below:

    :1,%s/(\([0-9]\+\))\s/\1-/g

    Here is it explained:

    [:1,%s]

    Tells, vi to execute the search and replace command from line 1 to the end of the document

    [(\([0-9]\+\))\s]

    The regex including the backreference that will find the area code including the parenthesis and a trailing space.  The \(  and \) parenthesis wrap the numerical part of the string that we are trying to save and mark it as a backreference.  The \+ indicates that we one one or more of the numerical characters (the plus char must be escaped).  The \s indicates a whitespace character the we also want to match.

    [\1-]

    The characters that we want to use to replace what we have found.  The \1 indicates we want to use the first backreference that was matched followed by a '-' character. 

    [g]

    Tells, vi to execute the search and replace on all occurances found in a line.

  • Internet Search for Code: SymbolHound
    09/26/2014 11:38AM

    http://symbolhound.com/

    Allows you to enter search strings that include the typical set of characters included in various programming languages.

  • Print Lines in a File From a Specific Line Number Until the End of the File with sed
    09/17/2014 11:33AM

    If you know that you want all of the lines in a given file from n to EOF the following is the sed command:

    sed -n '3,$p' some_file.txt

    To print out lines 2 - 5 simply modify it to:

    sed -n '2,5p' some_file.txt

  • Removing the Last Token From a String in Bash with awk
    09/04/2014 5:07PM

    Let's say that you have some number of files for which you want to create a containing directory that is named with all but the last token of the file name, and you want to remove just the last token to create the name of the directory.

    Much easier to explain with an example.  Given this list of files:

    $ ls -1
    foo_10_10_sometrash
    foo_1_sometrash
    foo_2_sometrash
    foo_3_sometrash
    foo_4_sometrash
    foo_5_5_sometrash
    foo_5_sometrash
    foo_6_6_sometrash
    foo_7_7_sometrash
    foo_8_8_sometrash
    foo_9_9_sometrash

    You want to create a directory for each of the files as follows:

    foo_5_sometrash should have a directory named foo_5.

    Further, let's assume that you have thousands, or hundreds of thousands of files.  In that case doing it via a script while you get a cup of coffee is the preferred solution.

    The work will be done within a for i in loop iterating over the output from ls with a nested awk command.

    for i in `ls -1`; do DIRNAME=$(echo $i | awk -F_ '{$NF=""; print $0}' | sed 's/ /_/g' | sed 's/_$//g'); mkdir $DIRNAME; done

    Here is the command broken down:

    DIRNAME=$(....)

    will set the var $DIRNAME to the result of the scripting within the parenthesis.

    awk -F_ '{$NF=""; print $0}

    will set the field separator to a '_', the character on which you will be 'splitting' your string.  '$NF=""' will set the last field to an empty string and then the 'print $0' will print the entire input line.

    The following sed commands will replace the spaces generated by the awk command with the original separators, '_', and then remove the spurious, trailing '_'.


  • Backspace, Delete, and/or Return Key Stops Working in Oracle SQL Developer
    08/29/2014 9:09AM

    So, I figure up SQL Developer to run some queries against a QC server and for some reason, I am no longer able to use the backspace, delete, or return keys to edit .sql files opened in the program.

    I tried opening a new .sql file, and restarting SQL Developer.  I then tried restarting Windows.  None of those worked.

    After a bit of searching I found a forum posting that indicated by going to Tools/Preferences/Accelerators and clicking the "Load Preset..." button in the bottom right of the dialog box would fix the problem.  My guess is that some key mapping preference file had gotten corrupted some how and that by replacing it with a default that it fixes the problem.

    After doing so, I was back in business.

Advanced Search

Categories

Archives