首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >打印文件中单词的第一次出现,以字母表中的每个字母开头。

打印文件中单词的第一次出现,以字母表中的每个字母开头。
EN

Stack Overflow用户
提问于 2017-11-20 20:09:33
回答 4查看 338关注 0票数 2

我有一个文件,上面有一个形容词A-Z的列表

如何打印以A开头的第一个单词,然后以B开头的第一个单词.一直到Z?

我想grep可能是解决问题的方法。但对其他人开放,哇,python...other。

一些示例输出:

代码语言:javascript
复制
$ cat adjectives.txt | head
Adamant: unyielding; a very hard substance
Adroit: clever, resourceful
Amatory: sexual
Animistic: quality of recurrence or reversion to earlier form
Antic: clownish, frolicsome
Arcadian: serene
Baleful: deadly, foreboding
Bellicose: quarrelsome (its synonym belligerent can also be a noun)
Bilious: unpleasant, peevish
Boorish: crude, insensitive

$ cat adjectives.txt | grep '^[ABCDE]' | head
Adamant: unyielding; a very hard substance
Adroit: clever, resourceful
Amatory: sexual
Animistic: quality of recurrence or reversion to earlier form
Antic: clownish, frolicsome
Arcadian: serene
Baleful: deadly, foreboding
Bellicose: quarrelsome (its synonym belligerent can also be a noun)
Bilious: unpleasant, peevish
Boorish: crude, insensitive

,所以我的示例输出将是:

代码语言:javascript
复制
Adamant: unyielding; a very hard substance
Baleful: deadly, foreboding
...
Irksome: annoying
Jejune: dull, puerile
...
Wheedling: flattering
Zealous: eager, devoted

文件全文来自 这里

代码语言:javascript
复制
$ cat adjectives.txt
Adamant: unyielding; a very hard substance
Adroit: clever, resourceful
Amatory: sexual
Animistic: quality of recurrence or reversion to earlier form
Antic: clownish, frolicsome
Arcadian: serene
Baleful: deadly, foreboding
Bellicose: quarrelsome (its synonym belligerent can also be a noun)
Bilious: unpleasant, peevish
Boorish: crude, insensitive
Calamitous: disastrous
Caustic: corrosive, sarcastic; a corrosive substance
Cerulean: sky blue
Comely: attractive
Concomitant: accompanying
Contumacious: rebellious
Corpulent: obese
Crapulous: immoderate in appetite
Defamatory: maliciously misrepresenting
Didactic: conveying information or moral instruction
Dilatory: causing delay, tardy
Dowdy: shabby, old-fashioned; an unkempt woman
Efficacious: producing a desired effect
Effulgent: brilliantly radiant
Egregious: conspicuous, flagrant
Endemic: prevalent, native, peculiar to an area
Equanimous: even, balanced
Execrable: wretched, detestable
Fastidious: meticulous, overly delicate
Feckless: weak, irresponsible
Fecund: prolific, inventive
Friable: brittle
Fulsome: abundant, overdone, effusive
Garrulous: wordy, talkative
Guileless: naive
Gustatory: having to do with taste or eating
Heuristic: learning through trial-and-error or problem solving
Histrionic: affected, theatrical
Hubristic: proud, excessively self-confident
Incendiary: inflammatory, spontaneously combustible, hot
Insidious: subtle, seductive, treacherous
Insolent: impudent, contemptuous
Intransigent: uncompromising
Inveterate: habitual, persistent
Invidious: resentful, envious, obnoxious
Irksome: annoying
Jejune: dull, puerile
Jocular: jesting, playful
Judicious: discreet
Lachrymose: tearful
Limpid: simple, transparent, serene
Loquacious: talkative
Luminous: clear, shining
Mannered: artificial, stilted
Mendacious: deceptive
Meretricious: whorish, superficially appealing, pretentious
Minatory: menacing
Mordant: biting, incisive, pungent
Munificent: lavish, generous
Nefarious: wicked
Noxious: harmful, corrupting
Obtuse: blunt, stupid
Parsimonious: frugal, restrained
Pendulous: suspended, indecisive
Pernicious: injurious, deadly
Pervasive: widespread
Petulant: rude, ill humored
Platitudinous: resembling or full of dull or banal comments
Precipitate: steep, speedy
Propitious: auspicious, advantageous, benevolent
Puckish: impish
Querulous: cranky, whining
Quiescent: inactive, untroublesome
Rebarbative: irritating, repellent
Recalcitrant: resistant, obstinate
Redolent: aromatic, evocative
Rhadamanthine: harshly strict
Risible: laughable
Ruminative: contemplative
Sagacious: wise, discerning
Salubrious: healthful
Sartorial: relating to attire, especially tailored fashions
Sclerotic: hardening
Serpentine: snake-like, winding, tempting or wily
Spasmodic: having to do with or resembling a spasm, excitable,
intermittent
Strident: harsh, discordant; obtrusively loud
Taciturn: closemouthed, reticent
Tenacious: persistent, cohesive,
Tremulous: nervous, trembling, timid, sensitive
Trenchant: sharp, penetrating, distinct
Turbulent: restless, tempestuous
Turgid: swollen, pompous
Ubiquitous: pervasive, widespread
Uxorious: inordinately affectionate or compliant with a wife
Verdant: green, unripe
Voluble: glib, given to speaking
Voracious: ravenous, insatiable
Wheedling: flattering
Withering: devastating
Zealous: eager, devoted
EN

回答 4

Stack Overflow用户

发布于 2017-11-20 20:19:48

awk去营救!

代码语言:javascript
复制
$ awk '!a[tolower(substr($0,1,1))]++' file

这将为每个初始字符创建一个计数器,并且只在计数为零(即第一个实例)时打印。tolower()的存在是为了使它不区分大小写,如果不需要,可以删除。substr($0,1,1)从行中提取第一个字符。有一个隐式循环将对输入文件的所有行重复此操作。

通过稍微修改脚本

代码语言:javascript
复制
$ awk '++a[substr($0,1,1)]==2' file  

您可以获得第二条记录(如果存在)或使用<3代替==2前2条记录。

如果您的文件已经排序,并且案例是一致的,您可以选择一个更简单的脚本。

代码语言:javascript
复制
$ uniq -w1 file

uniq命令提取比较值的第一个实例,此处仅限于第一个字符。因此,它将立即提取所有字母中的第一个。如果案例不一致,添加-i忽略case标志。

一次扫描就够了,不需要多次扫描.

票数 9
EN

Stack Overflow用户

发布于 2017-11-20 20:21:57

Python版本:

代码语言:javascript
复制
import itertools

with open('adjectives.txt') as fp:
    # Group lines by first letter. If the lines weren't already sorted, 
    # you could replace fp with sorted(fp).
    groups = itertools.groupby(fp, key=lambda line: line[0])

    for first_letter, group in groups:
        print(next(group), end='')
票数 3
EN

Stack Overflow用户

发布于 2017-11-20 20:28:47

也许吧,巴什:

代码语言:javascript
复制
for i in {A..Z}; do grep -m1 ^$i adjectives.txt; done
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47400218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档