How to prevent from destroying symlinks, using Perl when to replace with sed regex?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












1















Based on How do I prevent sed -i from destroying symlinks?, but I use Perl.



I tried by middle of all these questions:



  • Is there a way to make perl -i not clobber symlinks?

  • perl symlink on gz file

  • How do I prevent sed from destroying hardinks?

It was unsucessfull. Here is the little and simple sample:



#!/bin/perl
use strict; use warnings;

while (<>)


## For all lines, replace all occurrences of #5c616c with another colour
s/#5c616c/#8bbac9/g $(readlink -e -- "<>")

## $(readlink -e -- "<>") is similar to --in-place --follow-symlinks

## Print the line
print;










share|improve this question






















  • The first link solution you point out is particularly interesting for not having to complicate code.

    – Rui F Ribeiro
    Jan 6 at 22:21












  • The codes in Shell are easy than in Perl, but I am not sure it is possible to make something similar in Shell to that in Perl. See the answer of [terdon](@terdon): unix.stackexchange.com/a/492724/232293

    – Gustavo Reis
    Jan 6 at 22:28















1















Based on How do I prevent sed -i from destroying symlinks?, but I use Perl.



I tried by middle of all these questions:



  • Is there a way to make perl -i not clobber symlinks?

  • perl symlink on gz file

  • How do I prevent sed from destroying hardinks?

It was unsucessfull. Here is the little and simple sample:



#!/bin/perl
use strict; use warnings;

while (<>)


## For all lines, replace all occurrences of #5c616c with another colour
s/#5c616c/#8bbac9/g $(readlink -e -- "<>")

## $(readlink -e -- "<>") is similar to --in-place --follow-symlinks

## Print the line
print;










share|improve this question






















  • The first link solution you point out is particularly interesting for not having to complicate code.

    – Rui F Ribeiro
    Jan 6 at 22:21












  • The codes in Shell are easy than in Perl, but I am not sure it is possible to make something similar in Shell to that in Perl. See the answer of [terdon](@terdon): unix.stackexchange.com/a/492724/232293

    – Gustavo Reis
    Jan 6 at 22:28













1












1








1








Based on How do I prevent sed -i from destroying symlinks?, but I use Perl.



I tried by middle of all these questions:



  • Is there a way to make perl -i not clobber symlinks?

  • perl symlink on gz file

  • How do I prevent sed from destroying hardinks?

It was unsucessfull. Here is the little and simple sample:



#!/bin/perl
use strict; use warnings;

while (<>)


## For all lines, replace all occurrences of #5c616c with another colour
s/#5c616c/#8bbac9/g $(readlink -e -- "<>")

## $(readlink -e -- "<>") is similar to --in-place --follow-symlinks

## Print the line
print;










share|improve this question














Based on How do I prevent sed -i from destroying symlinks?, but I use Perl.



I tried by middle of all these questions:



  • Is there a way to make perl -i not clobber symlinks?

  • perl symlink on gz file

  • How do I prevent sed from destroying hardinks?

It was unsucessfull. Here is the little and simple sample:



#!/bin/perl
use strict; use warnings;

while (<>)


## For all lines, replace all occurrences of #5c616c with another colour
s/#5c616c/#8bbac9/g $(readlink -e -- "<>")

## $(readlink -e -- "<>") is similar to --in-place --follow-symlinks

## Print the line
print;







sed perl symlink replace






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 6 at 21:24









Gustavo ReisGustavo Reis

154




154












  • The first link solution you point out is particularly interesting for not having to complicate code.

    – Rui F Ribeiro
    Jan 6 at 22:21












  • The codes in Shell are easy than in Perl, but I am not sure it is possible to make something similar in Shell to that in Perl. See the answer of [terdon](@terdon): unix.stackexchange.com/a/492724/232293

    – Gustavo Reis
    Jan 6 at 22:28

















  • The first link solution you point out is particularly interesting for not having to complicate code.

    – Rui F Ribeiro
    Jan 6 at 22:21












  • The codes in Shell are easy than in Perl, but I am not sure it is possible to make something similar in Shell to that in Perl. See the answer of [terdon](@terdon): unix.stackexchange.com/a/492724/232293

    – Gustavo Reis
    Jan 6 at 22:28
















The first link solution you point out is particularly interesting for not having to complicate code.

– Rui F Ribeiro
Jan 6 at 22:21






The first link solution you point out is particularly interesting for not having to complicate code.

– Rui F Ribeiro
Jan 6 at 22:21














The codes in Shell are easy than in Perl, but I am not sure it is possible to make something similar in Shell to that in Perl. See the answer of [terdon](@terdon): unix.stackexchange.com/a/492724/232293

– Gustavo Reis
Jan 6 at 22:28





The codes in Shell are easy than in Perl, but I am not sure it is possible to make something similar in Shell to that in Perl. See the answer of [terdon](@terdon): unix.stackexchange.com/a/492724/232293

– Gustavo Reis
Jan 6 at 22:28










1 Answer
1






active

oldest

votes


















2














The readlink -e command is not portable so should not be relied upon.



$ cat input
Like quills upon the fretful porpentine.
$ ln -s input alink
$ readlink -e alink
readlink: illegal option -- e
usage: readlink [-n] [file ...]


Within the Perl code, instead replace the links with the filename it points to using Perl's readlink function then loop over the input as usual.



$ perl -i -ple 'BEGINfor(@ARGV) $_=readlink if -l tr/A-Z/a-z/' alink


alink is still a symbolic link and the contents of input have been modified:



$ perl -E 'say readlink "alink"'
input
$ cat input
LIKE QUILLS UPON THE FRETFUL PORPENTINE.


Within a Perl script this might look something like



#!/usr/bin/env perl
use strict;
use warnings;

for my $arg (@ARGV)
$arg = readlink $arg if -l $arg;


# in-place edit with backup filename, perldoc -v '$^I'
$^I = ".whoops";

while (readline)
s/#5c616c/#8bbac9/g;
print;



though may need List::Util::uniq or similar to avoid modifying the same filename two or more times, if the input contains duplicate filenames.






share|improve this answer























  • What if <> is necessary? See the first codes lines of [terdon](@terdon): unix.stackexchange.com/a/492724/232293.

    – Gustavo Reis
    Jan 6 at 22:09











  • <> is a less readable way to say readline. compare perl -MO=Deparse,-p -e '<>;readline'

    – thrig
    Jan 6 at 23:16











  • Hi, I have tested it, it almost worked - it preserved the symlinks, but it made all unusual. You can download the small archive file: transfernow.net/f07ahfb8joa4 and test by yourself.

    – Gustavo Reis
    Jan 7 at 1:29










Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492886%2fhow-to-prevent-from-destroying-symlinks-using-perl-when-to-replace-with-sed-reg%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














The readlink -e command is not portable so should not be relied upon.



$ cat input
Like quills upon the fretful porpentine.
$ ln -s input alink
$ readlink -e alink
readlink: illegal option -- e
usage: readlink [-n] [file ...]


Within the Perl code, instead replace the links with the filename it points to using Perl's readlink function then loop over the input as usual.



$ perl -i -ple 'BEGINfor(@ARGV) $_=readlink if -l tr/A-Z/a-z/' alink


alink is still a symbolic link and the contents of input have been modified:



$ perl -E 'say readlink "alink"'
input
$ cat input
LIKE QUILLS UPON THE FRETFUL PORPENTINE.


Within a Perl script this might look something like



#!/usr/bin/env perl
use strict;
use warnings;

for my $arg (@ARGV)
$arg = readlink $arg if -l $arg;


# in-place edit with backup filename, perldoc -v '$^I'
$^I = ".whoops";

while (readline)
s/#5c616c/#8bbac9/g;
print;



though may need List::Util::uniq or similar to avoid modifying the same filename two or more times, if the input contains duplicate filenames.






share|improve this answer























  • What if <> is necessary? See the first codes lines of [terdon](@terdon): unix.stackexchange.com/a/492724/232293.

    – Gustavo Reis
    Jan 6 at 22:09











  • <> is a less readable way to say readline. compare perl -MO=Deparse,-p -e '<>;readline'

    – thrig
    Jan 6 at 23:16











  • Hi, I have tested it, it almost worked - it preserved the symlinks, but it made all unusual. You can download the small archive file: transfernow.net/f07ahfb8joa4 and test by yourself.

    – Gustavo Reis
    Jan 7 at 1:29















2














The readlink -e command is not portable so should not be relied upon.



$ cat input
Like quills upon the fretful porpentine.
$ ln -s input alink
$ readlink -e alink
readlink: illegal option -- e
usage: readlink [-n] [file ...]


Within the Perl code, instead replace the links with the filename it points to using Perl's readlink function then loop over the input as usual.



$ perl -i -ple 'BEGINfor(@ARGV) $_=readlink if -l tr/A-Z/a-z/' alink


alink is still a symbolic link and the contents of input have been modified:



$ perl -E 'say readlink "alink"'
input
$ cat input
LIKE QUILLS UPON THE FRETFUL PORPENTINE.


Within a Perl script this might look something like



#!/usr/bin/env perl
use strict;
use warnings;

for my $arg (@ARGV)
$arg = readlink $arg if -l $arg;


# in-place edit with backup filename, perldoc -v '$^I'
$^I = ".whoops";

while (readline)
s/#5c616c/#8bbac9/g;
print;



though may need List::Util::uniq or similar to avoid modifying the same filename two or more times, if the input contains duplicate filenames.






share|improve this answer























  • What if <> is necessary? See the first codes lines of [terdon](@terdon): unix.stackexchange.com/a/492724/232293.

    – Gustavo Reis
    Jan 6 at 22:09











  • <> is a less readable way to say readline. compare perl -MO=Deparse,-p -e '<>;readline'

    – thrig
    Jan 6 at 23:16











  • Hi, I have tested it, it almost worked - it preserved the symlinks, but it made all unusual. You can download the small archive file: transfernow.net/f07ahfb8joa4 and test by yourself.

    – Gustavo Reis
    Jan 7 at 1:29













2












2








2







The readlink -e command is not portable so should not be relied upon.



$ cat input
Like quills upon the fretful porpentine.
$ ln -s input alink
$ readlink -e alink
readlink: illegal option -- e
usage: readlink [-n] [file ...]


Within the Perl code, instead replace the links with the filename it points to using Perl's readlink function then loop over the input as usual.



$ perl -i -ple 'BEGINfor(@ARGV) $_=readlink if -l tr/A-Z/a-z/' alink


alink is still a symbolic link and the contents of input have been modified:



$ perl -E 'say readlink "alink"'
input
$ cat input
LIKE QUILLS UPON THE FRETFUL PORPENTINE.


Within a Perl script this might look something like



#!/usr/bin/env perl
use strict;
use warnings;

for my $arg (@ARGV)
$arg = readlink $arg if -l $arg;


# in-place edit with backup filename, perldoc -v '$^I'
$^I = ".whoops";

while (readline)
s/#5c616c/#8bbac9/g;
print;



though may need List::Util::uniq or similar to avoid modifying the same filename two or more times, if the input contains duplicate filenames.






share|improve this answer













The readlink -e command is not portable so should not be relied upon.



$ cat input
Like quills upon the fretful porpentine.
$ ln -s input alink
$ readlink -e alink
readlink: illegal option -- e
usage: readlink [-n] [file ...]


Within the Perl code, instead replace the links with the filename it points to using Perl's readlink function then loop over the input as usual.



$ perl -i -ple 'BEGINfor(@ARGV) $_=readlink if -l tr/A-Z/a-z/' alink


alink is still a symbolic link and the contents of input have been modified:



$ perl -E 'say readlink "alink"'
input
$ cat input
LIKE QUILLS UPON THE FRETFUL PORPENTINE.


Within a Perl script this might look something like



#!/usr/bin/env perl
use strict;
use warnings;

for my $arg (@ARGV)
$arg = readlink $arg if -l $arg;


# in-place edit with backup filename, perldoc -v '$^I'
$^I = ".whoops";

while (readline)
s/#5c616c/#8bbac9/g;
print;



though may need List::Util::uniq or similar to avoid modifying the same filename two or more times, if the input contains duplicate filenames.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 6 at 21:57









thrigthrig

24.5k23056




24.5k23056












  • What if <> is necessary? See the first codes lines of [terdon](@terdon): unix.stackexchange.com/a/492724/232293.

    – Gustavo Reis
    Jan 6 at 22:09











  • <> is a less readable way to say readline. compare perl -MO=Deparse,-p -e '<>;readline'

    – thrig
    Jan 6 at 23:16











  • Hi, I have tested it, it almost worked - it preserved the symlinks, but it made all unusual. You can download the small archive file: transfernow.net/f07ahfb8joa4 and test by yourself.

    – Gustavo Reis
    Jan 7 at 1:29

















  • What if <> is necessary? See the first codes lines of [terdon](@terdon): unix.stackexchange.com/a/492724/232293.

    – Gustavo Reis
    Jan 6 at 22:09











  • <> is a less readable way to say readline. compare perl -MO=Deparse,-p -e '<>;readline'

    – thrig
    Jan 6 at 23:16











  • Hi, I have tested it, it almost worked - it preserved the symlinks, but it made all unusual. You can download the small archive file: transfernow.net/f07ahfb8joa4 and test by yourself.

    – Gustavo Reis
    Jan 7 at 1:29
















What if <> is necessary? See the first codes lines of [terdon](@terdon): unix.stackexchange.com/a/492724/232293.

– Gustavo Reis
Jan 6 at 22:09





What if <> is necessary? See the first codes lines of [terdon](@terdon): unix.stackexchange.com/a/492724/232293.

– Gustavo Reis
Jan 6 at 22:09













<> is a less readable way to say readline. compare perl -MO=Deparse,-p -e '<>;readline'

– thrig
Jan 6 at 23:16





<> is a less readable way to say readline. compare perl -MO=Deparse,-p -e '<>;readline'

– thrig
Jan 6 at 23:16













Hi, I have tested it, it almost worked - it preserved the symlinks, but it made all unusual. You can download the small archive file: transfernow.net/f07ahfb8joa4 and test by yourself.

– Gustavo Reis
Jan 7 at 1:29





Hi, I have tested it, it almost worked - it preserved the symlinks, but it made all unusual. You can download the small archive file: transfernow.net/f07ahfb8joa4 and test by yourself.

– Gustavo Reis
Jan 7 at 1:29

















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492886%2fhow-to-prevent-from-destroying-symlinks-using-perl-when-to-replace-with-sed-reg%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown






Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?