0

I want to edit the original files with perl, when I have a numeric sequence of aliases.

The task is to use perl on the command line, e.g.

 perl -pe 's/apples/oranges/' 

But to edit files in place:

 perl -i.bak 

The -i option does not work with symbolic links

 ln -s fs fruitSalad 

With -i, perl will re-write a new file, replacing (clobbering) the prior symbolic link. i.e. it does not edit original.

There's a workaround (https://unix.stackexchange.com/questions/9318/is-there-a-way-to-make-perl-i-not-clobber-symlinks) which is like this:

 perl -i.bak -pe 's/apples/oranges/' $(readlink fs) 

Here's my problem. I want to edit in-place, the original files from a sequence of symbolic links.

 ln -s fs.1 fruitSalad-1 ln -s fs.2 fruitSalad-2 

With normal files, perl will work with ranges, e.g.

 perl -i.bak -pe 's/apples/oranges/' fs.{1..2} 

But that does not work with the $(readlink __) trick. It's not possible to combine the above, and I prefer to avoid xargs.

Any idea how to edit ranges of files via their symbolic links from the command line? What am I missing?

1
  • Here's an example with xargs.... seq 1 10 | xargs -n 1 -I % readlink %.xml | xargs -I % perl -pe 's#apples#oranges#ge' % Commented Jun 30, 2017 at 21:38

1 Answer 1

1

One method is to replace the contents of @ARGV (the filenames being acted on) with the target of the link prior to looping over the file contents should that file be a symbolic link.

$ mkdir blah && cd blah $ echo aa > orig $ ln -s orig slink $ perl -i -pe 'BEGIN{@ARGV=map{$_=readlink if -l}@ARGV}; s/aa/bb/' slink $ readlink slink orig $ cat orig bb $ 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.