I do not understand what does this command do:

 grep '<span id="geodata" class="geo">[-0-9.]*; [-0-9.]*</span>' -R articles/ --only-matching | sed 's@articles//@@' | sed 's@:<span id=.geodata. class=.geo.>@ @' | sed 's@; @ @' | sed 's@</span>@@' | sort -u -b -k1 > geocodes_from_html.txt

Some background: I'm processing wiki articles, I have a folder ("articles") full of them. The processing script was written years ago when the geo information about a place used to like that:

   <span id="geodata" class="geo">[-0-9.]*; [-0-9.]*</span>

Now it looks like that:

  <abbr class="latitude">[-0-9.]*</abbr><abbr class="longitude">[-0-9.]*</abbr>

What changes do I need to make to make the command work?

  • 3
    That code is a lousy way to extract the two coordinates from that html element... Instead of changing it you should use tools designed for this job... – don_crissti Sep 12 '17 at 13:27

The provided grep command searches for the string <span [...]</span> in any file, which is in the directory articles. The following sed commands are replacing several strings.

For example sed s@articles/@@ (with one slash / only) can be read as: sed search@this_string@replace_with_this@; the string articles/ will be replaced by nothing. Instead of piping from one sed to the next, you can combine all scripts to one with the same result.

If you do not want to use any other commands to extract the coordinates, you could use:

grep '<abbr class="latitude">[-0-9.]*</abbr><abbr class="longitude">[-0-9.]*</abbr>' -R articles --only-matching | sed 's@articles/@@;s@:<abbr class="latitude">@ @;s@<abbr class="longitude">@ @;s@</abbr>@@g' | sort -u -b -k1 >geocodes_from_html.txt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.