3

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
  • 64,580
  • 5
  • 52
  • 81
capser
  • 2,442
  • 5
  • 42
  • 74

2 Answers2

6

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
  • 64,580
  • 5
  • 52
  • 81
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
  • 175,061
  • 34
  • 275
  • 318
Will
  • 1,206
  • 9
  • 22
  • 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