以下解释仅代表我想要实现的目标。
我有两个文本文件:第一个文本文件log1.txt
包含以下条目:
Black
Blue
Brown
Copper
Cyan
Gold
Gray
Green
第二个文本文件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
or实现先前的输出shell
?我尝试同时应用两个循环:for loop" and/or
while循环`但没有用!例如,我尝试了这个尴尬的代码:
#!/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
我不知道或不知道如何在“第一种颜色”、“第二种颜色”等之间进行更改
答案1
Debian 上的withzsh
和 with libnumbertext-tools
's 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,您会得到一百个第一代替一百零一)
如果您无法安装zsh
或任何拼写数字的软件,但在第三个log3.txt
文件中有一个英文序数列表,您可以在大多数 shell 中执行此操作,包括bash
(至少像 Bourne、像 rc、fish):
#! /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
您的 shell 不认识英语,因此自动生成带有正确后缀的拼写数字到任意计数将需要一些额外的工作。仅使用数字进行编号并额外假设 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
代码需要稍微清理一下,但说明了这一点。