简体   繁体   中英

How do I translate a list using a dictionary in bash?

Say I have a dictionary TSV file dict.txt :

apple     pomme
umbrella  parapluie
glass     verre
...       ...

and another file list.txt containing a list of words (from the left column of dict.txt ):

pie
apple
blue
...

I'd like to translate them into the corresponding words from the right column of dict.txt , ie:

tarte
pomme
bleu
...

what is the easiest way to do so?

You can use awk:

awk 'FNR==NR{a[$1]=$2;next} a[$1]{print a[$1]}' dict.txt list.txt

EDIT: If there is a requirement to have multi words (separated by spaces) as word meaning in the dictionary using tab se field separator you can use:

awk -F '\t' 'FNR==NR{a[$1]=$2;next} a[$1]{print a[$1]}' dict.txt list.txt

If you don't have many words (so that everything fits in memory) you can use an associative array:

#!/bin/bash

declare -A english2french=()

# Build dictionary
linenb=0
while ((++linenb)) && IFS=$'\t' read -r en fr; do
    if [[ -z $fr ]] || [[ -z $en ]]; then
        echo "Error line $linenb: one of the two is empty fr=\`$fr' en=\`$en'"
        continue
    fi
    english2french["$en"]=$fr
done < dict.txt

# Translate
linenb=0
while ((++linenb)) && read -r en; do
    [[ -z $en ]] && continue
    fr=${english2french["$en"]}
    if [[ -n $fr ]]; then
        echo "$fr"
    else
        echo >&2 "Error line $linenb: word \`$en' unknown"
    fi
done < list.txt

It seems a bit long, but there are lots of error checks ;) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM