简体   繁体   English

优化sed grep regex shell脚本

[英]Optimising a sed grep regex shell script

I have managed to create a working shell script (OS X) that seems to do what I want it to do (with little to no prior experience with shell). 我设法创建了一个工作的shell脚本(OS X),该脚本似乎可以完成我想要的工作(以前很少或几乎没有shell经验)。 However, even though it seems to work, it does so rather slowly -- so I'm wondering if there are any obvious tweaks that would optimize performance. 但是,即使它似乎起作用,它的运行速度也相当缓慢-所以我想知道是否有任何明显的调整可以优化性能。

So basically what the code does is that it passes a file through a python script to a REST API, and then it parses the resulting text (gets the 'lemma'-s out of it and assembles them into a continous string). 因此,基本上,代码的作用是将文件通过python脚本传递给REST API,然后解析结果文本(从中获取“引理”,并将它们组装成连续的字符串)。

The text that we get back from the REST API looks something like this: 我们从REST API中获得的文本如下所示:

filename
form wsafter anas
Szekó_József " " []
fideszes " " [{"lemma": "fideszes", "tag": "[/Adj][Nom]", "morphana": "Fidesz[/N]=fidesz+es[_Adjz:s/Adj]=es+[Nom]=", "readable": "Fidesz[/N]=fidesz + es[_Adjz:s/Adj] + [Nom]", "twolevel": "f:F i:i d:d e:e s:s z:z :[/N] e:e s:s :[_Adjz:s/Adj] :[Nom]"}, {"lemma": "fideszes", "tag": "[/N][Nom]", "morphana": "Fidesz[/N]=fidesz+es[_Nz:s/N]=es+[Nom]=", "readable": "Fidesz[/N]=fidesz + es[_Nz:s/N] + [Nom]", "twolevel": "f:F i:i d:d e:e s:s z:z :[/N] e:e s:s :[_Nz:s/N] :[Nom]"}]
polgármester " " [{"lemma": "polgármester", "tag": "[/N][Nom]", "morphana": "polgár[/N]=polgár+mester[/N]=mester+[Nom]=", "readable": "polgár[/N] + mester[/N] + [Nom]", "twolevel": "p:p o:o l:l g:g á:á r:r :[/N] m:m e:e s:s t:t e:e r:r :[/N] :[Nom]"}, {"lemma": "polgármester", "tag": "[/N][Nom]", "morphana": "polgármester[/N]=polgármester+[Nom]=", "readable": "polgármester[/N] + [Nom]", "twolevel": "p:p o:o l:l g:g á:á r:r m:m e:e s:s t:t e:e r:r :[/N] :[Nom]"}]
tragikus " " [{"lemma": "tragikus", "tag": "[/Adj][Nom]", "morphana": "tragikus[/Adj]=tragikus+[Nom]=", "readable": "tragikus[/Adj] + [Nom]", "twolevel": "t:t r:r a:a g:g i:i k:k u:u s:s :[/Adj] :[Nom]"}, {"lemma": "tragikus", "tag": "[/N][Nom]", "morphana": "tragikus[/N]=tragikus+[Nom]=", "readable": "tragikus[/N] + [Nom]", "twolevel": "t:t r:r a:a g:g i:i k:k u:u s:s :[/N] :[Nom]"}]
halála " " [{"lemma": "halál", "tag": "[/N][Poss.3Sg][Nom]", "morphana": "halál[/N]=halál+a[Poss.3Sg]=a+[Nom]=", "readable": "halál[/N] + a[Poss.3Sg] + [Nom]", "twolevel": "h:h a:a l:l á:á l:l :[/N] a:a :[Poss.3Sg] :[Nom]"}]
után " " [{"lemma": "után", "tag": "[/Post]", "morphana": "után[/Post]=után", "readable": "után[/Post]", "twolevel": "u:u t:t á:á n:n :[/Post]"}]
egy " " [{"lemma": "egy", "tag": "[/Det|Art.NDef]", "morphana": "egy[/Det|Art.NDef]=egy", "readable": "egy[/Det|Art.NDef]", "twolevel": "e:e g:g y:y :[/Det|Art.NDef]"}, {"lemma": "egy", "tag": "[/Num][Nom]", "morphana": "egy[/Num]=egy+[Nom]=", "readable": "egy[/Num] + [Nom]", "twolevel": "e:e g:g y:y :[/Num] :[Nom]"}, {"lemma": "egy", "tag": "[/N|Pro][Nom]", "morphana": "egy[/N|Pro]=egy+[Nom]=", "readable": "egy[/N|Pro] + [Nom]", "twolevel": "e:e g:g y:y :[/N|Pro] :[Nom]"}]
polgármester-jelölt " " [{"lemma": "polgármester-jelöl", "tag": "[/V][Pst.NDef.3Sg]", "morphana": "polgár[/N]=polgár+mester[/N]=mester+[Nom]=+-[Hyph:Hyph]=-+jelöl[/V]=jelöl+t[Pst.NDef.3Sg]=t", "readable": "polgár[/N] + mester[/N] + [Nom] + -[Hyph:Hyph] + jelöl[/V] + t[Pst.NDef.3Sg]", "twolevel": "p:p o:o l:l g:g á:á r:r :[/N] m:m e:e s:s t:t e:e r:r :[/N] :[Nom] -:- :[Hyph:Hyph] j:j e:e l:l ö:ö l:l :[/V] t:t :[Pst.NDef.3Sg]"}, {"lemma": "polgármester-jelölt", "tag": "[/Adj][Nom]", "morphana": "polgár[/N]=polgár+mester[/N]=mester+[Nom]=+-[Hyph:Hyph]=-+jelöl[/V]=jelöl+t[_PerfPtcp/Adj]=t+[Nom]=", "readable": "polgár[/N] + mester[/N] + [Nom] + -[Hyph:Hyph] + jelöl[/V] + t[_PerfPtcp/Adj] + [Nom]", "twolevel": "p:p o:o l:l g:g á:á r:r :[/N] m:m e:e s:s t:t e:e r:r :[/N] :[Nom] -:- :[Hyph:Hyph] j:j e:e l:l ö:ö l:l :[/V] t:t :[_PerfPtcp/Adj] :[Nom]"}, {"lemma": "polgármester-jelölt", "tag": "[/N][Nom]", "morphana": "polgár[/N]=polgár+mester[/N]=mester+[Nom]=+-[Hyph:Hyph]=-+jelölt[/N]=jelölt+[Nom]=", "readable": "polgár[/N] + mester[/N] + [Nom] + -[Hyph:Hyph] + jelölt[/N] + [Nom]", "twolevel": "p:p o:o l:l g:g á:á r:r :[/N] m:m e:e s:s t:t e:e r:r :[/N] :[Nom] -:- :[Hyph:Hyph] j:j e:e l:l ö:ö l:l t:t :[/N] :[Nom]"}, {"lemma": "polgármester-jelöl", "tag": "[/V][Pst.NDef.3Sg]", "morphana": "polgármester[/N]=polgármester+[Nom]=+-[Hyph:Hyph]=-+jelöl[/V]=jelöl+t[Pst.NDef.3Sg]=t", "readable": "polgármester[/N] + [Nom] + -[Hyph:Hyph] + jelöl[/V] + t[Pst.NDef.3Sg]", "twolevel": "p:p o:o l:l g:g á:á r:r m:m e:e s:s t:t e:e r:r :[/N] :[Nom] -:- :[Hyph:Hyph] j:j e:e l:l ö:ö l:l :[/V] t:t :[Pst.NDef.3Sg]"}, {"lemma": "polgármester-jelölt", "tag": "[/Adj][Nom]", "morphana": "polgármester[/N]=polgármester+[Nom]=+-[Hyph:Hyph]=-+jelöl[/V]=jelöl+t[_PerfPtcp/Adj]=t+[Nom]=", "readable": "polgármester[/N] + [Nom] + -[Hyph:Hyph] + jelöl[/V] + t[_PerfPtcp/Adj] + [Nom]", "twolevel": "p:p o:o l:l g:g á:á r:r m:m e:e s:s t:t e:e r:r :[/N] :[Nom] -:- :[Hyph:Hyph] j:j e:e l:l ö:ö l:l :[/V] t:t :[_PerfPtcp/Adj] :[Nom]"}, {"lemma": "polgármester-jelölt", "tag": "[/N][Nom]", "morphana": "polgármester[/N]=polgármester+[Nom]=+-[Hyph:Hyph]=-+jelölt[/N]=jelölt+[Nom]=", "readable": "polgármester[/N] + [Nom] + -[Hyph:Hyph] + jelölt[/N] + [Nom]", "twolevel": "p:p o:o l:l g:g á:á r:r m:m e:e s:s t:t e:e r:r :[/N] :[Nom] -:- :[Hyph:Hyph] j:j e:e l:l ö:ö l:l t:t :[/N] :[Nom]"}]
maradt " " [{"lemma": "marad", "tag": "[/V][Pst.NDef.3Sg]", "morphana": "marad[/V]=marad+t[Pst.NDef.3Sg]=t", "readable": "marad[/V] + t[Pst.NDef.3Sg]", "twolevel": "m:m a:a r:r a:a d:d :[/V] t:t :[Pst.NDef.3Sg]"}, {"lemma": "maradt", "tag": "[/Adj][Nom]", "morphana": "marad[/V]=marad+t[_PerfPtcp/Adj]=t+[Nom]=", "readable": "marad[/V] + t[_PerfPtcp/Adj] + [Nom]", "twolevel": "m:m a:a r:r a:a d:d :[/V] t:t :[_PerfPtcp/Adj] :[Nom]"}]
a " " [{"lemma": "a", "tag": "[/Det|Art.Def]", "morphana": "a[/Det|Art.Def]=a", "readable": "a[/Det|Art.Def]", "twolevel": "a:a :[/Det|Art.Def]"}, {"lemma": "a", "tag": "[/Det|Pro|(Post)]", "morphana": "a[/Det|Pro|(Post)]=a", "readable": "a[/Det|Pro|(Post)]", "twolevel": "a:a :[/Det|Pro|(Post)]"}, {"lemma": "a", "tag": "[/N][Nom]", "morphana": "a[/N|Ltr]=a+[Nom]=", "readable": "a[/N|Ltr] + [Nom]", "twolevel": "a:a :[/N|Ltr] :[Nom]"}, {"lemma": "a", "tag": "[/N|Pro|(Post)][Nom]", "morphana": "a[/N|Pro|(Post)]=a+[Nom]=", "readable": "a[/N|Pro|(Post)] + [Nom]", "twolevel": "a:a :[/N|Pro|(Post)] :[Nom]"}]
Baranya " " [{"lemma": "Baranya", "tag": "[/N][Nom]", "morphana": "Baranya[/N]=Baranya+[Nom]=", "readable": "Baranya[/N] + [Nom]", "twolevel": "B:B a:a r:r a:a n:n y:y a:a :[/N] :[Nom]"}]
megyei " " [{"lemma": "megyei", "tag": "[/Adj][Nom]", "morphana": "megye[/N]=megye+i[_Adjz:i/Adj]=i+[Nom]=", "readable": "megye[/N] + i[_Adjz:i/Adj] + [Nom]", "twolevel": "m:m e:e g:g y:y e:e :[/N] i:i :[_Adjz:i/Adj] :[Nom]"}, {"lemma": "megyei", "tag": "[/Adj][Nom]", "morphana": "megyei[/Adj]=megyei+[Nom]=", "readable": "megyei[/Adj] + [Nom]", "twolevel": "m:m e:e g:g y:y e:e i:i :[/Adj] :[Nom]"}]
településen "" [{"lemma": "település", "tag": "[/N][Supe]", "morphana": "telep[/N]=telep+ül[/V]=ül+és[_Ger/N]=és+en[Supe]=en", "readable": "telep[/N] + ül[/V] + és[_Ger/N] + en[Supe]", "twolevel": "t:t e:e l:l e:e p:p :[/N] ü:ü l:l :[/V] é:é s:s :[_Ger/N] e:e n:n :[Supe]"}, {"lemma": "település", "tag": "[/N][Supe]", "morphana": "telep[/N]=telep+ülés[/N]=ülés+en[Supe]=en", "readable": "telep[/N] + ülés[/N] + en[Supe]", "twolevel": "t:t e:e l:l e:e p:p :[/N] ü:ü l:l é:é s:s :[/N] e:e n:n :[Supe]"}, {"lemma": "település", "tag": "[/N][Supe]", "morphana": "települ[/V]=települ+és[_Ger/N]=és+en[Supe]=en", "readable": "települ[/V] + és[_Ger/N] + en[Supe]", "twolevel": "t:t e:e l:l e:e p:p ü:ü l:l :[/V] é:é s:s :[_Ger/N] e:e n:n :[Supe]"}, {"lemma": "település", "tag": "[/N][Supe]", "morphana": "település[/N]=település+en[Supe]=en", "readable": "település[/N] + en[Supe]", "twolevel": "t:t e:e l:l e:e p:p ü:ü l:l é:é s:s :[/N] e:e n:n :[Supe]"}]
, " " [{"lemma": ",", "tag": "[Punct]", "morphana": "", "readable": "", "twolevel": ""}]
a " " [{"lemma": "a", "tag": "[/Det|Art.Def]", "morphana": "a[/Det|Art.Def]=a", "readable": "a[/Det|Art.Def]", "twolevel": "a:a :[/Det|Art.Def]"}, {"lemma": "a", "tag": "[/Det|Pro|(Post)]", "morphana": "a[/Det|Pro|(Post)]=a", "readable": "a[/Det|Pro|(Post)]", "twolevel": "a:a :[/Det|Pro|(Post)]"}, {"lemma": "a", "tag": "[/N][Nom]", "morphana": "a[/N|Ltr]=a+[Nom]=", "readable": "a[/N|Ltr] + [Nom]", "twolevel": "a:a :[/N|Ltr] :[Nom]"}, {"lemma": "a", "tag": "[/N|Pro|(Post)][Nom]", "morphana": "a[/N|Pro|(Post)]=a+[Nom]=", "readable": "a[/N|Pro|(Post)] + [Nom]", "twolevel": "a:a :[/N|Pro|(Post)] :[Nom]"}]
szocialista " " [{"lemma": "szocialista", "tag": "[/Adj][Nom]", "morphana": "szocialista[/Adj]=szocialista+[Nom]=", "readable": "szocialista[/Adj] + [Nom]", "twolevel": "s:s z:z o:o c:c i:i a:a l:l i:i s:s t:t a:a :[/Adj] :[Nom]"}, {"lemma": "szocialista", "tag": "[/N][Nom]", "morphana": "szocialista[/N]=szocialista+[Nom]=", "readable": "szocialista[/N] + [Nom]", "twolevel": "s:s z:z o:o c:c i:i a:a l:l i:i s:s t:t a:a :[/N] :[Nom]"}]
Csorbai_Ferenc "" []
, " " [{"lemma": ",", "tag": "[Punct]", "morphana": "", "readable": "", "twolevel": ""}]
( "" [{"lemma": "(", "tag": "[Punct]", "morphana": "", "readable": "", "twolevel": ""}]

The expected result: 预期结果:

Szekó_József fideszes polgármester tragikus halál után egy polgármester-jelöl marad a Baranya megyei település , a szocialista Csorbai_Ferenc , ( 

Here is the script itself: 这是脚本本身:

#! /bin/bash

CV=1
for file in /Users/balintkis/tdk/budapest-onk-valasztas/bigramizalt-nev-support/*
do
    echo $file
    tempfilename="/Users/balintkis/tdk/budapest-onk-valasztas/bigramizalt-nev-support-result/temp$CV.txt"

    szoveg=$(python3 docker-py.py $file) 

    echo "$szoveg" >> $tempfilename
    sed -E -i "" "1,2d" $tempfilename
    sed -i "" '/^[[:space:]]*$/d' $tempfilename

    while IFS= read -r line
    do
        lemmacount=$(echo "$line" | egrep -c '\[{\"lemma\": \".*?\"')
        if [ "$lemmacount" -ge "1" ]
        then
            sp0=$(echo "$line" | egrep -o '\[{\"lemma\": \".*?\"')
            sp=${sp0:12}
            sp=${sp%?}
            sp=$sp" "
            echo -n "$sp" >> /Users/balintkis/tdk/budapest-onk-valasztas/bigramizalt-nev-support-result/kiir$CV.txt
        else
            sp=$(echo "$line" | sed -E 's|.*\[\](.*)\[\/.*|\1|g')
            sp=$(echo $sp | sed -E 's|\s*(.*)\s*|\1|g')
            echo -n "$sp " >> /Users/balintkis/tdk/budapest-onk-valasztas/bigramizalt-nev-support-result/kiir$CV.txt
       fi
    done < "$tempfilename"

    rm $tempfilename
    CV=$((CV+1))
done

Thanks for any suggestions and please be as explicit with suggestions as you can be (ideally with code suggestions), seeing that I'm all pretty new to this. 感谢您的任何建议,请尽可能明确地建议(理想情况下使用代码建议),因为我对此很陌生。 Thanks again! 再次感谢!

I can give you one starting point here, and it's the basic usage of grep : 我可以在这里给您一个起点,它是grep的基本用法:

Imagine you have a file, which looks like this: 假设您有一个文件,看起来像这样:

info1
info2
blabla
info3
more blabla

You want to do something with all lines, containing info . 您想对所有包含info

You seem to do the following (pseudo-code): 您似乎在执行以下操作(伪代码):

while readline(<filename>)
do
  use "grep" to see if the word "info" is on that line. In that case, do something
done

This can be done in an easier way like this ( grep is automatically checking every line in the file): 可以这样简单地完成此操作( grep自动检查文件中的每一行):

grep "info" <filename> | do something

You need to clarify the question - sample output does not match input. 您需要澄清问题-示例输出与输入不匹配。 However, based on some assumptions, using awk to filter/JQ to parse JSON 但是,基于一些假设,使用awk过滤/ JQ解析JSON

cat resp.txt |
    awk 'BEGIN { print "[" } END { print "[] ]" } /lemma/ { sub("^[^[]*. ", ""); print $0, "," }' | 
    jq '.[][].lemma' |
    uniq |
    tr -d '"' |
    tr '\n' ' '

The result looks like below. 结果如下所示。 You should be able to merge them 您应该能够合并它们

fideszes polgármester tragikus halál után egy polgármester-jelöl polgármester-jelölt polgármester-jelöl polgármester-jelölt marad

awk is needed to extract the JSON from the response. 需要awk从响应中提取JSON。 jq to extract the lemma's. jq提取引理。

Consider changing the output file to be JSON only. 考虑将输出文件更改为仅JSON。 It will be much easier to use a single tool (jq) to parse it. 使用单个工具(jq)解析它会容易得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM