Linkify text file in Linux

SwagFapper

I have parsed all rows containing urls from a text file and appended line breaks, and I want to make the links clickable in a new file.

How do I append <a href> -tags around only the urls, using standard linux tools, preferably awk? It needs to be automatable in cron.

For example,

source file chaturls.txt:

    12:30 <user> check this: https://link.to/stuff.jpg</br>
    13:47 <user4> https://another.link.lol eyyyy</br>

desired output in new file, chatlinkified.html:

12:30 <user> check this: <a href='https://link.to/stuff.jpg'>https://link.to/stuff.jpg</a></br>
13:47 <user4> <a href='https://another.link.lol'>https://another.link.lol</a> eyyyy</br>

I tried awk '{printf "<a href=\"%s\">%s</a><br>", $0,$0}' chaturls.txt > chatlinkified.html, but this makes the whole line an (invalid) clickable link.

Hielke Walinga
sed -E 's@(https?://[^[:space:]/$.?#].[^[:space:]<]*)@<a href="\1">\1</a>@g' chaturls.txt > chatlinkified.html

You can use sed and refer back to the matched group with \1. NB. here I separate using the @ instead of / (as in s/../../g), you are free the use any character and this saves some escapes.

The regex for finding the URL does some validation checks for the first character after the https?:// and then proceeds the match until a space or the starting bracket of another tag.

You can if you want to use a more simpler regex for the url like, given in one of the comments https?://[^ ]*) which doesn't include this small validation.

You can find more extensive validated url regex here: https://mathiasbynens.be/demo/url-regex (But you have to convert from PHP regex to sed extended regex)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Linkify any text Android

From Dev

linux compare text file

From Dev

PHP: How do I linkify all links inside a given text?

From Dev

Android, EditText - How to "delinkify" already linkified (using Linkify class) text

From Dev

text colors for one TextView using two Linkify's and weblinks clickable

From Dev

linux replacing line in text file

From Dev

Linkify Method in html.erb file showing up with HTML in view

From Dev

How to create a text file with specific text in it in Linux?

From Dev

How to create a text file with specific text in it in Linux?

From Dev

How to Randomize Columns of a Text File in Linux

From Dev

Where is the character encoding of a text file stored in Linux?

From Dev

Print Text (Without File) On Linux With LPR

From Dev

Linux PdfToText function return blank text file

From Dev

How get value from text file in linux

From Dev

Merge two columns of a text file in Linux

From Dev

partial merging 2 text file in linux

From Dev

Sort lines in text file with specific separator in Linux

From Dev

Sort lines in text file with specific separator in Linux

From Dev

Linux : Data extraction from text file

From Dev

Linux: Locating text file from a string input

From Dev

Linux PdfToText function return blank text file

From Dev

find and replace text in file recursively in linux

From Dev

Linux script to extract usernames from text file

From Dev

Linux Extract Matched Text Field from File

From Dev

Embedded linux to read text and append it to another file

From Dev

Partially open and modify a text file in Linux

From Dev

How to search each occurrence in a text file Linux?

From Dev

AWK / Linux script calculations from a text file

From Dev

Removing block of text from csv file in Linux

Related Related

  1. 1

    Linkify any text Android

  2. 2

    linux compare text file

  3. 3

    PHP: How do I linkify all links inside a given text?

  4. 4

    Android, EditText - How to "delinkify" already linkified (using Linkify class) text

  5. 5

    text colors for one TextView using two Linkify's and weblinks clickable

  6. 6

    linux replacing line in text file

  7. 7

    Linkify Method in html.erb file showing up with HTML in view

  8. 8

    How to create a text file with specific text in it in Linux?

  9. 9

    How to create a text file with specific text in it in Linux?

  10. 10

    How to Randomize Columns of a Text File in Linux

  11. 11

    Where is the character encoding of a text file stored in Linux?

  12. 12

    Print Text (Without File) On Linux With LPR

  13. 13

    Linux PdfToText function return blank text file

  14. 14

    How get value from text file in linux

  15. 15

    Merge two columns of a text file in Linux

  16. 16

    partial merging 2 text file in linux

  17. 17

    Sort lines in text file with specific separator in Linux

  18. 18

    Sort lines in text file with specific separator in Linux

  19. 19

    Linux : Data extraction from text file

  20. 20

    Linux: Locating text file from a string input

  21. 21

    Linux PdfToText function return blank text file

  22. 22

    find and replace text in file recursively in linux

  23. 23

    Linux script to extract usernames from text file

  24. 24

    Linux Extract Matched Text Field from File

  25. 25

    Embedded linux to read text and append it to another file

  26. 26

    Partially open and modify a text file in Linux

  27. 27

    How to search each occurrence in a text file Linux?

  28. 28

    AWK / Linux script calculations from a text file

  29. 29

    Removing block of text from csv file in Linux

HotTag

Archive