use vi editor to insert newline char in replace

Something else I have to do and cannot remember and then have to look up.

In vi to insert a newline character in a search and replace, do the following:

:%s/look_for/replace_with^M/g

the command above would replace all instances of “look_for” with “replace_with\n” (with \n meaning newline)

to get the “^M”, enter the key combination “ctl-V” then after that (release all keys) press the “enter” key.

Get PS to display the complete path (on Linux)

Something else I can never remember and waste time looking up is how to display long long long paths when looking at processes with “ps”. Well just a couple of “W” does the trick.

ps -auxww

More on why, later but wanted to put it down while it was fresh

Apple/Mac – Convert Picture Clipping ( pictclipping ) image to PNG, JPEG, etc

UPDATE 2011 March 8: A commenter reports dragging the pictcliped image into PowerPoint allows you save the image from there, possibly in other formats. I cannot test this as I do not have this setup. It appears however that the latest version of iTunes, at least, no longer creates these when copying album art out of the app.

UPDATE 2010 July 4: This process no longer works with 10.6 (.3 for me) as reported by readers. It seems that Apple found this feature and killed it. However there is a simple workaround.  See the section for 10.6.3

For versions of OSX up to 10.6:

So a little annoying feature of some Apple/Mac apps is the creation of a image in the format “pictclipping”. Converting these to something usable is not so intuitive so I am posting it here as to be easily located for myself if for nobody else (though I imagine others are looking for this from time to time).

Anyway, the easiest way to do this seems to be to use the clipboard and the “Preview” application.
To do the conversion in this way, first double click the pictclipping image to open it in the finder. Next from the Finder’s “Edit” menu choose “Copy”. Next, launch the “Preview” application and from the “File” menu choose “New from clipboard”. Then using the “File” “Save as” menu options, save as any image type you like.

Pretty simple, but not something I would have thought of right away.

For versions of OSX after 10.6:

Ok, so it seems Apple did not like the feature of being able to coy a Picture Clipping so easily, using their own software. I have tried to figure out a solution and did some searching but without some purchased software, so far, it seems this is not possible. However, here is something you can do to hold you over till something is discovered.

Right click on the Picture Clipping, in the Finder, choose “Quick Look [NAME OF YOUR PICTURE CLIPPING HERE]”  to view the full sized image in the Finder Quick Look previewer app. Then use the selective screenshot tool to grab just the image. If not familiar with this process: with the Picture Clipping image up, choose the [command] key at the same time as the [shift] key and then press the [4] key. This will bring up a small crossed lines (not sure how to explain it, think crosshairs for targeting something) pointer. Use this to hilight just the portion of the picture clipping in the preview that you wish to copy. When you release the pointer this image will be on the desktop as a PNG file with a name like “Screen shot 2010-07-04 at 5.16.53 AM”. Simply rename this file to something useful like “my-album-art.png” and you are done.

Hope something better appears. I will keep looking for something.

j

Determine if a String is XML using Java and Regular Expressions

So again I am posting something I have to do every now and then and have to spend time, each time, to check the pattern or usage etc. for.

Once in a while, in an app that does not do much XML, and therefore is not already using an XML parser of some kind, will need to at the least, determine if a String is XML. With a pretty simple Regular Expression, it is possible using plain old Java and without using any specific XML technology.

I know there are other references out there for doing this, but it is here below as a code sample, for my easy reference and maybe it will help someone else out, who knows. Enjoy.

Are we XML (like) data? :

import java.util.regex.Pattern;
import java.util.regex.Matcher;


public class test {



    /**
     * return true if the String passed in is something like XML
     *
     *
     * @param inString a string that might be XML
     * @return true of the string is XML, false otherwise
     */
    public static boolean isXMLLike(String inXMLStr) {

        boolean retBool = false;
        Pattern pattern;
        Matcher matcher;

        // REGULAR EXPRESSION TO SEE IF IT AT LEAST STARTS AND ENDS
        // WITH THE SAME ELEMENT
        final String XML_PATTERN_STR = "<(\\S+?)(.*?)>(.*?)</\\1>";



        // IF WE HAVE A STRING
        if (inXMLStr != null && inXMLStr.trim().length() > 0) {

            // IF WE EVEN RESEMBLE XML
            if (inXMLStr.trim().startsWith("<")) {

                pattern = Pattern.compile(XML_PATTERN_STR,
                Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE);

                // RETURN TRUE IF IT HAS PASSED BOTH TESTS
                matcher = pattern.matcher(inXMLStr);
                retBool = matcher.matches();
            }
        // ELSE WE ARE FALSE
        }

        return retBool;
    }



}/**/

DOM Document – get or extract contained document (or Node) as XML Source

Something I have to do every once in a while, and can never remember how (especially when under some tight deadline, with people standing over my shoulder asking “is it done yet, is it done?” “how much longer?” etc.) is to extract a fragment of one DOM document to get the XML source of the nested or contained document. So I am going to add a note here, for everyone’s easy reference.

First step is to get a Node to be the Root Node of the new Document. Using methods like Document’s getElementsByTagName(String) and Node.getChildNodes(), or using XPathAPIs and CachedXPathAPI class’ selectSingleNode(Node n, String xPath).

Next we can use a StringWriter and a Transformer to covert the Node to XML Source. Better than a rambling explanation, a simple source example should be do the trick. You can use a method something like the nodeToXMLString example below.

  private String nodeToXMLString(Node node) throws TransformerException
  {
    StringWriter sw = new StringWriter();

    Transformer serializer = TransformerFactory.newInstance().newTransformer();
    serializer.transform(new DOMSource(node), new StreamResult(sw));

    return (sw.toString());
  }

Declare and fill a multi dimensional array in Java

This is one of those super simple things I wont use for ever and ever and then when it comes time to use it, I can never remember and have to look at the API again. It makes me angry that I don’t remember it and so it is here now and I (and others maybe) can remember with a click.  

Using a Java String array as an example, the declaration looks like:

 

String[][] myStr = {
{"col-0-row-0","col-1-row-0"},
{"col-0-row-0","col-1-row-0"}
};
Follow

Get every new post delivered to your Inbox.