로그수집기

박동규·2022년 11월 26일
0

로그수집기

https://github.com/dongkyu-park/linux-log-collector

https://velog.io/@kyu/%EB%A6%AC%EB%88%85%EC%8A%A4-%EB%AA%85%EB%A0%B9%EC%96%B4

사용했던 명령어들

  • 파일 내용만 삭제
cat /dev/null > 해당파일
  • 파일의 문자열 한 줄씩 읽기
i=1

while read line || [ -n "$line" ] ; do
  echo "Line $i: $line"
  ((i+=1))
done < file.txt
  • '[ -n "$line" ]' 조건이 필요한 이유

read는 Line에 개행문자가 포함되어있어야 Line으로 인식하고 True를 리턴합니다. 만약 마지막 줄을 읽었을 때, 개행문자가 없다면 False가 리턴되어 반복문이 종료됩니다. 이 때문에 마지막 Line가 처리되지 않을 수 있습니다.

테스트 연산자 -n은 문자열의 길이가 0이 아닐 때 True를 리턴합니다. 마지막 Line에 개행문자가 없어도 Empty String이 아니기 때문에 True가 되어 반복문이 수행됩니다.

#!/bin/bash 

while read A B C ETC
do 
    echo "${A}-${B}-${C}-${ETC}"
done < test.txt

>>> 결과 <<<
a-A-1-aaa AAA 111
b-B-2-bbb BBB 222
...
  • wc : (word count) 사용자가 지정한 파일의 행, 단어, 문자수를 세는 프로그램
$ wc  [-clmw] [fileName]

기본형식 : wc 옵션 파일이름
파일이름을 입력하지 않으면 표준 입력으로 부터 정보를 받아들여 계산한다.

>>> 옵션 <<<
-l 행
-w 단어
-c 문자
  • gz 압축 풀기
gzip -d {압축 파일명}.gz
  • += 연산자
i=0

((i+=1))

위와 같은 형태로 사용

  • 파일 삭제
rm [옵션][삭제 할 디렉토리/파일]

test.txt 삭제
ex) rm test.txt new_folder

/home/user/test.txt 삭제
ex) rm /home/user/test.txt

new_folder 디렉토리 삭제
ex) rm -r new_folder

new_folder 디렉토리 삭제 시 삭제 확인 메시지를 출력하지 않음
ex) rm -rf new_folder
  • 중복 제거하고 횟수 세기
# input.txt

bat
abc
apple
Abc
BALL
ABc
bat

sort input.txt | uniq -c

>>> 결과 <<<
	  1 abc
      1 Abc
      1 ABc
      1 apple
      1 BALL
      2 bat
  • 파일 정렬하고 내용 덮어쓰기
sort [대상파일명] -o [정렬된내용을저장할파일명]

동일한 파일에 sorted된 내용을 덮어쓰고 싶다면
ex) sort a.txt -o a.txt

  • 변수에 값 할당
VAR1=1004
VAR2=hello
VAR3="hello"
VAR4='hello'

# 큰따옴표/따옴표를 사용하는 습관을 들이자
VAR5=hello world # NO
VAR6="hello world" # OK

쉘 스크립트는 기본적으로 값의 타입이 없고(untyped) 모두 문자열로 인식된다. 공백이 들어간 문자열이든 그렇지 않든, 문자열 값을 큰따옴표/따옴표로 감싸주는 습관을 들이는 것이 이상적이라고 함. 큰따옴표/따옴표를 사용하여 문자열을 감싸주든, 아무것도 사용하지 않든 똑같은 문자열

  • 리눅스의 압축 방식

윈도우즈(Windows) 에서의 압축은 zip 등의 방식으로 파일이나 폴더들을 묶음과 동시에 압축(compress) 하는 것을 의미하나

리눅스(Linux)에서는 파일이나 폴더들을 묶는 것(archive)과 실제로 압축(compress)하는 기능으로 세분화 되어있었다.

리눅스에서 여러 파일을 한 파일로 묶은 것아카이브(archive)라 하며 확장자는 .tar (tape archives)이다.

그리고 보통 위에서 생성된 tar(아카이브) 파일을 다시 gzip을 사용하여 압축해서 .tar.gz의 확장자를 가지는 압축 아카이브파일을 실무에서 많이 사용하고 있다.

참조 :
https://codechacha.com/ko/shell-script-read-file/
https://young-cow.tistory.com/33
https://codechacha.com/ko/shell-script-create-file-append-strings/
파일 한 줄씩 읽어오기 : http://www.dreamy.pe.kr/zbxe/CodeClip/3765968
로딩바 : https://nirsa.tistory.com/19
/dev/null 로 출력 버리기 : https://minsone.github.io/shell/shell-dev-null
tail + grep : https://coding-factory.tistory.com/802
find, grep, awk : https://jybaek.tistory.com/704
/dev/null 2>&1 & 이란 : https://unluckyjung.github.io/linux/2020/11/11/md-Dev_Null/
리눅스 기본 명령어 : https://cocoon1787.tistory.com/717
gz 압축하기, 압축풀기 : https://araikuma.tistory.com/121
로그 추출 쉘 스크립트 : https://nirsa.tistory.com/22
log 카운트 추출 : https://powerkkim.tistory.com/entry/log%EB%B6%84%EC%84%9D-2-%ED%86%B0%EC%BA%A3-Accesslog-%ED%86%B5%EA%B3%84-count-%EC%B6%94%EC%B6%9C-feature-awk-grep
리눅스 압축 해제 방식 : https://ifuwanna.tistory.com/31
Bash Shell - 숫자 변수 증가/감소 시키기 : https://codechacha.com/ko/shell-script-increment-decrement/
wc 로 파일 라인 수 구하기 : https://taesany.tistory.com/96%20wc%20-l
echo 옵션 : https://rhrhth23.tistory.com/21
변수에 값 할당 : https://mong9data.tistory.com/134
문자열 포함여부 확인하기 : https://yooloo.tistory.com/132
vi 전체 복사하기, 붙여넣기 방법 : https://selfdevelope.tistory.com/225
파일 삭제 : https://coding-factory.tistory.com/752
중복을 제거하는 uniq : https://www.lesstif.com/lpt/linux-uniq-95879394.html
파일 정렬하고 내용 덮어쓰기 : https://seongtak-yoon.tistory.com/64
폴더 안의 모든 압축 파일 풀기 : https://sexydatadesigner.tistory.com/108

0개의 댓글