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

  • Looping Through a List of Files with Spaces in the File Name with Bash
    02/16/2016 8:58PM

    If you have a list of files that you want to operate on in a loop in bash and some of them have spaces in the file name the default IFS (Internal Field Separator) will match with the space and tokenize the file.

    The simple approach is to temporarily set the IFS as follows.  This can be done in a shell script, but the following example is directly on the command line for 'one-liner' usage.

    $ OIFS="$IFS"

    $ IFS=$'\n'

    $ for i in `find ./ -type f -iname '*some_criteria*'`; do "something with $i"; done

    $ IFS="$OIFS"

    The previous commands will:

    1. Save the existing IFS
    2. Update the IFS to a newline char
    3. Execute your loop with the results of a find command
    4. Reset the IFS
  • Adding the Contents of a Source File to the Beginning of a Target File
    10/26/2015 4:49PM

    Following is *nix a command that you can use to add the contents of a source text file to the start of another text file (the source file).

    $ echo -e '0r <source_file_name>\nw' | ed -s <target_file_name>

  • Use awk to Print from nth element to the End of the Line
    09/15/2015 5:03PM

    If you want to extract from the nth token to the end of the line, following is how you can do that with awk:

    Given a source file with the following:

    line1 -- 01   0011 1
    line2 -- 01   0011 2
    line3 -- 01   0011 3
    line4 -- 01   0011 4
    line5 -- 01   0011 5
    line6 -- 01   0011 6
    line7 -- 01   0011 7
    line8 -- 01   0011 8
    line9 -- 01   0011 9
    line10 -- 01   0011 10

    If you want remove the 1st, 2nd, and 3rd items from the list, you can use awk to set those fields to an empty value as follows

    awk '{$1=$2=$3=""; print $0}' test.out

    Which will result in:

       value1 00 0011 1
       value2 00 0011 2
       value3 00 0011 3
       value4 00 0011 4
       value5 00 0011 5
       value6 00 0011 6
       value7 00 0011 7
       value8 00 0011 8
       value9 00 0011 9
       value10 00 0011 10

  • Generate a Randome String of a Specified Size with a Shell Script
    06/24/2015 12:48PM

    The following is a one-liner for generating a random string of a fixed size in bash, where the possible characters to use in the string are any digit, letter, and a newline.

    By adding the newline, you are fairly sure to prevent getting one long line of text.

    $ < /dev/urandom tr -dc "[:digit:][:alpha:][\n]" | head -c1000 > file.out

  • Writing a BASH Script to Read from STDIN to a Variable
    05/15/2015 1:33PM

    Let's say you have some program that is generating output to STDOUT and you want to write a script to read that output from STDIN and use it as a variable in your script.

    To do so:

    #!/bin/bash

    SOME_VAR=$(cat)

    echo "SOME_VAR = $SOME_VAR" 

  • 1 2 3 >>
Advanced Search

Categories

Archives