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?

  • 2
    The correspondence between the two sets is off though... – don_crissti Jul 31 '18 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 '18 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... – don_crissti Aug 1 '18 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 '18 at 1:26
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",
"ш":"ž",
| improve this answer | |
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}
| improve this answer | |
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',
'ш': 'ž',
| improve this answer | |
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',
'г': 'č',
'д': 'ć',
| improve this answer | |
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',
'ш' : 'ž',
| improve this answer | |
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);

?>
| improve this answer | |
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
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.