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(英語の数字が足りない場合は置き換えます)

おすすめ記事