regex - Using bash regexp to insert the contents of a file into another -
i have javascript file jquery function call:
$.getscript('/scripts/files/file.js'); i want replace line contents of file @ path. bash script have far:
cat public/scripts/old.js | sed -e "s/$\.getscript\('(.)+'\);/$(cat \1)/g" > public/scripts/new.js however, regular expression , remembering path not seem working correctly. cat: 1: no such file or directory seems if cat being called on number 1 (which should remembered portion of regexp). how can fix this?
because using $() inside double quotes, shell parsing cat \1, stripping backslash , trying run cat 1 pass output part of argument sed. sed has command (r) reading file, filename must literal, , cannot result of previous sed commands (at least in standard sed, perhaps implementations provide ability). sed wrong tool this. awk solution, fragile.
here's possible perl solution (warning: fragile):
perl -ne 'if( $_ =~ /\$\.getscript\('"'(.*)'"'\)/ ) { system( "cat $1" ) } else {print}' public/scripts/old.js
Comments
Post a Comment