0

我有一个文件 input.txt,里面有很多奇怪的字符、html 标签和有用的材料。我想在“描述”一词之后显示 35 个字符,不包括像 $&lmp 这样的奇怪字符,并且在新文件 output.txt 中没有 html 标记。

输入样本:

    </image>
  <title>A Londoner Looks Back: Were The Olympics Awesome?</title>
  <link>http://www.askmen.com/sports/fanatic/london-olympics-post-mortem.html</link>
  <description rdf:parseType="Literal">

                The other evening I walked out of London&amp;rsquo;s &lt;a
href="http://www.askmen.com/fashion/watch_100/135_olympic-watches.html"&gt;Olympic
stadium onto the new &amp;ldquo;Javelin&amp;rdquo; train into town. (The journey from east to
central London, quite recently still something of a commuter&amp;rsquo;s nightmare, took just
six minutes.) A railway worker on the platform didn&amp;rsquo;t just point everyone the way
onto the train; he did a dance for us. You don&amp;rsquo;t usually get that on London
transport. These Olympics made the city happier.I now live in Paris, but I
consider myself a Londoner. I went to nursery school in London, spent 15 years of my life
in the city, speak in a London accent, visit my parents and siblings here, and, as someone
of mongrel origin who belongs nowhere, I feel at home in the world&amp;rsquo;s most
cosmopolitan city. To steal a line from the 1980s film Sammy and Rosie Get Laid:
&amp;ldquo;I&amp;rsquo;m not English. I&amp;rsquo;m a Londoner.&amp;rdquo; But London is also a sprawling,
gray, wet, overpriced city where traveling anywhere always seems to take forever, and
Londoners are not positive people. In fact, we are whiners. Going into the
Olympics, the whining was at full blast. Landing in London days before &lt;a
href="http://www.askmen.com/sports/bodybuilding/olympic-bobsledding.html"&gt;the Games
began, I found my friends and family full of dread. The Games&amp;rsquo; organizers had
indicated that while the Olympics were on, traveling anywhere would take even longer than
forever. My sister had been told to be at her desk at 7 a.m. during the Games to avoid the
rush hour -- this in a city where many people start work nearer to 10 a.m. A friend showed
me a kind of war scenario prepared by the bank where he worked, full of ominous questions
like, &amp;ldquo;What if your supply chain stopped?&amp;rdquo; &amp;ldquo;What if your technology
failed?&amp;rdquo; &amp;ldquo;What if your brand, image and reputation were impacted by any of the
above?&amp;rdquo; And what was all this upheaval in aid of? To watch some doped-up
moustachioed Eastern European women win incomprehensible weightlifting events? In a YouGov
survey days before the opening ceremony, only 51% of Britons expressed an interest in the
Olympics -- and that was a lot better than earlier surveys.On the day of the
opening ceremony I happened to have a meeting down the street from my last &lt;a
href="http://www.askmen.com/london/"&gt;London address (a shared flat above a now defunct
liquor store). I ran to Baker Street tube, as I&amp;rsquo;d done a thousand times before. Then
I got on a media bus to the opening ceremony that passed Southwark Bridge with the
Financial Times building where I had worked in the 1990s. It was like a dream:
You move through a familiar landscape that has been transformed. The Olympics helped me
see London afresh.It was during the opening ceremony that the mood among
Londoners changed. I know foreigners didn&amp;rsquo;t get all the references: the Windrush
ship that brought the first Jamaican immigrants to Britain in 1948, the BBC weather
forecaster Michael Fish assuring us there would be no hurricane the night before one
struck in 1987, the dance of the state-funded National Health Service nurses. But Londoners got it. Danny Boyle, the director, gave us a multicultural and funny
Britain that had finally shed its imperial delusions of grandeur. The Olympic torch was
run into the stadium not by an Aryan superman but by the pot-bellied middle-aged ex-rower
Steve Redgrave, who can&amp;rsquo;t run. For the first time in my life, Boyle&amp;rsquo;s Britain
made me feel a patriot. The opening ceremony remains the highlight of my Olympics.Then the sports began, and with it the instinctive expectation that the Brits
would fall flat on their faces. We may have invented modern sports, but England&amp;rsquo;s
soccer team hasn&amp;rsquo;t won a prize since 1966, and no British man has won Wimbledon
since 1936. Surely our Olympians would continue the tradition?&amp;nbsp;It seemed
so on the first day, when Britain&amp;rsquo;s much-hyped male cyclists failed to win a medal
or even to figure in the run-in in front of Buckingham Palace. Only on the second day did
our first medal arrive: A silver for cyclist Lizzie Armitstead, a polite young vegetarian
from the rural north so little-known that at the press conference she had to introduce
herself to the nation. &amp;ldquo;I could never get my head around eating corpses,&amp;rdquo; she
explained. On the fourth morning, Britain still had no golds. The more
excitable newspapers began demanding inquests. And then the golds came in a crazy rush,
won by a bunch of underpaid Britons of all colors whose frank delight was irresistible.
Above all, there was Mo Farah, the Somali-born runner, who had arrived in London&amp;rsquo;s
suburbs as an eight-year-old barely able to speak English, and had really wanted to play
on the wing for Arsenal, but who won gold in the 10,000 and 5,000 meters instead. After
his first gold, an African journalist asked if he wouldn&amp;rsquo;t rather have been running
for Somalia. &amp;ldquo;Look, mate, this is my country,&amp;rdquo; replied Farah. He was
Boyle&amp;rsquo;s multicultural Britain. The second Saturday of the Olympics, when
Farah was among six Britons to win gold, was Britain&amp;rsquo;s best sporting day since 1966.
It was our best single Olympic day since the Games were held in London in 1908. Of course,
we embarked on an orgy of patriotism. On BBC TV, the new &amp;ldquo;British heroes&amp;rdquo; were
feted much like &amp;ldquo;heroes of the harvest&amp;rdquo; on North Korean state TV. Foreigners
rightly accused the Britons of practically ignoring the other 200 nations. However,
that&amp;rsquo;s what every country at the Olympics does. Each country watches its own Games.                


</item>
  <title>How Facial Hair Can Save You From Skin Cancer</title>
  <link>http://www.askmen.com/sports/news/moustaches-and-skin-cancer.html</link>
  <description rdf:parseType="Literal">

输出应该是这样的:

The other evening I walked out of London Olympic
stadium onto the new Javelin train into town. (The journey from east to
central London, quite recently still

If you thought moustaches were solely to distinguish regular males from porn stars and
hipsters, think again. A new study suggests that

我努力了:

sed 's/^.*<description>/<description>/
s/&lt;/</g
s/&gt;/>/g
s/&amp;rsquo;/'"'"'/g
s/&amp;ccedil;/c/g
s/<[^>]*>//g
s/^\(.\{35\}\).*/\1/' inputsample.txt
4

1 回答 1

1

我不相信 sed 有可能,因为 sed 不理解 XML 实体。你需要使用像 Perl 或 Python 这样的编程语言来完成这样的事情。

我能得到你的最接近的是:

$ sed -nE '/description/s/.*description(.{,35}).*>/\1/p' file_name

-E方法使用扩展的正则表达式,因此{35,35}将起作用。-n说不打印。我正在捕捉接下来的 35 个字符并用整行替换它们。

但是,任何特殊实体(例如&nbsp;和所有赌注)都已关闭。

于 2012-08-19T04:10:22.047 回答