Bareword encontrado donde el operador esperaba en -e línea 1, cerca de "9A"

¿Por qué recibo un error de sintaxis?

% perl -ne 'if (/https://([-.0-9A-Za-z]+\.[-0-9A-Za-z]+)/) { print $1 ; }'
Bareword found where operator expected at -e line 1, near "9A"
        (Missing operator before A?)
Bareword found where operator expected at -e line 1, near "9A"
        (Missing operator before A?)
syntax error at -e line 1, near "9A"
syntax error at -e line 1, near ";}"
Execution of -e aborted due to compilation errors.

preguntado el 27 de noviembre de 13 a las 05:11

You weren't paying attention to my answer to your question on sed, awk, perl or lex — find strings by prefix+regex ignoring rest of input. It demonstrated the techniques shown in the answers here. -

@JonathanLeffler, I did this independently (based on stackoverflow.com/a/20172099/1122270) and when your answer only had sed in it (note the missing -l here, too), but thanks for your help! -

2 Respuestas

If the regex contains slashes, use a different character and the explicit m operador:

perl -ne 'if (m%https://([-.0-9A-Za-z]+\.[-0-9A-Za-z]+)%) { print $1 ; }'

o:

perl -ne 'print $1 if m{https://([-.0-9A-Za-z]+\.[-0-9A-Za-z]+)}'

respondido 27 nov., 13:05

You need backslashes in front of the // after https:

perl -ne 'if (/https:\/\/([-.0-9A-Za-z]+\.[-0-9A-Za-z]+)/) { print $1 ; }'

Otherwise it thinks the regex is already over.

respondido 27 nov., 13:05

duh, makes sense! \o\ |o| /o/ Is there any other way, to avoid the hand waving? ^_^ - cnst

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.