자음으로 시작하고 모음으로 끝나는 단어를 텍스트에서 추출합니다.

자음으로 시작하고 모음으로 끝나는 단어를 텍스트에서 추출합니다.

텍스트 파일을 자음으로 시작하고 모음으로 끝나는 단어만 포함하는 다른 텍스트 파일로 변환하여 숫자와 구두점을 제거하는 Linux 쉘 프로그램을 만들어야 합니다.

모음=aoeui 자음=bcdfghjklmnpqrstvwxyz

즉, 원문의 형식은 그대로 유지하고, 요건에 맞지 않는 단어(모음으로 시작하여 자음으로 끝나는), 숫자, 구두점만 제거합니다.

grep둘 중 하나를 시도해 보았지만 sed어떤 결론도 내릴 수 없었습니다.

답변1

POSIX적으로:

consonants=BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz
vowels=AEIOUaeiou

< file tr -cs '[:alpha:]' '[\n*]' |
  grep -x "[$consonants].*[$vowels]"

영어 자음 중 하나로 시작하고 영어 모음 중 하나로 끝나는 모든 알파벳 문자(해당 로케일에서 분류됨)가 보고됩니다.

< file tr -cs '[:alpha:]' '[\n*]' |
  grep -x "[$consonants][$consonants$vowels]*[$vowels]"

영문자만 포함된 내용으로 제한됩니다. (on은 영문자가 아니므 Stéphane로 일치하지 않습니다 .)é허용하다편지).

< file tr -cs "$consonants$vowel" '[\n*]' |
  grep -x "[$consonants].*[$vowels]"

이러한 영어 문자 중 하나가 아닌 모든 문자는 무시됩니다(따라서 find peridicoinside 에서 검색됩니다 periódico).

(일부 tr구현(예: GNU) 은 tr멀티바이트 문자를 지원하지 않으므로 어쨌든 해당 ó/é 문자로 인해 차단됩니다.)

예를 들어:

FooBar Fee123 foo-bar periódico

FreeBSD 시스템( POSIX 가 있는 시스템 tr)의 일반적인 en_US.UTF-8 로케일을 입력하면 3가지 솔루션을 얻을 수 있습니다.

1            2           3

Fee          Fee         Fee
foo          foo         foo
periódico                peri
                         dico

그 중 어느 것도 U+00E9 문자로 입력된 위치와 일치하지 않지만 Blé모두 U+0301 다음 위치에서 결합된 급성 악센트(알파벳 문자 아님)를 찾을 것입니다. 반면 첫 번째 것은 t와 Written과 일치하지 않습니다. 일치하는 선명도를 결합한 형태.éBleBléeStéphane

perl이 문제를 해결하려면 첫 번째 방법 대신 tr필터링하기 전에 결합된 태그를 보존하는 방법을 사용할 수 있습니다 grep.

< file perl -Mopen=locale -pe 's/[^\pL\pM]+/\n/g' |
  grep -x "[$consonants].*[$vowels]"

아니면 모든 작업을 수행합니다 perl.

< file perl -Mopen=locale -lne 'print for
  grep /^[bcdfghj-np-tv-z].*[aeiou]$/i, /[\pL\pM]+/g'

답변2

GNU 사용 grep:

grep -io '\<[bcdfghjklmnpqrstvwxyz][a-z]*[aeiou]\>'

답변3

그리고 grep:

grep -oiw '[bcdfghjklmnpqrstvwxyz][a-z]*[aeiou]'

첫 번째대괄호 표현자음, 두 번째 문자 az 및 마지막 모음과 일치합니다.

답변4

초기 텍스트 형식을 유지하면서 원하는 단어를 필터링하려면 -해결책:

샘플 textfile콘텐츠:

Any delicate you how kindness horrible outlived servants. You high bed wish help call draw side. Girl quit if case mr sing as no have. At none neat am do over will. Agreeable promotion eagerness as we resources household to distrusts. Polite do object at passed it is. Small for ask shade water manor think men begin. 

He oppose at thrown desire of no. Announcing impression unaffected day his are unreserved indulgence. Him hard find read are you sang. Parlors visited noisier how explain pleased his see suppose. Do ashamed assured on related offence at equally totally. Use mile her whom they its. Kept hold an want as he bred of. Was dashwood landlord cheerful husbands two. Estate why theirs indeed him polite old settle though she. In as at regard easily narrow roused adieus. 

So delightful up dissimilar by unreserved it connection frequently. Do an high room so in paid. Up on cousin ye dinner should in. Sex stood tried walls manor truth shy and three his. Their to years so child truth. Honoured peculiar families sensible up likewise by on in. 

일하다:

awk -v IGNORECASE=1 '{ 
       for(i=1;i<=NF;i++) 
           if ($i~/^[bcdfghjklmnpqrstvwxz][a-z]*[aoeui]$/) 
               printf "%s ",$i; print "" 
       }' textfile > newfile

콘텐츠 newfile:

delicate horrible case no none do we to Polite do shade 

He desire see Do mile he polite settle 

So Do so three to so sensible likewise

----------

별도의 줄에서 모든 단어를 필터링하려면 -grep해결책:

grep -woi '[bcdfghjklmnpqrstvwxz][a-z]*[aoeui]' oldfile > newfile
  • -w( --word-regexp) - 일치하는 하위 문자열이 줄의 시작 부분에 있거나 단어를 형성하지 않는 문자가 앞에 있어야 한다는 테스트입니다. 다시 말하지만, 줄 끝에 있어야 하거나 단어를 형성하지 않는 문자가 뒤에 와야 합니다.

관련 정보