【发布时间】:2020-06-25 04:17:32
【问题描述】:
我有两个文件,即 File_A 和 File_B。 File_A 每行包含一个单词,File_B 包含句子。我必须从 File_A 读取单词并在 File_B 中搜索以该单词开头的行并将整行复制到 File_C。 File_A 和 File_B 都已排序
举例
文件_A:
he
I
there
文件_B:
he was at least equally intrigued by hers.
I guess he's going to use it in his business.
I don't know if he's angry or not.
there were five dogs.
there is fly in my soup.
we don't know what he is doing.
文件_C:
he was at least equally intrigued by hers.
I guess he's going to use it in his business.
I don't know if he's angry or not.
there were five dogs.
there is fly in my soup.
我尝试使用 shell 脚本,但它是启发式方法,因此需要很长时间。 File_A 和 File_B 都是大文件。
这是我尝试过的代码
#! /bin/bash
for first in `cat File_A`
do
while read line
do
first_col=$(echo $line|head -n1 | awk '{print $1;}')
if [[ "$first" == "$first_col" ]]
then
echo $line >> File_C
fi
done <File_B
done
【问题讨论】:
-
如果您在自己解决此问题时遇到具体问题,可以在这里提问。您还应该首先决定要使用哪种编程语言。
-
请展示你的努力:将代码包含在问题中,即使它不起作用。
-
@MichaelButscher 我已经标记了编程语言。
-
@DYZ 这是我做的代码#! /bin/bash for first in
cat File_Ado while read line do first_col=$(echo $line|head -n1 | awk '{print $1;}') if [[ "$first" == "$first_col" ] ] 然后回显 $line >> File_C fi 完成 -
请将其作为问题的一部分。您希望我们在评论中阅读未格式化的 shell 脚本吗?
标签: python python-3.x shell perl sh