由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Apple版 - 请教脚本和编程高手:关键字自动查找与输出
相关主题
Google nixes Nexus One看看你的retina Macbook Pro 有没有Ghost image
Mac OS X十周岁生日快乐我把我的所有电脑的mouse wheel的方向改了
Lion使用体会-犀利的兼容性用了2年mac还不会用automator???
发一个link, Mac终端常用操作指令,很爽很强大。谁用iwork 出来说一下体会阿
Re: 有适用与Mac的糍粑吗?safari怎么这么烂
如何查找文件夹/子文件夹中的文件的内容?Mac下编译latex的执行脚本
小白好慢,怎么办呢?mac太弱智了!!!
求助:开机后Finder一直出不来推荐一个新的XBMC的add-on
相关话题的讨论汇总
话题: 文件话题: delta话题: apple话题: windows话题: 关键词
进入Apple版参与讨论
1 (共1页)
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
3
fgrep -i -f fileA fileB
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也得先跟大家打个招呼不是。。。
1 (共1页)
进入Apple版参与讨论
相关主题
推荐一个新的XBMC的add-onRe: 有适用与Mac的糍粑吗?
为神马现在看icefilms(atv2)老是‘脚本错误:video.plugin.add-on'如何查找文件夹/子文件夹中的文件的内容?
apple tv 能容易地看很多中文台吗?小白好慢,怎么办呢?
为啥我的icefilms看不了了。。。求助:开机后Finder一直出不来
Google nixes Nexus One看看你的retina Macbook Pro 有没有Ghost image
Mac OS X十周岁生日快乐我把我的所有电脑的mouse wheel的方向改了
Lion使用体会-犀利的兼容性用了2年mac还不会用automator???
发一个link, Mac终端常用操作指令,很爽很强大。谁用iwork 出来说一下体会阿
相关话题的讨论汇总
话题: 文件话题: delta话题: apple话题: windows话题: 关键词