1

Ok, I have some data in the form:

<a href="/praha/jaktovidi/_zprava/jak-to-vidi-rektor-richard-hindls-5-cervna--1220102">
<a href="/praha/jaktovidi/_zprava/jak-to-vidi-erik-best-4-cervna--1219526">
<a href="/praha/jaktovidi/_zprava/jak-to-vidi-ivan-hoffman-3-cervna--1218826">
<a href="/praha/jaktovidi/_zprava/jak-to-vidi-pavel-kosatik-19-kvetna-verejne-chci-podekovat-statnim-zastupcum-a-policistum-kteri-se-nebali--1225675">

and I'd like to change it to:

5-cervna <a href="/praha/jaktovidi/_zprava/jak-to-vidi-rektor-richard-hindls-5-cervna--1220102">
4-cervna <a href="/praha/jaktovidi/_zprava/jak-to-vidi-erik-best-4-cervna--1219526">
3-cervna <a href="/praha/jaktovidi/_zprava/jak-to-vidi-ivan-hoffman-3-cervna--1218826">
19-kvetna <a href="/praha/jaktovidi/_zprava/jak-to-vidi-pavel-kosatik-19-kvetna-verejne-chci-podekovat-statnim-zastupcum-a-policistum-kteri-se-nebali--1225675">

ie. extract the date from the link and prepend it to the link. Is there a nice way to do this with regular expressions, or with some nice bash script? The number of dashes is not always the same and it's not "cervna" all the time either.

I've arrived to the following grep command which grabs the appropriate output, I just don't know how to paste it together with the original data:

grep -Po '[0-9]{1,2}-[[:alpha:]]*'
  • You want us to suggest a regular expression (which is a BAD idea for parsing HTML) to extract data that you have not shown us? Please include an example of the actual data you need to parse. – terdon Jul 30 '13 at 15:08
  • @terdon Actually, the fact that it's html is irrelevant, it might as well be random words, I just have another script that accepts data in the given form. The example I have given is solid. – VPeric Jul 30 '13 at 15:12
  • 2
    In the example you have posted, there is a single dash before the part you need to match but in your real data you say there is more. Any solution I give you for what you have posted is likely to not work on your real data. If you can't show the real thing, at least give dummy data that accurately represent your needs. – terdon Jul 30 '13 at 15:20
  • Please post the actual data you'd like the solution to work against! – slm Jul 30 '13 at 15:26
  • 1
    But ДМИТРИЙ МАЛИКОВ's answer works unchanged. – Paulo Almeida Jul 30 '13 at 15:39
4
$> sed -r -e 's/(.*-)([0-9]+-[a-z]+)(.*)/\2 \1\2\3/' text 
8-july <a href="arch/names-8-july--123123123">
12-february <a href="arch/seman-12-february-text-text--321321321">
17-june <a href="arch/nmaes-17-june--213213321">

It seems to be appropriate for your newest example too:

$> sed -r 's/(.*-)([0-9]+-[a-z]+)(.*)/\2 \1\2\3/' text2
5-cervna <a href="/praha/jaktovidi/_zprava/jak-to-vidi-rektor-richard-hindls-5-cervna--1220102">
4-cervna <a href="/praha/jaktovidi/_zprava/jak-to-vidi-erik-best-4-cervna--1219526">
3-cervna <a href="/praha/jaktovidi/_zprava/jak-to-vidi-ivan-hoffman-3-cervna--1218826">
19-kvetna <a href="/praha/jaktovidi/_zprava/jak-to-vidi-pavel-kosatik-19-kvetna-verejne-chci-podekovat-statnim-zastupcum-a-policistum-kteri-se-nebali--1225675">
| improve this answer | |
3

This assumes that you are looking for the first match of a number, followed by a dash and then a string of word characters.

    perl -ne '/(\d+-\w+)/; print "$1 $_"' file.txt
| improve this answer | |
3

Different solution with cut and paste (it depends on there being an existing text file):

cut -d"-" -f 6-7 text | paste - text

After the edit, this would work:

grep -Po '[0-9]{1,2}-[[:alpha:]]*' text | paste - text
| improve this answer | |
3
perl -pe '$_ = "$& $_" if /[0-9]{1,2}-[[:alpha:]]*/'
| improve this answer | |
  • @slm it won't work with the posted example, the OP says that in the real data there are more dashes which is why Stephane is using dollars 6 and 7. – terdon Jul 30 '13 at 15:20
  • @terdon - thanks see that bit towards the end. – slm Jul 30 '13 at 15:22

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.