Print Lines in a File From a Specific Line Number Until the End of the File with sed

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

Continue reading “Print Lines in a File From a Specific Line Number Until the End of the File with sed”

Removing the Last Token From a String in Bash with awk

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 → Continue reading “Removing the Last Token From a String in Bash with awk”

Checking that Input or a Variable is an Integer in BASH

Here is a quick snippet for checking whether or not a variable is a valid integer in BASH.  It is also a howto for regular expressions in a shell script.

# Make sure that FOO is an integer
if [[ ! "$FOO" =~ ^[0-9]+$ ]]; then
        echo "The FOO was NOT an integer"
fi
Continue reading “Checking that Input or a Variable is an Integer in BASH”

Creating an Array in Bash from a File With Each Element on a Separate Line

Let’s say that you have a file and you would like to convert each line in the file to an element in an array.

The key to this is knowing about and how to manipulate the IFS (Internal Field Separator).  The default IFS is whitespace (a space, tab, or newline) and if you create an array passing it a whitespace delimited list of strings, each token will be set to an element in the array.

ARRAY=(a b d 
Continue reading “Creating an Array in Bash from a File With Each Element on a Separate Line”

Removing The Last N Character From a String in Bash Script with sed

Here is a quick one-liner for trimming a specific number of characters from the end of a string under bash:

# Remove the last 5 characters
$ echo "somestringwith12345" | sed "s/.....$//g"
$ somestringwith

# Remove the last 3 characters
$ echo "somestringwith12345" | sed "s/...$//g"
$ somestringwith12
Continue reading “Removing The Last N Character From a String in Bash Script with sed”

Running Multiple Instances of Notepad++ Under Windows 7

If you want to run multiple versions of Notepad++ under Windows 7 create a .bat file with the following:

"C:\Program Files (x86)\Notepad++\notepad++.exe" -multiInst %1

And simply run the bat file via a cmd prompt/ → Continue reading “Running Multiple Instances of Notepad++ Under Windows 7”

Splitting a String in Bash on the FIRST Occurrence of a Character

About a year ago I posted an article about how to split into an array of values based on a given delimiter in bash.

The following is how to take that same string and split it on the first occurrence of the same user defined delimiter.

Both use the ‘read’ command, but in a slightly different way.

Instead of passing read the -a [aname] parameter which tells it that “The words are assigned to sequential indices of the array → Continue reading “Splitting a String in Bash on the FIRST Occurrence of a Character”

BASH Script With Default Arguments Defined in The Script

Often times you will want to write a BASH script where you don’t want to have to keep track of all of the positional command line arguments and/or you might want to configure it with a set of environmental variables while having a default value for each in the script.

Following is the syntax for declaring them in the shell script, and then an example on how to invoke it.

#!/bin/bash

: ${ARG1:="somedefault_arg1"}
: ${ARG2:="10"}

echo "ARG1 = $ARG1"
echo 
Continue reading “BASH Script With Default Arguments Defined in The Script”

Pin a “Show Desktop” Link to the Task Bar in Windows 7

To create a short cut and pin it to the task bar in Windows 7 do the following:

  1. Right-click on the Desktop and select New -> Shortcut.
  2. Enter the following in the field labeled “Type the location of the item:”  %windir%\explorer.exe shell:::{3080F90D-D7AD-11D9-BD98-0000947B0257}
  3. Click “Next” and name the shortcut and then click “Finish”.
  4. Update the icon so that you will recognize it when you pin it to the task bar.  To do so, right-click on the shortcut and select “Change Icon”
Continue reading “Pin a “Show Desktop” Link to the Task Bar in Windows 7”