0

I need to replace a string in a JSON file with the filename of that file. I successfully matched the string to be replaced using the regex below, but I can't figure out how to get the filename in there to replace the string with.

Note: This is a script used in a rule inside the Hazel app. The app tells me to refer to the file being processed in the script using '$1'. So how do I insert the filename for the file $1 in the find and replace function below?

perl -pi -e 's/((?<=text": ")\S*(?="))/FILENAME/g' PATH-TO-FILE

1
  • I didn't understood. You have to substitute the string or is some other application that have to do it? BTW if you have to do it, you can try to use sed: sed -i 's/TEXT_TO_CHANGE/FILENAME/g' path/to/file/FILENAME will change all the strings TEXT_TO_CHANGE with FILENAME. Try before without -i to see the effect.
    – Hastur
    Commented Dec 18, 2015 at 11:12

1 Answer 1

1

The name of the current file is $ARGV

perl -pi -e 's/((?<=text": ")\S*(?="))/$ARGV/g' PATH-TO-FILE 

http://perldoc.perl.org/perlvar.html#Variables-related-to-filehandles

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .