• SQL 문 찾기 (SELECT문)
-- Shell 스크립트
##
# Used : line_search.sh [file_name]
# SELECT로 시작하고 "\"로 끝나는 쿼리만 추출.
##
#파일 라인수
file_end=`wc -l $1|awk '{print $1}'`
#검색시작 라인
line_start=1  

#범위 검색 반복 Loop
while true
do
#추출 시작점 가져오기
head_start=`cat -n $1 | sed -n ${line_start},${file_end}p | grep SELECT | head -1 | awk '{print $1}'`
#추출 끝점 가져오기
head_end=`cat -n $1 | sed -n ${head_start},${file_end}p | grep \; | head -1 | awk '{print $1}'`

echo $head_start
echo $head_end

sed -n $head_start,${head_end}p $1
# 검색시작 라인 +1 (다음 검색시작점)
# line_start=`expr ${head_end} + 1`
line_start=$((head_end+1))

#파일끝과 검색시작이 같으면 종료
if [ ${file_end} -eq ${line_start} ]
then
        echo ${file_end}
        echo ${line_start}
        exit
fi
done

 

  • 업그레이드 한 내용
#!/bin/sh

file_end=`wc -l $1|awk '{print $1}'`
line_start=1  
head_start=${line_start}
head_end=${file_end}

get_line ()
{
        set -A arr_st
        set -A arr_en
        arr_st[0]=1

        i=1
        j=0
        for a in `grep -n -E "SELECT|UPDATE|INSERT" $1|cut -d ':' -f1`
        do
                arr_st[i]=`grep -n -E "SELECT|UPDATE|INSERT" $1|cut -d ':' -f1`
                arr_en[j]=`grep -n -E "param|dual|[)];" $1|cut -d ':' -f1`
                #i=`expr ${i} + 1`
                i=$((i+1))
                #j=`expr ${j} + 1`
                j=$((j+1))
        done
}

while true
do

    head_start=`cat -n $1 | sed -n ${line_start},${file_end}p | grep -i -E "SELECT|UPDATE|INSERT|DELETE" |grep -v EXECUTE | head -1 | cut -f1 | tr -d ' '`

    if [[ "${head_start}" = "" ]];
    then
            echo "2: head_start empty !"
            echo ${head_start}
            exit
    else
            #echo "->|"${head_start}"|"
            head_end=`cat -n $1 | sed -n ${head_start},${file_end}p | grep -i -E "order|group|where|[)]|dual|[)];"| head -1 | awk '{print $1}' | tr -d ' '`
            if [[ "${head_start}" = "${head_end}" ]];
            then
              #head_end=`expr ${head_start} + 0`
              head_end=$((head_start+0))
            else
              #head_end=`expr ${head_end}`
              head_end=$((head_start))
            fi
    fi

    sed -n $head_start,${head_end}p $1 | sed 's/^ *//g'

     if [[ "${head_start}" = "${head_end}" ]];
     then
       line_start=$((head_end+1))
     else
       line_start=$((head_end+2))
     fi

    if [ ${file_end} -eq ${line_start} ]
    then
            echo ${file_end}
            echo ${line_start}
            exit
    fi

done

'OS > 리눅스' 카테고리의 다른 글

find 명령어 사용  (0) 2023.11.15
서버 모니터링 vmstat 스크립트  (0) 2023.08.28
리눅스 백스페이스 ^? / ^H 해결법  (0) 2023.04.04

+ Recent posts