Daily Archives: 28 September 2011


Remove leading characters/information from line using sed

Remove leading numbering from line

We had these log files that on most lines at the beginning there was a number followed by a dot and some times it had space characters.
The following sed command removes that prefix. and leaved intact the rest of the lines that do not have the prefix.

cat $log | sed '/^[0-9]*. */!d; s///;q';

e.g

Input: 123. Some text here.
Output:Some text here.

Remove leading whitespace (space and tabs) from line:

The following sed script will remove all leading whitespace from each line by using a regular expression to match space characters and tabs.

cat $log | sed 's/^[ t]*//';

e.g

Input:       Some text here.
Output:Some text here.

Extract filename from full path filename / Get file extension

The first command strips down the full path filename to the filename only ising the basename command.

filename=$(basename $filenamefullpath)

Afterwards you can see how to extract the file extension from the filename.  There is no need to do this after issuing the above command since this command will just remove everything after the first from right dot (‘.’) — so make sure that the filename you are parsing has a dot or you will end up with wrong results (like the full path or a part of the full path if it contains a dot somewhere).

extension=${filename##*.}

Finally, by issuing the following command you remove everything after the first dot on the right (including).

filename=${filename%.*}