awk에서 정규표현식 matching

다음의 명령을 사용하고자 한다면

command | grep pattern | awk '{print $3}'

이 명령은 awk에서의 정규표현식 matching기능을 이용하여 다음과 같이 간단히
사용할 수 있다.

command | awk '/pattern/{print $3}'

--------------------<원문>---------------------
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

                             UNIX GURU UNIVERSE
                                UNIX HOT TIP

                       Unix Tip 3171 - October 12, 2009

                   http://www.ugu.com/sui/ugu/show?tip.today

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

REGEXP MATCHING IN AWK

If you ever find yourself typing "command | grep pattern | awk '{print $3}'
you can shorten this by using the regexp matching in awk, like this:

command | awk '/pattern/{print $3}'

반응형
Posted by She쥐포s

Unix 쉘에서의 산술 비교

Unix 쉘에서의 산술 비교는 정수값에 한정되어 있다. 다음은 기본적인
쉘 명령을 사용하여 부동 소수값을 비교하는 팁이다.

--------- CUT HERE-----------------
#! /bin/sh
# test shell script
n1="01.401"
n2="01.350"

function compareFloatSmall
{
sort -n <<: | head -1
$n1
$n2
:
}

function compareFloatGreat
{
sort -r -n <<: | head -1
$n1
$n2
:
}

small=$(compareFloatSmall $n1 $n2)
echo "Comparing $n1 to $n2: smaller $less"
great=$(compareFloatGreat $n1 $n2)
echo "Comparing $n1 to $n2: greater $great"
--------- CUT HERE-----------------

다른 방법으로 'awk' 프로그램을 사용할 수 있다.
--------- CUT AGAIN HERE-----------
#! /bin/sh
# A couple of examples in awk.
n1="03.550"
n2="02.550"

echo "$n1 $n2" | awk '{
       if ( $1 >= $2 ) print $1
       if ( $1 <= $2 ) print $2
       if ( $1 >  $2 ) print $1
       if ( $1 <  $2 ) print $2
       if ( $1 == $2 ) print $1, $2
}'
--------- CUT AGAIN HERE-----------

<><><><><><><><><><>원 문<><><><><><><><><><><>
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

                             UNIX GURU UNIVERSE
                                UNIX HOT TIP

                       Unix Tip 2926 - February  9, 2009

                   http://www.ugu.com/sui/ugu/show?tip.today

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=


ARITHMETIC COMPARISON

In UNIX shell arithmetic comparison
is limited to integer values.  Here
is a tip to compare floating values
using basic shell commands.

--------- CUT HERE-----------------

#! /bin/sh
# test shell script
n1="01.401"
n2="01.350"

function compareFloatSmall
{
sort -n <<: | head -1
$n1
$n2
:
}

function compareFloatGreat
{
sort -r -n <<: | head -1
$n1
$n2
:
}

small=$(compareFloatSmall $n1 $n2)
echo "Comparing $n1 to $n2: smaller $less"
great=$(compareFloatGreat $n1 $n2)
echo "Comparing $n1 to $n2: greater $great"

--------- CUT HERE-----------------

Alternatively you can use a small 'awk' program.

--------- CUT AGAIN HERE-----------
#! /bin/sh
# A couple of examples in awk.
n1="03.550"
n2="02.550"

echo "$n1 $n2" | awk '{
       if ( $1 >= $2 ) print $1
       if ( $1 <= $2 ) print $2
       if ( $1 >  $2 ) print $1
       if ( $1 <  $2 ) print $2
       if ( $1 == $2 ) print $1, $2
}'
--------- CUT AGAIN HERE-----------

반응형
Posted by She쥐포s

어떤 명령을 실행한 결과에서 일정패턴을 찾아 그 결과중 일정부분을 찾으려고 했을때
다음과 같이 입력하는 경험을 해본적이 있을 것이다.
물론 나도 그랬으니까...

$ command | grep PATTERN | awk '{ print $3 }'

위의 문장은 awk의 특성을 이용하여 다음과 같이 줄여 쓸 수 있다.

$ command | awk '/PATTERN/{ print $3 }'

-------------------------<원문>-------------------------
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
                             UNIX GURU UNIVERSE
                                UNIX HOT TIP
                       Unix Tip 3171 - October 11, 2008
                   http://www.ugu.com/sui/ugu/show?tip.today
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

REGEXP MATCHING IN AWK

If you ever find yourself typing "command | grep pattern | awk '{print $3}'
you can shorten this by using the regexp matching in awk, like this:

command | awk '/pattern/{print $3}'

반응형
Posted by She쥐포s
OS와 ps가 지원하는 포맷에 따라 달라지지만 다음의 내용은 Solaris에서 작동한다.

# ps -eo etime,pid,args | awk -F- '$1>14{print}'

etime은 일반적인 프로세스 시작시간 대신 총 소요된 시간을 반환한다.
etime 값은 dd-HH:MM:SS 의 형식이다.(날짜-시간:분:초)

○ 명령어 설명
- ps -e
    현재 실행중인 모든 프로세스의 정보를 나열한다.
- ps -o format
    format에 명시한 포맷으로 정보를 출력한다.
- etime
    프로세스가 실행된 소요시간 출력
- pid
    Process ID 출력
- args
    명령어와 모든 인수를 문자열로 출력
    기타 자세한 포맷에 대한 정보는 man ps를 이용해 확인하기 바란다.
- awk -F- '$1>14{print}'
    -를 구분자로 하여 첫번째 항목을 찾아 14이상인 항목만 출력한다.
     위의 예에서는 실행한지 15일 이상이 되는 프로세스를 출력하는 예이다.
반응형
Posted by She쥐포s
o 단순히 빈라인(※ 스페이스나 탭을 사용하지 않은 순수한 빈라인)의 수를 알아내기 위해서는
   다음의 명령을 사용하면 된다.

cat FILENAME | awk '/^$/ { ++x } END { print "No. of Blank line = " x }'
반응형
Posted by She쥐포s

퍼왔습니다.
http://devfrog.egloos.com/tb/293560

awk 에서 shell 변수 사용하기

#!/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 변수로 인식시키기 위해서는 ' " ${변수명} "  ' 과 같은 방식으로 사용해야 한다.

반응형
Posted by She쥐포s
이전버튼 1 이전버튼

블로그 이미지
She쥐포s
Yesterday
Today
Total

달력

 « |  » 2024.3
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

최근에 올라온 글

최근에 달린 댓글

글 보관함