p*******9 发帖数: 1860 | 1 有一个问题请教论坛里的脚本语言和编程高手。
今有两个文件:文件A里面有100个关键词,文件B里面有5000行的文本。任务是在文件B
中查找文件A里的关键词,如果文件A里的关键词出现在文件B中,则把该关键词输入到
文件C中。请问如何实现?
文件A(关键词):
----------------------------------------
Apple
Banana
Can
Delta
...
----------------------------------------
文件B(文本):
----------------------------------------
define: apple
get 100 Delta
...
-----------------------------------------
文件C(结果):
-----------------------------------------
Apple
Delta
----------------------------------------- |
c***s 发帖数: 192 | 2 用awk或python。
件B
【在 p*******9 的大作中提到】 : 有一个问题请教论坛里的脚本语言和编程高手。 : 今有两个文件:文件A里面有100个关键词,文件B里面有5000行的文本。任务是在文件B : 中查找文件A里的关键词,如果文件A里的关键词出现在文件B中,则把该关键词输入到 : 文件C中。请问如何实现? : 文件A(关键词): : ---------------------------------------- : Apple : Banana : Can : Delta
|
q****q 发帖数: 9 | |
p*******9 发帖数: 1860 | 4 grep -i -of fileA.txt fileB.txt > fileC.txt
用Windows Notepad 打开,发现关键词都挤到一块了,请问如何分行,或者空格?
fileC.txt
-------------------
appleDelta
------------------- |
r*****o 发帖数: 636 | 5 一定要Windows么?在Linux下面,试试这个
grep -f file_A.txt file_B.txt > file_C.txt
【在 p*******9 的大作中提到】 : grep -i -of fileA.txt fileB.txt > fileC.txt : 用Windows Notepad 打开,发现关键词都挤到一块了,请问如何分行,或者空格? : fileC.txt : ------------------- : appleDelta : -------------------
|
l*********s 发帖数: 5409 | 6 use a better editor that is unix-format aware.
【在 p*******9 的大作中提到】 : grep -i -of fileA.txt fileB.txt > fileC.txt : 用Windows Notepad 打开,发现关键词都挤到一块了,请问如何分行,或者空格? : fileC.txt : ------------------- : appleDelta : -------------------
|
p*******9 发帖数: 1860 | 7 用VIM发现问题消失了,换行正确。
但如果关键词在fileB中重复出现的话,输出结果也会重复出现关键字。
请问如何避免这种重复?
文件A(关键词):
----------------------------------------
Apple
Banana
Can
Delta
...
----------------------------------------
文件B(文本):
----------------------------------------
define: apple
get 100 Delta
define: apple
get 100 Delta
...
-----------------------------------------
文件C(结果):
-----------------------------------------
apple
Delta
apple
Delta
----------------------------------------- |
S**I 发帖数: 15689 | 8 grep -i -of fileA.txt fileB.txt | sort | uniq > fileC.txt
【在 p*******9 的大作中提到】 : 用VIM发现问题消失了,换行正确。 : 但如果关键词在fileB中重复出现的话,输出结果也会重复出现关键字。 : 请问如何避免这种重复? : 文件A(关键词): : ---------------------------------------- : Apple : Banana : Can : Delta : ...
|
p*******9 发帖数: 1860 | 9 出错了,uniq不认识。
Microsoft Windows [Version 6.1.7601]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Windows\system32>cd..
C:\Windows>cd..
C:\>D:
D:\> grep -i -of fileA.txt fileB.txt | sort | uniq > fileC.txt
'uniq' is not recognized as an internal or external command,
operable program or batch file.
======================================
已经搞定了,需要安装 CoreUtils for Windows |
S**I 发帖数: 15689 | 10 So you're using Windows; then why you post you question in Apple board?
Unfortunately, Windows batch doesn't have an easy solution for what you
want as *NIX. You probably need at least Windows Powershell.
【在 p*******9 的大作中提到】 : 出错了,uniq不认识。 : Microsoft Windows [Version 6.1.7601] : Copyright (c) 2009 Microsoft Corporation. All rights reserved. : C:\Windows\system32>cd.. : C:\Windows>cd.. : C:\>D: : D:\> grep -i -of fileA.txt fileB.txt | sort | uniq > fileC.txt : 'uniq' is not recognized as an internal or external command, : operable program or batch file. : ======================================
|
l*********s 发帖数: 5409 | 11 probably he is running windows on top of apple hardware.Not uncommon :-),
【在 S**I 的大作中提到】 : So you're using Windows; then why you post you question in Apple board? : Unfortunately, Windows batch doesn't have an easy solution for what you : want as *NIX. You probably need at least Windows Powershell.
|
p**o 发帖数: 3409 | 12 keywords = set(
line.strip() for line in file('A.txt')
)
words = set(
file('B.txt').read().replace('\n', ' ').split(' ')
)
with open('C.txt', 'w') as f:
f.write('\n'.join(keywords & words))
件B
【在 p*******9 的大作中提到】 : 有一个问题请教论坛里的脚本语言和编程高手。 : 今有两个文件:文件A里面有100个关键词,文件B里面有5000行的文本。任务是在文件B : 中查找文件A里的关键词,如果文件A里的关键词出现在文件B中,则把该关键词输入到 : 文件C中。请问如何实现? : 文件A(关键词): : ---------------------------------------- : Apple : Banana : Can : Delta
|
l**n 发帖数: 7272 | 13 LOL。兄弟,你用windows也得先跟大家打个招呼不是。。。
【在 p*******9 的大作中提到】 : 出错了,uniq不认识。 : Microsoft Windows [Version 6.1.7601] : Copyright (c) 2009 Microsoft Corporation. All rights reserved. : C:\Windows\system32>cd.. : C:\Windows>cd.. : C:\>D: : D:\> grep -i -of fileA.txt fileB.txt | sort | uniq > fileC.txt : 'uniq' is not recognized as an internal or external command, : operable program or batch file. : ======================================
|
w********2 发帖数: 16371 | 14 应该罚包子。
【在 l**n 的大作中提到】 : LOL。兄弟,你用windows也得先跟大家打个招呼不是。。。
|