JavaSript: Remove all non printable and all non ASCII characters from text

According to the ASCII character encoding, there are 95 printable characters in total.
Those characters are in the range [0x20 to 0x7E] ([32 to 126] in decimal) and they represent letters, digits, punctuation marks, and a few miscellaneous symbols.
Character 0x20 (or 32 in decimal) is the space character ' ' and
character 0x7E (or 126 in decimal) is the tilde character '~'.

Since all the printable characters of ASCII are conveniently in one continuous range, we used the following to filter all other characters out of our string in JavaScript.

printable_ASCII_only_string = input_string.replace(/[^ -~]+/g, "");

What the above code does is that it passes the input string through a regular expression which will match all characters out of the printable range and replace them with nothing (hence, delete them).
In case you do not like writing your regular expression with the space character to it, you can re-write the above regular expression using the hex values of the two characters as follows:

printable_ASCII_only_string = input_string.replace(/[^\x20-\x7E]+/g, "");

Excel / Calc: Convert a Hexadecimal number to Decimal

The following examples allow you to convert hexadecimal values of the format 0xYYYYYY to decimal using a spreadsheet editor like Calc or Excel.

The following codes will remove the first two characters (the value 0x) of the cell B2 and then convert the result to decimal using the HEX2DEC function.

Using the RIGHT function

In this example, we used the RIGHT function with the num_chars parameter to be equal to the number of characters in the cell minus 2. This used to delete the 0x value from the HEX column by removing the first two characters of the cell.
To get the number of characters in the cell we use the LEN function on the cell of interest.


Using the SUBSTITUTE function

In the following example we used the SUBSTITUTE function to automatically find the 0x prefix of the HEX value and delete it by replacing it with an empty string.


Using the REPLACE function

The last example uses the REPLACE function. Starting from the character in position 1 in the cell, it replaces the sub-string of size 2 with the empty string and thus deleting the prefix. Please note that this function is not zero-based so the first character is at position 1 and not at position 0.


Functions Legend:

  • RIGHT(text,[num_chars])RIGHT returns the last character or characters in a text string, based on the number of characters you specify in the variable num_chars. RIGHT always counts each character, whether single-byte or double-byte, as 1, no matter what the default language setting is.
  • LEN(text)LEN returns the number of characters in a text string. Again, LEN always counts each character, whether single-byte or double-byte, as 1, no matter what the default language setting is.
  • HEX2DEC(number)HEX2DEC converts a hexadecimal number to decimal.
  • SUBSTITUTE(text, old_text, new_text, [instance_num]) – Substitutes new_text for old_text in a text string. You can use SUBSTITUTE when you want to replace specific text in a text string.
  • REPLACE(old_text, start_num, num_chars, new_text)REPLACE replaces part of a text string, based on the number of characters you specify, with a different text string. Use REPLACE when you want to replace any text that occurs in a specific location in a text string. REPLACE always counts each character, whether single-byte or double-byte, as 1, no matter what the default language setting is.

Replace a character in all filenames

The following command will search for files in the current directory (.) that have in their name the colon character :.
The files that match will then be renamed and all instances of the colon character : in the names will be replaced by the full stop character ..

find . -name "*:*" -execdir sh -c 'mv "$1" "${1//:/.}"' _ {} \;
  • -execdir command {} + is like -exec, but the specified command is run from the subdirectory containing the matched file, which is not normally the directory in which you started find.

Example: if you have a file named 2017-03-15 14:34:44.116002523.png then it will be renamed to 2017-03-15

NotePad++: Remove multi-line (/**/) comments automatically 3

We are going to use NotePad++ replace functionality (CTRL+H) to remove all multi-line comments that are enclosed in a pair of /* and */.

While in the source file you want to edit, open up the ‘Replace’ window either from the ‘Search’ menu or by using CTRL+H.

Set ‘Search Mode’ to ‘Regular expression’ and tick the ‘. matches newline’.

In the ‘Find what:’ input type /\*(.*?)\*/\n and leave the ‘Replace with:’ input empty.

NotePad++ Remove Multi-Line CommentsTo find the next comment, you can click the ‘Find Next’ button and then hit ‘Replace’.

To make the change on all comments just hit ‘Replace All’.

Inline replacement of all newlines in file with br tag

In case you have some output you want to add it to an HTML document, you need to make some modifications to it to make it appear properly.

One of them would be to replace the newline characters with the <br> tag.

If you have GNU sed, you can use the -i option, which will do the replacement in place.

sed -i 's/$/<br>/' myTextFile.txt

Otherwise you will have to redirect to another file and rename it over the old one.

sed 's/$/<br>/' myTextFile.txt > myTextFile.txt.tmp && mv myTextFile.txt.tmp myTextFile.txt

If you want to perform this change on the results of another command (because you are redirecting it to an email client like mutt) you can use the following example

someCommand | sed 's/$/<br>/' | someOtherCommand

Replace date in files 1


You have many simple text log files of a system, where the date is formatted using the slash character / and you want to update the dates to some other date.
Usually when using the sed, the slash character is reserved for separating the parts of the expression you want to evaluate.
In this case though, we can go around this limitation by using another symbol as the separator, leaving the slash character available for us to use in our regular expression.


The following example demonstrates just that. You will see that we used the colon character : in the place of the separator allowing us to use the slash character / in the expression.

sed -i 's:2015/01/06:2015/01/15:g' *.log

What we did here was change the character that sed uses to delimit its options with :, this way we could use / as any other character.
All log files in that folder will get automatically updated since we used *.log at file selection parameter.
The -i parameter instructs sed to make all replacements in place. i.e. All files will be modified to reflect the changes, it will not create new ones.