regexshellawksed

Wrap a string in tags using text manipulation in a Unix environment


I have a text file that outputs as below:

    /usr/local/jack/var/12345/configs/10.254.254.254
    1845:    edit "n_10.73.6.0/24"
    1847:        set subnet 10.73.6.0 255.255.255.0
    2161:        set member "n_10.73.8.0/24" "n_10.73.1.0/24" "n_10.73.2.0/24" "n_10.73.3.0/24" "n_10.73.4.0/24" "n_10.73.5.0/24" "n_10.73.6.0/24" "n_10.73.7.0/24" "n_10.73.9.0/24"
    7030:        set dst 10.73.6.0 255.255.255.0

    /usr/local/jack/var/12346/configs/10.254.254.255
    1845:    edit "n_10.73.6.0/24"
    1847:        set subnet 10.73.6.0 255.255.255.0
    2161:        set member "n_10.73.8.0/24" "n_10.73.1.0/24" "n_10.73.2.0/24" "n_10.73.3.0/24" "n_10.73.4.0/24" "n_10.73.5.0/24" "n_10.73.6.0/24" "n_10.73.7.0/24" "n_10.73.9.0/24"
    7030:        set dst 10.73.6.0 255.255.255.0
    8 matches
    2 files contained matches
    931 files searched
    31258200 bytes searched
    0.056300 seconds
    logout

I want to be able to turn the file path into a hyperlink based on the file path itself. For example:

/usr/local/jack/var/12346/configs/10.254.254.255

would become:

<a href="http://jack.localnet/viewvc/12345/configs/10.254.254.255?view=markup">/usr/local/jack/var/12346/configs/10.254.254.255</a>

There could be 1,2 or hundreds of occurrences within this file. I want to make it so each occurrence will be replaced with a hyperlink.

Is this something that is possible with bash, sed, awk, python or similar?


Solution

  • sed 's,^\(/usr/local/jack/var/[0-9]*\)\(/.*\)$,<a href="http://jack.localnet/viewvc/12345\2?view=markup">\1\2</a>,g' file.txt
    

    add -i if you want to replace the content of the file directly (but you need to be sure that the command is working fine). Safer, you can do :

    sed 's,^\(/usr/local/jack/var/[0-9]*\)\(/.*\)$,<a href="http://jack.localnet/viewvc/12345\2?view=markup">\1\2</a>,g' file.txt > new_file.txt
    

    Result :

    <a href="http://jack.localnet/viewvc/12345/configs/10.254.254.254?view=markup">/usr/local/jack/var/12345/configs/10.254.254.254</a>
    1845:    edit "n_10.73.6.0/24"
    1847:        set subnet 10.73.6.0 255.255.255.0
    2161:        set member "n_10.73.8.0/24" "n_10.73.1.0/24" "n_10.73.2.0/24" "n_10.73.3.0/24" "n_10.73.4.0/24" "n_10.73.5.0/24" "n_10.73.6.0/24" "n_10.73.7.0/24" "n_10.73.9.0/24"
    7030:        set dst 10.73.6.0 255.255.255.0
    
    <a href="http://jack.localnet/viewvc/12345/configs/10.254.254.255?view=markup">/usr/local/jack/var/12346/configs/10.254.254.255</a>
    1845:    edit "n_10.73.6.0/24"
    1847:        set subnet 10.73.6.0 255.255.255.0
    2161:        set member "n_10.73.8.0/24" "n_10.73.1.0/24" "n_10.73.2.0/24" "n_10.73.3.0/24" "n_10.73.4.0/24" "n_10.73.5.0/24" "n_10.73.6.0/24" "n_10.73.7.0/24" "n_10.73.9.0/24"
    7030:        set dst 10.73.6.0 255.255.255.0
    8 matches
    2 files contained matches
    931 files searched
    31258200 bytes searched
    0.056300 seconds
    logout
    

    is it normal if 12346 becomes 12345 ?