awk 에서 shell 변수 사용하기

Posted at 2009/02/13 19:56 // in Tips/Utility programs // by Daniel
awk 에서 shell 변수 사용하기 - http://devfrog.egloos.com/293560

#!/bin/sh
if [ $# -ne 4 ]
then
    echo "Usage: log_cnt <file_name> <err_code> <log_point|0> <proc_hour>"
    exit
fi
cnt=1
if [ $3 -ne 0 ]
then
cat $1 | awk 'BEGIN{FS=";"}{ if ( $10 == '"${2}"' && $7 == '"${3}"' ) print }' | grep ^$4 | wc -l
else
while [ "$cnt" -ne 5 ]
do
    echo "log point $cnt: c"
    cat $1 | awk 'BEGIN{FS=";"}{ if ( $10 == '"${2}"' && $7 == '"${cnt}"' ) print }' | grep ^$4 | wc -l
    cnt=`expr $cnt + 1`
done
fi
-----
shell script 에서 awk 표현식 안에서 shell 의 input parameter 를 쓸 경우가 있다.
이때, awk 에서 shell 변수로 인식시키기 위해서는 ' " ${변수명} "  ' 과 같은 방식으로 사용해야 한다.

크리에이티브 커먼즈 라이센스
Creative Commons License

이 글에는 트랙백을 보낼 수 없습니다

  1. 비밀방문자

    2010/01/30 02:37 [수정/삭제] [답글]

    관리자만 볼 수 있는 댓글입니다.