Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl print single quotes from command line script

Tags:

bash

perl

The following

echo text | perl -lnE 'say "word: $_\t$_"'

prints

word: text  text

I need

word: 'text'    'text'

Tried:

echo text | perl -lnE 'say "word: \'$_\' \'$_\'";' #didn't works
echo text | perl -lnE 'say "word: '$_' '$_'";'     #neither

How to correctly escape the single quotes for bash?

Edit:

want prepare a shell script with a couple of mv lines (for checking, before really renames the files), e.g tried to solve the following:

find . type f -print | \
perl \
  -MText::Unaccent::PurePerl=unac_string \
  -MUnicode::Normalize=NFC -CASD \
  -lanE 'BEGIN{$q=chr(39)}$o=$_;$_=unac_string(NFC($_));s/[{}()\[\]\s\|]+/_/g;say "mv $q$o$q $_"' >do_rename

e.g. from the filenames like:

Somé filénamé ČŽ (1980) |Full |Movie| Streaming [360p] some.mp4

want get the following output in the file do_rename

mv 'Somé filénamé ČŽ (1980) |Full |Movie| Streaming [360p] some.mp4' Some_filename_CZ_1980_Full_Movie_Streaming_360p_some.mp4

and after the manual inspection want run:

bash do_rename

for running the actual rename...

like image 595
kobame Avatar asked Jan 08 '23 21:01

kobame


2 Answers

You can use ASCII code 39 for ' to avoid escape hell,

echo text | perl -lnE 'BEGIN{ $q=chr(39) } say "word: $q$_$q\t$q$_$q"'
like image 123
mpapec Avatar answered Jan 18 '23 00:01

mpapec


You can use:

echo text | perl -lnE "say \"word: '\$_'\t'\$_'\""
word: 'text'    'text'

BASH allows you to include escaped double quote inside a double quote but same doesn't apply for single quoted. However while doing so we need to escape $ to avoid escaping from BASH.

like image 38
anubhava Avatar answered Jan 18 '23 01:01

anubhava