prefix


An example of MySQL code that executes TRIM() to remove a prefix and/or a suffix from all entries that match a WHERE clause

The following code will remove the prefix http://wow.example.com from all rows that match the where clause:

Update `my_table`
set
`my_column` = TRIM(
  LEADING 'http://wow.example.com'
  FROM `my_column`)
WHERE (`my_column` LIKE '%http://wow.example.com/%');

The next block will remove the suffix index.php from all entries that match the where clause:

Update `my_table`
set
`my_column` = TRIM(
  TRAILING 'index.php'
  FROM `my_column`)
WHERE (`my_column` LIKE '%/index.php%');

In case we need to remove the string needle both from the prefix and the suffix while using a where clause, we can use the following code:

Update `my_table`
set
`my_column` = TRIM(
  BOTH 'needle'
  FROM `my_column`)
WHERE (`my_column` LIKE '%needle%');

How to: remove prefix and suffix from a variable in bash 3

string="/scripts/log/mount_hello_kitty.log";
prefix="/scripts/log/mount_";
string=${string#$prefix}; #Remove prefix
suffix=".log";
string=${string%$suffix}; #Remove suffix
echo $string; #Prints "hello_kitty"

In the above example we have as input the variable string that contains following /scripts/log/mount_hello_kitty.log.

We want to remove from that variable the prefix, which is /scripts/log/mount_ and the suffix, which is .log.

The above code will replace in place the input that is contained in the variable string and remove the prefix and suffix we defined in the respective variables.

string=${string#$prefix} returns a copy of string after the prefix was removed from the beginning of the variable.

string=${string%$suffix} returns a copy of string after the suffix was removed from the end of the variable.


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.