2011-09-30 84 views
0

我是新来的bash脚本编写者,我很难找出这个问题。我有大约两百文件遵循此模式:读取文件并将任意值存储到bash脚本中的变量

ANÁLISE DA GLOSA FUNGICIDA 
A ANÁLISE RESULTA EM: 
S='Glosa02626354' = "agente que destrói ou previne o crescimento de fungos" 
    {antifúngico: O I]antifúngico clássico utilizado no tratamento não previne a disseminação típica da infecção., 
    agente antifúngico: Os resultados sugerem a utilização terapêutica do extrato do limão como I]agente antifúngico na Odontologia., 
    fungicida: A duração do ]fungicida no carpete tem garantia de cinco anos., 
    antimicótico: Os grupos nomearam o I]antimicótico e realizaram campanha de lançamento fictícia, com material técnico de divulgação e brindes., 
    agente antimicótico: Em caso de infecção, deverá ser instituído o uso de um I]agente antimicótico.} 

Chave: FUNGICIDA <noun.artifact> 
ILI: 02626354 
Sense 1 
{02626354} <noun.artifact> antifungal, antifungal agent, fungicide, antimycotic, antimycotic agent -- (any agent that destroys or prevents the growth of fungi) 
     => {13935705} <noun.substance> agent -- (a substance that exerts some force or effect) 
      => {00005598} <noun.Tops> causal agent, cause, causal agency -- (any entity that causes events to happen) 
       => {00001740} <noun.Tops> entity -- (that which is perceived or known or inferred to have its own distinct existence (living or nonliving)) 

在这种情况下,我必须存储括号之间以下值:“antifúngico”,“AGENTEantifúngico”,“fungicida”,“antimicótico”和' agenteantimicótico'在一个变量。这些文字当然会在每个文件中有所不同。作为比较,这里是另一个文件:

ANÁLISE DA GLOSA VIA ÁPIA 
A ANÁLISE RESULTA EM: 
S='Glosa02634922' = "estrada da antiga Roma, na Itália, extendendo-se ao sul, de Roma a Brindisi; iniciada em 312 AC" 
    {Via Ápia: Toda a I]Via Apia era conhecida quer pela sua extensão, quer pela sua extraordinária beleza.} 

Chave: VIA ÁPIA <noun.artifact> 
ILI: 02634922 
Sense 1 
{02634922} <noun.artifact> Appian Way#1 -- (an ancient Roman road in Italy extending south from Rome to Brindisi; begun in 312 BC) 
     => {03390668} <noun.artifact> highway#1, main road#1 -- (a major road for any form of motor transport) 
      => {03941718} <noun.artifact> road#1, route#2 -- (an open way (generally public) for travel or transportation) 
       => {04387207} <noun.artifact> way#6 -- (any artifact consisting of a road or path affording passage from one place to another; "he said he was looking for the way out") 
        => {00019244} <noun.Tops> artifact#1, artefact#1 -- (a man-made object taken as a whole) 
         => {00016236} <noun.Tops> object#1, physical object#1 -- (a tangible and visible entity; an entity that can cast a shadow; "it was full of rackets, balls and other objects") 
          => {00001740} <noun.Tops> entity#1 -- (that which is perceived or known or inferred to have its own distinct existence (living or nonliving)) 
         => {00002645} <noun.Tops> whole#2, whole thing#1, unit#6 -- (an assemblage of parts that is regarded as a single entity; "how big is that part compared to the whole?"; "the team is a unit") 
          => {00016236} <noun.Tops> object#1, physical object#1 -- (a tangible and visible entity; an entity that can cast a shadow; "it was full of rackets, balls and other objects") 
           => {00001740} <noun.Tops> entity#1 -- (that which is perceived or known or inferred to have its own distinct existence (living or nonliving)) 

这里,变量将只有一个值,字符串'ViaÁpia'。


更新:我发现了一种方法挑出是相关使用一些正则表达式魔法行:

grep ':*\.,' file_name.txt 

此命令的用于第一示例的输出是

{antifúngico: O I]antifúngico clássico utilizado no tratamento não previne a disseminação típica da infecção., 
    agente antifúngico: Os resultados sugerem a utilização terapêutica do extrato do limão como I]agente antifúngico na Odontologia., 
    fungicida: A duração do ]fungicida no carpete tem garantia de cinco anos., 
    antimicótico: Os grupos nomearam o I]antimicótico e realizaram campanha de lançamento fictícia, com material técnico de divulgação e brindes., 
+0

你能提供一个具体的例子吗?在我看来,像这样的东西:'x = $(cat file | grep )'可以工作,但很难说你的问题缺乏细节。 – Kevin

+0

@凯文,我从文件中提取了真实的例子。我首先想到把所有内容放在一起太混乱。 – rberaldo

+0

@rberaldo我想我明白你想要什么。你能添加一个(实际)所需输出格式的例子吗?另外,第一个定义是否真的有''''和''''''''符号? –

回答

0

如果你只是想把你的正则表达式的匹配结果赋值给bash中的一个变量,那么这应该这样做:

myVar=$(cat file_name.txt|grep ':*\.,') 

编辑:

这可能让你有点接近:

myVar=$(cat file_name.txt|grep ':*\.,'|./x.pl) 

其中x.pl是:

#!/usr/bin/perl 

while (<STDIN>) { 
    my @x = split /,/; 

    foreach (@x) { 
     print $1 . "\n" if /\{?\W*(.*?)\:/; 
    } 
} 

这将提取您想要的4个字,分离通过换行符。我仍然不完全清楚,如果这是你想要的,但。

+0

这给了我几乎我想要的。我的变量应该只有冒号前的内容。 – rberaldo

+0

我刚刚从你的问题中复制了正则表达式...是不是你想要的? – Kevin

+0

不,我只需要这些行的一些单词,更具体地说就是每行中冒号前面的单词。在我给出的例子中,我只需要'antifúngico','agenteantifúngico','fungicida'和'antimicótico'这两个字。 – rberaldo

0

如果你有GNU grep,你可能有好运grep -Po '(?<={)[^:]+(?=:)'

相关问题