How-to remove non-ascii characters and append a space in the field where the non-ascii characters were using a Perl one-liner?
formatting, perl, regex
Solution
Take out 2 non-ascii, add one space after field. Uses non-ascii and 3 spaces as delimiter pairs.
# s/[^[:ascii:]]{2}(.*?[ ]{3})/$1 /g
[^[:ascii:]]{2}
( .*? [ ]{3} )
Perl test case
$/ = undef;
$str = <DATA>;
$str =~ s/[^[:ascii:]]{2}(.*?[ ]{3})/$1 /g;
print $str;
__DATA__
SPAM EATER PO BOX 5555 FAKE STREET
FOO BAR ìPO BOX 1234 LOLLERCOASTER VILLAGE
LOL MAN PO BOX 9876 NEXT DOOR
Output >>
SPAM EATER PO BOX 5555 FAKE STREET
FOO BAR PO BOX 1234 LOLLERCOASTER VILLAGE
LOL MAN PO BOX 9876 NEXT DOOR
Problem
Hi Stack Overflow community, I have the following problem. I got this file called `bad`, with the following contents: ``` SPAM EATER PO BOX 5555 FAKE STREET FOO BAR ìPO BOX 1234 LOLLERCOASTER VILLAGE LOL MAN PO BOX 9876 NEXT DOOR ``` I want to remove the non-ascii character from it (at the start of the second column of the second record), in order to get a file free of strange characters and with all its columns aligned. Plus, there's this one requirement to achieve this using a Perl one-liner - so, no `awk`, `sed`, or alike commands can be used. I tried the following, but got short by one space in the third column: ``` $ perl -plne 's/[^[:ascii:]]//g' bad > bad.clean $ cat bad.clean SPAM EATER PO BOX 5555 FAKE STREET FOO BAR PO BOX 1234 LOLLERCOASTER VILLAGE LOL MAN PO BOX 9876 NEXT DOOR ``` I also tried using the same one-liner, but this time replacing the non-ascii character by a space. In this case, the record ended up with two extra spaces in the second column, and one extra space in the third: ``` $ perl -plne 's/[^[:ascii:]]/ /g' bad > bad.clean.space $ cat bad.clean.space SPAM EATER PO BOX 5555 FAKE STREET FOO BAR PO BOX 1234 LOLLERCOASTER VILLAGE LOL MAN PO BOX 9876 NEXT DOOR ``` Somehow, the non-ascii character seems to be taking 2 bytes instead of one - Is this correct, or am I missing something? The expected output is this: ``` SPAM EATER PO BOX 5555 FAKE STREET FOO BAR PO BOX 1234 LOLLERCOASTER VILLAGE LOL MAN PO BOX 9876 NEXT DOOR ``` Is there a way, using a Perl one-liner, to get the results as expected? I was thinking of a way to add one space after removing the non-ascii character, in the field in which the change has been made, but I can't find the way to do it. In addition, the non-ascii character can appear on any field, not only in the second one. By the way, some info that might be useful: This is an `AIX` machine, running `Perl v5.8.8`. Thank you! Edit: As @ThisSuitIsBlackNot mentions, there are two non-ascii characters. Therefore, I guess I just want to add one space to the end of that field, if at least one non-ascii character gets removed by the command. Is there a way to get this extra space included in the same sentence, so it can be done as a one-liner as well? Edit: After reviewing a large set of data, I can tell that the non-ascii characters always appears as pairs, and the next field in the original file (before running the one-liner) is always one space to the right compared to the other columns. So, I'm changing the title of this question to match the requirement: Perl one-liner to remove non-ascii characters and append a space in the field where the non-ascii characters were