1

I would like to create a PHP array from the Serbian alphabet.

Out of

а б в г д ђ е ж з и ј к л љ м н њ о п р с т ћ у ф х ц ч џ ш
a b c č ć d dž đ e f g h i j k l lj m n nj o p r s š t u v z ž

should become this (a custom array):

'а': 'a',
'б': 'b',
'в': 'c',
'г': 'č',
'д': 'ć',

How can I do this with awk or sed?

4
  • 2
    The correspondence between the two sets is off though... Jul 31, 2018 at 19:46
  • @don_crissti by number of characters yes, but by "words" - ie, space separated items, no the same number. Some of teh single characters in upper line are represented by 2 characters in lower line.
    – ivanivan
    Aug 1, 2018 at 14:58
  • 1
    @ivanivan - I'm not talking about the no. of "words" I'm talking about the correspondence between them, if you read the question and check the expected output (including the answers below) you'll understand... Aug 1, 2018 at 15:53
  • Luis, if any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Nov 12, 2018 at 1:26

7 Answers 7

2

This gets pretty close:

$ awk 'NR==1 { for( i=1; i<=NF; i++ ) {out[i][0]=$i } } NR==2 { for( i=1; i<=NF; i++) {out[i][1]=$i }  } END {for( i=1; i<=NF; i++ ) { print "\""out[i][0]"\":\""out[i][1]"\","}}' input
"а":"a",
"б":"b",
"в":"c",
"г":"č",
"д":"ć",
"ђ":"d",
"е":"dž",
"ж":"đ",
"з":"e",
"и":"f",
"ј":"g",
"к":"h",
"л":"i",
"љ":"j",
"м":"k",
"н":"l",
"њ":"lj",
"о":"m",
"п":"n",
"р":"nj",
"с":"o",
"т":"p",
"ћ":"r",
"у":"s",
"ф":"š",
"х":"t",
"ц":"u",
"ч":"v",
"џ":"z",
"ш":"ž",
2

With zsh using its ${a:^b} array zipping operator:

{read -A a; read -A b} < your-file-with-those-2-lines.txt
printf "'%s': '%s',\n" ${a:^b}
1

In bash, with parallel arrays:

$ read -a src < <(sed -n 1p input)
$ read -a dest < <(sed -n 2p input)
$ for((i=0; i < "${#src[@]}"; i++))
do
  printf "'%s': '%s',\n" "${src[i]}" "${dest[i]}"
done

The output is:

'а': 'a',
'б': 'b',
'в': 'c',
'г': 'č',
'д': 'ć',
'ђ': 'd',
'е': 'dž',
'ж': 'đ',
'з': 'e',
'и': 'f',
'ј': 'g',
'к': 'h',
'л': 'i',
'љ': 'j',
'м': 'k',
'н': 'l',
'њ': 'lj',
'о': 'm',
'п': 'n',
'р': 'nj',
'с': 'o',
'т': 'p',
'ћ': 'r',
'у': 's',
'ф': 'š',
'х': 't',
'ц': 'u',
'ч': 'v',
'џ': 'z',
'ш': 'ž',
1

Using AWK:

awk '                                  
BEGIN { OFS="'"': '"'";start=1 }       
NR>=start && $0!="" {                  
    for(i=1;i<=NF;i++)                 
        a[i]=a[i] (NR==start?"":OFS) $i
    }
END {                                  
    for(j=1;j<i;j++)                   
        print "'"'"'"a[j]"'"',"'"                     
}' file.txt

Output

'а': 'a',
'б': 'b',
'в': 'c',
'г': 'č',
'д': 'ć',
0
1
printf  "'%s' : '%s',\n" $(datamash -W transpose < input.txt)

Input

а б в г д ђ е ж з и ј к л љ м н њ о п р с т ћ у ф х ц ч џ ш
a b c č ć d dž đ e f g h i j k l lj m n nj o p r s š t u v z ž

Output

'а' : 'a',
'б' : 'b',
'в' : 'c',
'г' : 'č',
'д' : 'ć',
'ђ' : 'd',
'е' : 'dž',
'ж' : 'đ',
'з' : 'e',
'и' : 'f',
'ј' : 'g',
'к' : 'h',
'л' : 'i',
'љ' : 'j',
'м' : 'k',
'н' : 'l',
'њ' : 'lj',
'о' : 'm',
'п' : 'n',
'р' : 'nj',
'с' : 'o',
'т' : 'p',
'ћ' : 'r',
'у' : 's',
'ф' : 'š',
'х' : 't',
'ц' : 'u',
'ч' : 'v',
'џ' : 'z',
'ш' : 'ž',
1

You ask for awk but you want to create PHP array, so I figured I'd give you a PHP way of doing it - and yes, you can run PHP from command line...

Stick your 2 lines into a file

а б в г д ђ е ж з и ј к л љ м н њ о п р с т ћ у ф х ц ч џ ш
a b c č ć d dž đ e f g h i j k l lj m n nj o p r s š t u v z ž

And then read that file with file() which brings each line into an element in an array.

Then loop through that array, breaking each line into its own array based on space separation using explode()

Then loop through your 2 lines and rebuild an associative array.

I do notice the last character in the first line seems to bring along a new line character, but that could be an encoding issue... not familiar enough with working with foreign character sets.

<?php

$letters=file("letters");

for($i=0;$i<count($letters);$i++){
    $line[$i]=explode(" ",$letters[$i]);
}

for($i=0;$i<count($line[1]);$i++){
        $newArray[$line[0][$i]]=$line[1][$i];
}

print_r($newArray);

?>
0
awk 'NR==1 { printf "%s\n",'$0' }' input.txt | tr ' ' '\n' &amp;gt; a.txt
awk 'NR== 2{ printf "%s\n",'$0' }' input.txt | tr ' ' '\n' &amp;gt;. b.txt
paste -d : a b

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .