2011-09-30 86 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 '(?<={)[^:]+(?=:)'

相關問題