이 sed 명령 줄이 내가 생각했던 것처럼 작동하지 않는 이유는 무엇입니까?

이 sed 명령 줄이 내가 생각했던 것처럼 작동하지 않는 이유는 무엇입니까?

달리기

curl -s 'https://www.idealista.com/inmueble/94238881/' \
  -H 'authority: www.idealista.com' \
  -H 'cache-control: max-age=0' \
  -H 'upgrade-insecure-requests: 1' \
  -H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36' \
  -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
  -H 'sec-gpc: 1' \
  -H 'sec-fetch-site: same-origin' \
  -H 'sec-fetch-mode: navigate' \
  -H 'sec-fetch-user: ?1' \
  -H 'sec-fetch-dest: document' \
  -H 'referer: https://www.idealista.com/usuario/favoritos/' \
  -H 'accept-language: en-US,en;q=0.9' \
  -H $'cookie: didomi_token=eyJ1c2VyX2lkIjoiMTc5MTljZjItMzUxNi02NmRjLTk0YjYtNTM3ODFiMjY1NGU5IiwiY3JlYXRlZCI6IjIwMjEtMDQtMjhUMTg6NDg6MzIuMDk5WiIsInVwZGF0ZWQiOiIyMDIxLTA0LTI4VDE4OjQ4OjMyLjA5OVoiLCJ2ZW5kb3JzIjp7ImRpc2FibGVkIjpbInR3aXR0ZXIiLCJnb29nbGUiLCJmYWNlYm9vayIsImM6bWl4cGFuZWwiLCJjOmlkZWFsaXN0YS1mZVJFamUyYyIsImM6aWRlYWxpc3RhLUx6dEJlcUUzIiwiYzphYnRhc3R5LUxMa0VDQ2o4IiwiYzpob3RqYXIiLCJjOnlhbmRleG1ldHJpY3MiLCJjOmJlYW1lci1IN3RyN0hpeCIsImM6dGVhbGl1bWNvLURWRENkOFpQIiwiYzpjaGFyYmVhdC1aNFFrOENhaCJdfSwicHVycG9zZXMiOnsiZGlzYWJsZWQiOlsiZ2VvbG9jYXRpb25fZGF0YSIsImFu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iQHTAdQA9ACGwEOgIiARUAi8BIICRAEqAJsATsAocBTQCrAFigLQgWwBbIC4AFyALtAXeAvMBgwDCQGGgMPAYkAxgBjwDJAGTgMqAZYAy4BnIDPgGiQNIA0kBpYDTgGqgNYAbGA4uByQHKgOXAdGA6wB44D0gHqhILYACAAFwAUABUADIAHIAPABAACIAGEANAA1AB5AEMARQAmABPgCqAKwAWAAuABvADmAHoAQgAhoBEAESAI6ASwBLgCaAFKALcAYYAyABlwDUANUAbIA7wB7AD4gH2AfoBAACBwEXARgAjQBHACUgFBAKWAU8Aq4BcwC_AGKANYAbQA3ABvADiAHoAPkAhsBDoCLwEiAJiATKAmwBOwChwFIgLFAWgAtgBcgC7wF5gMCAYMAwkBhoDDwGRAMkAZOAy4BnIDPgGkANOgawBrMDkQOVAcuA6MB1gDxxkBwACgAQwAmABcAEcAMsAagA7IB9gH4ARgAjgBSwCrgFbAN4AmIBNgC0QFsALzAYEAw8BkQDOQGeAM-AckA5QVAfAAoAEMAJgAXABHADLAGoAOwAfgBGACOAFLAKvAWgBaQDeAJBATEAmwBTYC2AFyALzAYEAw8BkQDOQGeAM-AbkA5IBygAA.YAAAAAAAAAAA; smc="{}"; userUUID=29668197-3ec8-4380-83a0-af74497d4652; askToSaveAlertPopUp=true; cookieSearch-1="/areas/venta-terrenos/con-precio-hasta_30000,precio-desde_5000,metros-cuadrados-mas-de_500,metros-cuadrados-menos-de_5000,terrenos-urbanos/?shape=%28%28uyxcF%7Cqxl%40%7BxEyrlCput%40ghjBfnw%40%7Dnr%40rlxA%7Dj_AjdWq%60xAr%60r%40_pUvdy%40rwg%40r%7CLr%7BZmfy%40f%7DsB_%7EwBb_aDyj%7DA%7CsoBupaA%7CaO%29%29:1631778650981"; uc="jNLcI107+7z1wRs0x4TdO3u5jcaE+Wrl/o5Drt4SE9qHCxSMOrDfJCS1OVr9tkKQ1xbkhwtCXOZOKw1BLkvMTAMsML+Z10HjHWdUIRhRkRXsEcNnPFEt9rqCk0DCCd7EBSE6A/jp5vs="; nl="wrtrmuF9QzNOYYO2P8SN3OqyjHQXevAY7aYvx0cKdUNwML7qYn47dSs63/pFStgOTH50K6V1y0hMkNG4T70na63g0fJdDSpgDegfruZFCA9GnVx058kgR638a8Q81Gz9r1nzfAqJdfs="; SESSION=2e11a953aead5c1f~d77af03d-2746-4657-89a6-ab25cb02e889; contactd77af03d-2746-4657-89a6-ab25cb02e889="{\'email\':\'EqU9vfX+DxXdzl+iReclTDED4UB/DY6iSfTeSLvLlsE=\',\'phone\':\'634821160\',\'phonePrefix\':null,\'friendEmails\':null,\'name\':\'RL9PoIsz5dW5rvLNF/0gvA==\',\'message\':null,\'message2Friends\':null,\'maxNumberContactsAllow\':10,\'defaultMessage\':true}"; sendd77af03d-2746-4657-89a6-ab25cb02e889="{\'friendsEmail\':null,\'email\':\'EqU9vfX+DxXdzl+iReclTDED4UB/DY6iSfTeSLvLlsE=\',\'message\':null}"; datadome=Gegr2x~z62hH2U.ay-n4sMd3xgi-RaB1X5XMr4i5qV2q6GYsINszxSNDS732-spxaAaUp.m7aGMcOgN-DcAxFY9KCQsldTsDl-RVS5ocEm; cc=eyJhbGciOiJIUzI1NiJ9.eyJjdCI6OTk2MjMyNiwiZXhwIjoxNjMxOTUxOTc4fQ.0YJZGR34jgb1SWTAzL9DFPhAeeP4k0hE9igRE8-SQp0' \
--compressed | grep -m 1 "<meta name=\"description\" content="

나에게 줄 것입니다 :

<meta name="description" content="terreno de 936 m², Terreno en venta en paseo Blasco Ibáñez s/n, Costa Esuri, Ayamonte, Costa Esuri">

HTML 없이 텍스트만 가져오기 위해 적용을 시도했고 sed예상대로 작동하는 코드를 얻었습니다.

curl -s 'https://www.idealista.com/inmueble/94238881/' \
  -H 'authority: www.idealista.com' \
  -H 'cache-control: max-age=0' \
  -H 'upgrade-insecure-requests: 1' \
  -H 'user-agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Safari/537.36' \
  -H 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9' \
  -H 'sec-gpc: 1' \
  -H 'sec-fetch-site: same-origin' \
  -H 'sec-fetch-mode: navigate' \
  -H 'sec-fetch-user: ?1' \
  -H 'sec-fetch-dest: document' \
  -H 'referer: https://www.idealista.com/usuario/favoritos/' \
  -H 'accept-language: en-US,en;q=0.9' \
  -H $'cookie: didomi_token=eyJ1c2VyX2lkIjoiMTc5MTljZjItMzUxNi02NmRjLTk0YjYtNTM3ODFiMjY1NGU5IiwiY3JlYXRlZCI6IjIwMjEtMDQtMjhUMTg6NDg6MzIuMDk5WiIsInVwZGF0ZWQiOiIyMDIxLTA0LTI4VDE4OjQ4OjMyLjA5OVoiLCJ2ZW5kb3JzIjp7ImRpc2FibGVkIjpbInR3aXR0ZXIiLCJnb29nbGUiLCJmYWNlYm9vayIsImM6bWl4cGFuZWwiLCJjOmlkZWFsaXN0YS1mZVJFamUyYyIsImM6aWRlYWxpc3RhLUx6dEJlcUUzIiwiYzphYnRhc3R5LUxMa0VDQ2o4IiwiYzpob3RqYXIiLCJjOnlhbmRleG1ldHJpY3MiLCJjOmJlYW1lci1IN3RyN0hpeCIsImM6dGVhbGl1bWNvLURWRENkOFpQIiwiYzpjaGFyYmVhdC1aNFFrOENhaCJdfSwicHVycG9zZXMiOnsiZGlzYWJsZWQiOlsiZ2VvbG9jYXRpb25fZGF0YSIsImFuYWxpdGljYXMtZHlGVkdSZTgiXX0sInZlcnNpb24iOjIsImFjIjoiQUFBQS5BQUFBIn0=; euconsent-v2=CPFYMwBPFYMwBAHABBENBXCgAAAAAAAAAAAAAAAAAAEBoFAAVgAuACGAGQAMsAagA2QB2AD8AIAAQUAjABSwCngFXgLQAtIBrADeAHVAPkAhsBDoCKgEXgJEATYAnYBSIC5AGBAMJAYeAxgBk4DOQGeAM-AckA5QB1hKB6AAgABYAFAAMgAcABFADAAMQAeABEACYAFUALgAX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AfoBAACBwEXARgAjQBHACUgFBAKWAU8Aq4BcwC_AGKANYAbQA3ABvADiAHoAPkAhsBDoCLwEiAJiATKAmwBOwChwFIgLFAWgAtgBcgC7wF5gMCAYMAwkBhoDDwGRAMkAZOAy4BnIDPgGkANOgawBrMDkQOVAcuA6MB1gDxxkBwACgAQwAmABcAEcAMsAagA7IB9gH4ARgAjgBSwCrgFbAN4AmIBNgC0QFsALzAYEAw8BkQDOQGeAM-AckA5QVAfAAoAEMAJgAXABHADLAGoAOwAfgBGACOAFLAKvAWgBaQDeAJBATEAmwBTYC2AFyALzAYEAw8BkQDOQGeAM-AbkA5IBygAA.YAAAAAAAAAAA; smc="{}"; userUUID=29668197-3ec8-4380-83a0-af74497d4652; askToSaveAlertPopUp=true; cookieSearch-1="/areas/venta-terrenos/con-precio-hasta_30000,precio-desde_5000,metros-cuadrados-mas-de_500,metros-cuadrados-menos-de_5000,terrenos-urbanos/?shape=%28%28uyxcF%7Cqxl%40%7BxEyrlCput%40ghjBfnw%40%7Dnr%40rlxA%7Dj_AjdWq%60xAr%60r%40_pUvdy%40rwg%40r%7CLr%7BZmfy%40f%7DsB_%7EwBb_aDyj%7DA%7CsoBupaA%7CaO%29%29:1631778650981"; uc="jNLcI107+7z1wRs0x4TdO3u5jcaE+Wrl/o5Drt4SE9qHCxSMOrDfJCS1OVr9tkKQ1xbkhwtCXOZOKw1BLkvMTAMsML+Z10HjHWdUIRhRkRXsEcNnPFEt9rqCk0DCCd7EBSE6A/jp5vs="; nl="wrtrmuF9QzNOYYO2P8SN3OqyjHQXevAY7aYvx0cKdUNwML7qYn47dSs63/pFStgOTH50K6V1y0hMkNG4T70na63g0fJdDSpgDegfruZFCA9GnVx058kgR638a8Q81Gz9r1nzfAqJdfs="; SESSION=2e11a953aead5c1f~d77af03d-2746-4657-89a6-ab25cb02e889; contactd77af03d-2746-4657-89a6-ab25cb02e889="{\'email\':\'EqU9vfX+DxXdzl+iReclTDED4UB/DY6iSfTeSLvLlsE=\',\'phone\':\'634821160\',\'phonePrefix\':null,\'friendEmails\':null,\'name\':\'RL9PoIsz5dW5rvLNF/0gvA==\',\'message\':null,\'message2Friends\':null,\'maxNumberContactsAllow\':10,\'defaultMessage\':true}"; sendd77af03d-2746-4657-89a6-ab25cb02e889="{\'friendsEmail\':null,\'email\':\'EqU9vfX+DxXdzl+iReclTDED4UB/DY6iSfTeSLvLlsE=\',\'message\':null}"; datadome=Gegr2x~z62hH2U.ay-n4sMd3xgi-RaB1X5XMr4i5qV2q6GYsINszxSNDS732-spxaAaUp.m7aGMcOgN-DcAxFY9KCQsldTsDl-RVS5ocEm; cc=eyJhbGciOiJIUzI1NiJ9.eyJjdCI6OTk2MjMyNiwiZXhwIjoxNjMxOTUxOTc4fQ.0YJZGR34jgb1SWTAzL9DFPhAeeP4k0hE9igRE8-SQp0' \
--compressed | grep -m 1 "<meta name=\"description\" content=" | sed -E 's;^.*(content=\");;;s;\">$;;' 

내 질문은 왜 작동하지 않습니까 sed -E 's;^.*(content=\")\">$;;'? 그 목적은 나에게 다음 결과를 제공하는 것입니다.

terreno de 936 m², Terreno en venta en paseo Blasco Ibáñez s/n, Costa Esuri, Ayamonte, Costa Esuri 

그리고 그것은 나에게 다듬어지지 않은 것을 주었다

<meta name="description" content="terreno de 936 m², Terreno en venta en paseo Blasco Ibáñez s/n, Costa Esuri, Ayamonte, Costa Esuri">

한 사람은 가야 할 수도 있습니다https://www.idealista.com/inmueble/94238881/, 그런 다음 엽니다.개발자 도구브라우저에서cURL로 복사이 예를 가지고 놀아보세요.

답변1

정규식에는 두 가지 문제가 있습니다. 이는 정규식이 어떻게 작동하는지에 대한 오해 때문일 수 있습니다.

  1. ()캡처 그룹을 나타내는 데 특수 문자를 사용할 수 있는 "확장" 정규식 구문을 사용했습니다 . 그러나 일치 메커니즘 자체를 방해하지는 않습니다. 캡처 그룹을 사용하지 않으므로 정규식은 다음과 같습니다.
    ^.*content=\"\">$
    
    예상되는 패턴 content=""비어 있는인용된 문자열,이어서 폐막식>. 입력한 내용에서는 이런 일이 발생하지 않으므로 sed일치가 이루어지지 않아 아무 작업도 수행되지 않습니다. (그런데, 이스케이프할 필요는 없습니다 ". 정규식에서는 특별하지 않으며 프로그램은 작은따옴표로 묶여 있으므로 쉘도 이를 잘못 해석하지 않습니다 ".)
  2. 이렇게 수정해도
    ^.*content="[^"]*".*>$
    
    기준점으로 인해 일치하는 선 부분을 교체합니다.모두빈 문자열이 포함되어 있으므로 귀하의 경우에는 아무것도 남지 않습니다.

이를 완화하려면 캡처 그룹을 사용하는 원래 아이디어를 재사용하되 이를 사용하여 줄의 관련 부분을 포함하고 다음과 같이 전체 줄을 캡처 그룹의 내용으로 바꿔야 합니다.

sed -E 's;^.*content="([^"]*)".*$;\1;'

"이는 속성 이름 뒤의 첫 번째와 다음 사이의 모든 항목을 content=캡처링 그룹으로 정의하지만 그렇지 않으면 전체 줄과 일치합니다. 그런 다음 표현식을 통해 전체 줄을 캡처 그룹의 내용으로만 바꿉니다 \1.

관련 정보