LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need a regex, I suck at regex's (https://www.linuxquestions.org/questions/programming-9/need-a-regex-i-suck-at-regexs-14807/)

d3funct 02-21-2002 03:53 PM

Need a regex, I suck at regex's
 
Hi, I'm working on a 6000 line file full of document links that I need to html tag for a web page. I got the file by doing an 'ls -ltRa' of my server document directory. The output I got goes something like this:

/documents/serverA/pdfs/somefile.pdf
/documents/serverA/images/someimage.jpg
/documents/serverA/docs/somedoc.doc


What I want to do is use a regex to make the path an html tag and then use the document name as the reference to the path. But, I want to keep the filename in the path AND use it in the HREF. Any suggestions would be appreciated.

Malicious 02-21-2002 04:44 PM

Lesson number 1.

sed 's/\(\/.*\/\)\(.*\)/tag \1 ref \2 HREF \1\2' yourfile

Will output:

tag /documents/serverA/pdfs/ ref somefile.pdf HREF /documents/serverA/pdfs/somefile.pdf

Can you take it from there? The parens delimit a matched string and you can reference that on the substitution side with \n where n in the matched set, 1 being the first.

There will be a quiz...

acid_kewpie 02-21-2002 05:21 PM

Quote:

Originally posted by Malicious
Lesson number 1.

sed 's/\(\/.*\/\)\(.*\)/tag \1 ref \2 HREF \1\2' yourfile

Will output:

tag /documents/serverA/pdfs/ ref somefile.pdf HREF /documents/serverA/pdfs/somefile.pdf

Can you take it from there? The parens delimit a matched string and you can reference that on the substitution side with \n where n in the matched set, 1 being the first.

There will be a quiz...

correction-
sed 's/\(\/.*\/\)\(.*\)/tag \1 ref \2 HREF \1\2/' yourfile

check http://perso.club-internet.fr/thedec...wk/ch03_02.htm (from my sig)

Malicious 02-21-2002 06:10 PM

Damn, hate it when that happens! Missed a slash (slant, stroke, whatever). I had to turn in my slash key at the airport gate; they thought I could hurt somebody with it. Sure 'nuff.

mtsinc 02-25-2002 08:28 PM

As they say in Perl-land, "there's more than one way to do it!":)

perl -ne 'chomp; print "<a href=\"$_\">$_</a>\n";'


All times are GMT -5. The time now is 08:07 PM.