리눅스 역참조(후방참조(\1)) 어떻게 사용하는 건가요?

조회수 806회
Oct 8 21:40:00 cent unix_chkpwd[1480]: password check failed for user (user1)
Oct 8 21:40:00 cent sshd[1478]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=10.221.55.2 user=user1
Oct 8 21:40:02 cent sshd[1478]: Failed password for user1 from 10.211.55.2 port 53639 ssh2
Oct 8 21:40:05 cent sshd[1479]: Connection closed by 10.211.55.2
Oct 8 21:40:08 cent sshd[1478]: Failed password for user1 from 10.211.55.21 port 53655 ssh2
Oct 8 21:40:12 cent sshd[1478]: Failed password for user1 from 10.211.55.18 port 53619 ssh2
Oct 8 21:40:19 cent sshd[1478]: Failed password for user1 from 10.211.55.21 port 53655 ssh2
Oct 8 21:40:32 cent sshd[1478]: Failed password for user1 from 10.211.55.18 port 53619 ssh2
Oct 8 21:40:37 cent sshd[1478]: Failed password for user1 from 10.211.55.18 port 53619 ssh2
Oct 8 21:40:46 cent sshd[1478]: Failed password for user1 from 10.211.55.21 port 53655 ssh2
Oct 8 21:40:49 cent sshd[1479]: Connection closed by 10.211.55.21
Oct 8 21:40:52 cent sshd[1478]: Failed password for user1 from 10.211.55.18 port 53619 ssh2
Oct 8 21:40:55 cent sshd[1478]: Failed password for user1 from 10.211.55.18 port 53619 ssh2
Oct 8 21:41:02 cent sshd[1479]: Connection closed by 10.211.55.18

이런 내용이 있는 abc.log 파일에서 Failed password for user1 from의 from 뒤에 오는 ip 주소만 출력하고 싶은데 "from (.*)" 와 역참조 \1를 사용하면 된다고 하더라고요. 그런데 이 \1 역참조를 어떻게 사용하는지 아무리 검색해도 잘 안 나와서 여기서 질문 올립니다.

sed -n '/Failed password for user1 from/p' abc.log

이게 Failed password for user1 from가 포함된 라인만 출력하는 건데 여기서 어떻게 \1를 사용해야from 뒤에 있는 ip 주소만 출력되는지요?

Failed password for user1 from 가 너무 길어서 쉘 변수에 입력해서 사용하고 싶은데

SED_FAIL = "Failed password for user1 from" 이렇게 쉘 변수를 저장하는 게 맞을까요?

  • (•́ ✖ •̀)
    알 수 없는 사용자
  • sed -n '/Failed password for user1 from/p' abc.log | grep -Eo '([0-9]+\.){3}[0-9]+' 알 수 없는 사용자 2020.10.9 06:51
  • 파이프 사용 안 하고 sed에서 한 번에 하고 싶은데 그 방법은 없는 건가요? "from\(.*\)" 라고 from 뒷부분을 문자열을 \1로 참조하여 출력하면 된다고 해서 해보려 했는데 뭐가 잘못된 건지 계속 에러가 뜨더라고요 알 수 없는 사용자 2020.10.9 12:28

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)