user15885911
user15885911

Reputation:

Listing locales from /usr/share/i18n/locales

I want to (alphabetically) list all locales found in /usr/share/i18n/locales, along with the corresponding language and territory text found in all the locale files. How can get the output to look like this?

en_US - American English, United States
en_GB - British English, United Kingdom

Listing all the locale names is easy:

ls /usr/share/i18n/locales/*

But for the additional information, I tried an awk command, but it is nowhere near what I need:

awk '/language   /||/territory  /{print $2}' /usr/share/i18n/locales/en_US

Upvotes: 0

Views: 1100

Answers (2)

LMC
LMC

Reputation: 12672

Using locale command to list all available locales and then show requested details

#!/bin/bash

for l in $(locale -a); do
    read -r tmp1 lan tmp2 terr < <(LC_IDENTIFICATION="$l" locale -c language -c territory | tr '\n' ' ')
    echo "$l - $lan, $terr"
done

One-liner:

for l in $(locale -a); do read -r tmp1 lan tmp2 terr < <(LC_IDENTIFICATION="$l" locale -c language -c territory | tr '\n' ' '); echo "$l - $lan, $terr"; done

Another version:

#!/bin/bash
  
for l in $(locale -a); do
    LC_IDENTIFICATION="$l" locale -c 'language' -c 'territory' | \
    awk -v loc="$l" '{if(NR==2){ a=$0 }else if(NR==4){ print loc " - " a "," $0 }}'
done

Result:

aa_DJ - Afar,Djibouti
aa_DJ.utf8 - Afar,Djibouti
aa_ER - Afar,Eritrea
aa_ER@saaho - Afar,Eritrea
aa_ET - Afar,Ethiopia
af_ZA - Afrikaans,South Africa
af_ZA.utf8 - Afrikaans,South Africa
agr_PE - Aguaruna,Peru
ak_GH - Akan,Ghana
am_ET - Amharic,Ethiopia
....

Upvotes: 1

KamilCuk
KamilCuk

Reputation: 141060

So something like:

cd /usr/share/i18n/locales/ 
awk '/language   /{l=$2} /territory  /{print FILENAME " - "l ", " $2}' *

You can just "clear" $1 and use $0 that has leading space in it.

awk '$1 == "language"{$1="";l=$0} $1 == "territory"{$1="";print FILENAME " -" l "," $0}' *

Upvotes: 0

Related Questions