2つのテキストファイル間のリンク変数

2つのテキストファイル間のリンク変数

次の説明は、私が達成したいことだけを示しています。

2つのテキストファイルがあります。最初のテキストファイルにはlog1.txt次のエントリが含まれています。

Black
Blue
Brown
Copper
Cyan
Gold
Gray
Green

2番目のテキストファイルには、log2.txt次のエントリが含まれています。

Ugly
Nice
cool
pretty

両方のテキストを同時に読み、次の出力を生成したいと思います。

The first color Black is Ugly
The second color Blue is Nice
The third color Brown is cool
The fourth color Copper is pretty
The fifth color Cyan is Ugly
The sixth color Gold is Nice
The seventh color Gray is cool
The eighth color Green is pretty

またはを使用して以前のbash出力をどのように取得できますかshell? 2つのループを同時に適用してみました:for loop" and/orwhileループ`しかしうまくいきませんでした!たとえば、次のような厄介なコードを試してみました。

#!/bin/bash
while IFS= read -r line; do
    for ii in $(cat log1.txt); do

echo "The first color "$i "is" $ii

done <log2.txt
done

「最初の色」、「2番目の色」などを変更する方法がわかりません。

答え1

Debianでは、withzshとwithとlibnumbertext-tools一緒にspellout

#! /bin/zsh -
colors=(${(f)"$(<log1.txt)"})
adjectives=(${(f)"$(head -n ${#colors} <log2.txt)"})

/usr/lib/libnumbertext/spellout -l /usr/share/libnumbertext/en \
  -p ordinal 1-$#colors |
for color adjective in ${colors:^^adjectives}; do
  read num &&
  print -r The $num color $color is $adjective
done

(これはアメリカ英語です。例えば101の場合百番目の優先変える百日)

数字を綴るソフトウェアをインストールすることはできませんが、zsh3番目のファイルに英語の序数リストがある場合(少なくともBourne、rc、fishな​​ど)、次のようなほとんどのシェルlog3.txtでこれを行うことができます。bash

#! /bin/sh -
awk '
  BEGIN {while ((getline a < "log2.txt") > 0) adjective[na++] = a}
  {
    if ((getline num < "log3.txt") <= 0) num = NR "th"
    print "The "num" color "$0" is "adjective[(NR-1)%na]
  }' log1.txt

<digits>th(英語の数字が足りない場合は置き換えます)

答え2

シェルは英語を理解していないため、任意の数の正しいサフィックスを含むスペル番号を自動的に生成するには追加の作業が必要です。番号付けに数値のみを使用し、さらにlog1.txtがより長いファイルであると仮定した場合は、次のことを試してください。

#!/bin/bash
log1_length=$(wc -l <log1.txt)
log2_length=$(wc -l <log2.txt)

for i in $(seq $log1_length); do
    arg1=$(head -$i <log1.txt | tail -1)
    arg2=$(head -$(((i-1) % log2_length + 1)) <log2.txt | tail -1)
    echo "Color No. $i $arg1 is $arg2."
done

答え3

あなたが望むことを達成することができますケース制御構造次のように:

#!/bin/bash
log1_length=$(wc -l <log1.txt)
log2_length=$(wc -l <log2.txt)

for i in $(seq $log1_length); do 
    arg1="$(head -$i <log1.txt | tail -1)"
    arg2="$(head -$(((i-1) % log2_length + 1)) <log2.txt | tail -1)"
   # Case control structure to replace digit equivalent in words 
    case ${i} in
        1) echo -n "The first color ";;
        2) echo -n "The second color ";;
        3) echo -n "The third color ";;
        4) echo -n "The fourth color ";;
        5) echo -n "The fifth color ";;
        6) echo -n "The sixth color ";;
        7) echo -n "The seventh color ";;
        8) echo -n "The eighth color ";;
        9) echo -n "The ninth color ";;
       10) echo -n "The tenth color ";;
       11) echo -n "The eleventh color ";;
    esac 
    echo ${i}"$i${arg1} is ${arg2}" |  tr -d '0123456789'   
done

出力は次のとおりです。

The first color Black is Ugly
The second color Blue is Nice
The third color Brown is cool
The fourth color Copper is pretty
The fifth color Cyan is Ugly
The sixth color Gold is Nice
The seventh color Gray is cool
The eighth color Green is pretty

答え4

誰も配列の使用を提案しなかったという事実に驚きました。これはおおよその試みです(log3.txt上記の@Stephaneのアイデアを使用してください)。

#!/bin/bash

nl1=$( wc -l < log1.txt )
nl2=$( wc -l < log2.txt )
nlnums=$( wc -l < nums.txt )

declare -a arr1[$nl1]
declare -a arr2[$nl2]
declare -a nums[$nlnums]

for (( i=0; i < $nl1; i++ ))
do
    read arr1[$i]
done < log1.txt

for (( i=0; i < $nl2; i++ ))
do
    read arr2[$i]
done < log2.txt

for (( i=0; i < $nlnums; i++ ))
do
    read nums[$i]
done < nums.txt

j=0
for (( i=0; i < $nl1; i++ ))
do
    echo "The ${nums[$i]} color ${arr1[$i]} is ${arr2[$j]}"
    j=$(( (j+1) % $nl2 ))
done

ファイルの内容はnums.txt次のとおりです。

first
second
third
fourth
fifth
sixth
seventh
eighth
ninth
tenth

コードはいくつかのクリーンアップが必要ですが、ポイントを示しています。

関連情報