1

I have a compressed Javascript file which is in a single line form and contains

bla bla bla,;xyz=require("something"),blablabla=3xyz; if (require("somethingElse") === bla bla

I want to extract the required something and somethingElse from this "string". Here is the code I could build so far:

$ echo $script | sed 's|require(\([^)]*\))|\nget-this \1\n|g' | grep get-this | sed 's|get-this ||'

Output is:

something
somethingElse

How can I make this in a proper way?

Edit

Exact working code is follows:

cat app.min.js | grep -o 'require("[a-ZA-Z0-9]\+")' | grep -o '".*"' | sed 's/"//g'

Produces:

Wifi
net
FlashEEPROM
abc1234
DS18B20
1

grep has a -o or --only-matching option you can use for this, from man grep

   -o, --only-matching
          Print only the matched (non-empty) parts  of  a  matching  line,
          with each such part on a separate output line.

So for me this:

$ cat compressed_js_file  | grep -o 'require("[[:alpha:]]\+")'

produces

require("something")
require("somethingElse")
| improve this answer | |
  • Shorter: grep -o 'require("[[:alpha:]]\+")' compressed_js_file. – jcbermu Apr 25 '16 at 10:11
1

A longer way using only awk:

awk -F "require" '{print $2 $3}' compressed_js_file | awk -F '[()"]' '{print $3"\n"$8}'

It produces:

something
somethingelse
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.