2

I am trying to print out the text that is surrounded by single quotes.

/bin/bash -lc '/home/CASPER_REPORTS/scripts/CASPER_gen_report.sh CASPER_1'
/bin/bash -lc '/home/CASPER_REPORTS/scripts/CASPER_gen_report.sh CASPER_1A'
/bin/bash -lc '/home/CASPER_REPORTS/scripts/CASPER_gen_report.sh CASPER_2'
/bin/bash -lc '/home/CASPER_REPORTS/scripts/CASPER_gen_report.sh CASPER_3'
/bin/bash -lc '/home/CASPER_REPORTS/scripts/CASPER_gen_report.sh CASPER_3A'

The Boolean one I guess means that perl sees the string.

$ cat /tmp/casper_reports | perl -nle 'print /'.*'/'
1
1
1
1
1

However when I try and capture it with the parenthesis it throws an error

$ cat /tmp/boobomb | perl -nle 'print /'(.*)'/'
-bash: syntax error near unexpected token `('
zdim
  • 56,772
  • 4
  • 49
  • 75
capser
  • 2,322
  • 5
  • 33
  • 70

2 Answers2

5

In the Bash and Zsh shells, you can use $'' to allow escaped single quotes.

echo $'I wouldn\'t'

This also keeps $1 from being interpreted by bash and available to perl too.

perl -nle $'print $1 if /\'(.*)\'/' < /tmp/boobomb

also see https://unix.stackexchange.com/questions/30903/how-to-escape-quotes-in-shell

tripleee
  • 158,107
  • 27
  • 234
  • 292
Will
  • 1,114
  • 8
  • 19
  • This form of escaping does not work in `csh`, `dash`, or `sh`. May be worth noting that this is a `bash`/`zsh` -specific feature. – Silvio Mayolo Jan 10 '18 at 01:05
5

Use hex for the single quote (27) via hexadecimal escape, so \x27

perl -wnE'say $1 if /\x27(.*)\x27/' input.txt

This assumes a single pair of single quotes, per shown sample data on which it was tested.

zdim
  • 56,772
  • 4
  • 49
  • 75