문자 간 추출을 위한 쉘 스크립트

문자 간 추출을 위한 쉘 스크립트

아래와 같이 키워드 "/"와 "NET" 사이의 파일에서 사용자 이름을 추출하고 싶습니다. 이 작업을 수행하려면 어떻게 해야 합니까? 또는 이와 유사한 프로그램을 사용할 awksed있습니까 cut?

에서:

audit: command=true   rsa1/[email protected] running
audit: command=true   user2/[email protected] executing

도착하다:

audit: command=true   rsa1 running
audit: command=true   user2 executing

답변1

사용 sed:

< inputfile sed 's/\/.*NET//' > outputfile

sed로컬에서 사용:

sed -i.bak 's/\/.*NET//' inputfile

명령 #1 실패:

  • < inputfile: 콘텐츠를 's' inputfile로 리디렉션합니다.sedstdin
  • > outputfilesed:컨텐츠를 stdout다음으로 리디렉션합니다.outputfile

명령 #2 분할:

  • -i.bak: sed백업 파일을 강제 생성하고 inputfile.bak그 자리에서 편집합니다.inputfile
  • inputfile: 강제 sed판독 입력inputfile

정규식 분해:

  • s: 교체를 수행하도록 어설션
  • /: 검색 모드 시작
  • \/: /문자 와 일치합니다.
  • .*NETNET: 문자열이 끝나기 전의 모든 문자와 일치합니다.
  • /: 검색 모드 중지/교체 모드 시작
  • /: 교체 모드 중지

예제 출력:

~/tmp$ cat inputfile
audit: command=true   rsa1/[email protected] running
audit: command=true   user2/[email protected] executing
~/tmp$ < inputfile sed 's/\/.*NET//' > outputfile
~/tmp$ cat outputfile 
audit: command=true   rsa1 running
audit: command=true   user2 executing
~/tmp$ sed -i.bak 's/\/.*NET//' inputfile
~/tmp$ cat inputfile.bak
audit: command=true   rsa1/[email protected] running
audit: command=true   user2/[email protected] executing
~/tmp$ cat inputfile
audit: command=true   rsa1 running
audit: command=true   user2 executing
~/tmp$ 

답변2

다음을 시도해 볼 수도 있습니다 awk.

awk '{ split($3, a, "/"); $3 = a[1]; } 1' file

관련 정보