Jump to content

Removing special chars from variable

RageAx
Go to solution Solved by tikker,
34 minutes ago, RageAx said:

Can you use iconv on variables? I thought you can only use it on files.

And even if I use it on a file it justs returns '?' and not the respective character

You can make just pipe it in:

username_in="čćšžđ";
username_out=$(echo $username_in | iconv -f UTF-8 -t ASCII//TRANSLIT);
echo 'Input:' $username_in
echo 'Output: ' $username_out

which gives

Input: čćšžđ
Output:  ccszd

Replace UTF-8 with whatever encoding your usernames have. The //TRANSLIT part tries to approximate characters that aren't in the target encoding with similar looking characaters.

I have a variable that I need to remove special characters from to form a username. Since the variable is dynamic I cannot just simply replace the char in one position. I need to replace chars such as "čćšžđ" in upper and lower cases with their ISO counterparts such as "cszd". I have searched the command but I cannot find anything that would suit my needs.

Link to comment
Share on other sites

Link to post
Share on other sites

You'll have to write a small program yourself. Store the letters with diacritics and their normal versions in two constants, say strings and use them to remap each character in the username to its normal counterpart is what I would try.

If you are on linux, then maybe the iconv command can help you; convert it from whatever encoding to plain ASCII.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, tikker said:

Store the letters with diacritics and their normal versions in two constants, say strings and use them to remap each character in the username to its normal counterpart is what I would try.

I did forget to mention I am a complete noob when it comes to Bash. So I didn't understand most of what you just wrote :)

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, RageAx said:

I did forget to mention I am a complete noob when it comes to Bash. So I didn't understand most of what you just wrote :)

Well you didn't mention any OS, environment or language so I gave a general programmatic strategy (didn't notice the tag). See my edited answer. Maybe iconv can help you.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, tikker said:

Well you didn't mention any OS, environment or language so I gave a general programmatic strategy (didn't notice the tag). See my edited answer. Maybe iconv can help you.

Can you use iconv on variables? I thought you can only use it on files.

And even if I use it on a file it justs returns '?' and not the respective character

Link to comment
Share on other sites

Link to post
Share on other sites

maybe your bash environment isnt set up with correct encoding so it cant display what youre converting to

MSI GX660 + i7 920XM @ 2.8GHz + GTX 970M + Samsung SSD 830 256GB

Link to comment
Share on other sites

Link to post
Share on other sites

34 minutes ago, RageAx said:

Can you use iconv on variables? I thought you can only use it on files.

And even if I use it on a file it justs returns '?' and not the respective character

You can make just pipe it in:

username_in="čćšžđ";
username_out=$(echo $username_in | iconv -f UTF-8 -t ASCII//TRANSLIT);
echo 'Input:' $username_in
echo 'Output: ' $username_out

which gives

Input: čćšžđ
Output:  ccszd

Replace UTF-8 with whatever encoding your usernames have. The //TRANSLIT part tries to approximate characters that aren't in the target encoding with similar looking characaters.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

27 minutes ago, tikker said:

The //TRANSLIT part tries to approximate characters that aren't in the target encoding with similar looking characaters.

Horrible idea in languages with similarly looking characters.

Write in C.

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Dat Guy said:

Horrible idea in languages with similarly looking characters.

True, but you wouldn't want to represent those in ASCII then anway, but rather find a different way of handling them.

Crystal: CPU: i7 7700K | Motherboard: Asus ROG Strix Z270F | RAM: GSkill 16 GB@3200MHz | GPU: Nvidia GTX 1080 Ti FE | Case: Corsair Crystal 570X (black) | PSU: EVGA Supernova G2 1000W | Monitor: Asus VG248QE 24"

Laptop: Dell XPS 13 9370 | CPU: i5 10510U | RAM: 16 GB

Server: CPU: i5 4690k | RAM: 16 GB | Case: Corsair Graphite 760T White | Storage: 19 TB

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×