grep을 사용하여 빈라인 지우기

grep 명령어를 사용하여 빈 라인을 지우기 위해서는 다음의 명령을 사용할 수 있다.

grep -v "^ *$" INPUTFILE > OUTPUTFILE(^과 * 사이엔 스페이스가 들어감)

INPUTFILE : 빈라인을 가진 입력파일
OUTPUTFILE : 출력파일(빈라인 제외된)

만일 grep -v "^$" 만을 사용한다면 공백을 포함하는 빈 라인은 제거되지 않을 것이다.

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

                             UNIX GURU UNIVERSE
                                UNIX HOT TIP

                       Unix Tip 2945 - February 28, 2009

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

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


DELETING BLANK LINES USING GREP

To delete a blank line using a grep command you can use the next
command :-

grep -v "^ *$" file-y > file-x

where file-y is the input files has blank lines and file-x will
be the output file ( excluding the blank lines ).

if we use (  grep -v "^$" ) only , the blanck line which include
spaces will not be removed.

반응형
Posted by She쥐포s

유닉스엔 원칙적으로 확장자의 개념이 없다.
인간이 구분하기 위해 구분해 놓은 것에 불과하다.
편의상 구분해 놓은 구분자를 변경하는 방법에 관한 팁을 소개한다.

---------------------------------------------------------------------
확장자 바꾸기

여러 파일들의 확장자를 변경하고자 할 때 다음과 같은 방식을 이용할 수 없다.

    % mv *.abc *.def

대신 다음의 쉘 스크립트로 원하는 작업을 할 수 있다.

--------- 시작 ---------
#!/bin/sh
for f in *.abc; do
    mv $f `basename $f .abc`.def
done
--------- 끝 ---------

모든 .abc로 끝나는 파일을 찾아 $f를 파일명으로 바꿔가면서(for)

    mv FILENAME `basename $f.abc`.def

명령을 실행한다.

`basename $f .abc` 명령은 파일명중에서 .abc를 제외한 값을 반환한다.
따라서 위의 mv 명령은 다음과 같은 명령으로 완성된다.(file은 .abc를 제외한 파일명)

    mv file.abc file.def

    done

for 루프를 마친다.

csh이나 tcsh에서는 다음과 같이 하여 위의 작업을 할 수 있다.

    foreach f in ( *.abc )
        mv $f `basename $f .abc`.def
    end

--------- 원문 ---------

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

                             UNIX GURU UNIVERSE
                                UNIX HOT TIP

                       Unix Tip 2932 - February 15, 2009
                   http://www.ugu.com/sui/ugu/show?tip.today

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

CHANGE THE SUFFIX

If you want to change the suffix of
multiple files, you can't do:

       % mv *.abc *.def

However the following shell script
can be used to do the required
opperation:

***

Change all *.abc file to *.def the
following shell script would work:

#!/bin/sh
for f in *.abc; do
 mv $f `basename $f .abc`.def
done

How it works:

 for f in *.abc; do

Set up a look for all files ending
in .abc, and each time around setup
$f as the filename

 mv $f `basename $f.abc`.def

`basename $f .abc` takes the filename
in $f and removes any trailing occurences
of .abc, we then append .def to the result
and the resulting command becomes
"mv file.abc file.def"

  done

Ends the "for" loop above.

Under "csh" or "tcsh" a similar thing could be done with:

 foreach f in ( *.abc )
   mv $f `basename $f .abc`.def
 end

반응형
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
※ GNU vi-improved에 해당함.. 일반 Unix VI는 안됨(내용상의 오류가 있었음)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
                             UNIX GURU UNIVERSE
                                UNIX HOT TIP

                       Unix Tip 2361 - June 19, 2006

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

제어문자 제거하기

여러 파일에서 제어 문자를 제거해야하는 문제에 당면했다면, 여기 그 해답이 있다.
 
다음의 내용으로 스크립트 파일을 만들어라.
 
vi -c "%s/^M//g" -c "wq" junk1.c
vi -c "%s/^M//g" -c "wq" junk2.c
vi -c "%s/^M//g" -c "wq" junk3.c
vi -c "%s/^M//g" -c "wq" junk4.c
vi -c "%s/^M//g" -c "wq" junk5.c
vi -c "%s/^M//g" -c "wq" junk6.c


만일 이 스크립트 파일의 이름이 "xyz"라면:

#./xyz

스크립트는 junk1.c에서 junk6.c까지의 모든 파일에서 제어문자(^M)를 제거할 것이다.
 
^M을 만드려면 컨트롤을 누른상태에서 'v'와 'm'을 누른다.
 
^H를 만드려면 컨트롤을 누른상태에서 'v'와 'h'를 누른다.
 
@@@@@@@@@@@@@@@@@@@@[ 원문 ]@@@@@@@@@@@@@@@@@@@@

REMOVING CONTROL CHARACTERS

Have you ever faced problem of removing
Control Characters from Multiple Files,

Here is the solution create script file
with following entries,

vi -c "%s/^M//g" -c "wq" junk1.c
vi -c "%s/^M//g" -c "wq" junk2.c
vi -c "%s/^M//g" -c "wq" junk3.c
vi -c "%s/^M//g" -c "wq" junk4.c
vi -c "%s/^M//g" -c "wq" junk5.c
vi -c "%s/^M//g" -c "wq" junk6.c

If the filename of this script file
is "xyz":

#./xyz

This will remove the control characters(^M)
from all the files junk1.c ... junk6.c

To create ^M = Holding the control
press 'v' & 'm'.

To create ^H = Holding the control press
'v' & 'h'.
반응형
Posted by She쥐포s

bash-2.03# scp root@xxx.xxx.xxx.xxx:/export/home/nnnnn/htdocs/mmmmm/uu/abcdef.js .

파일을 날려먹어서 원본으로 부터 복구를 하려고 위의 명령어를 쳤더니..
움 뭔노무 작업을 하는데 /dev/random, /dev/urandom 장치가 필요하다고...

이너넷을 뒤져보니까 Solaris 8에는 위 장치들이 기본으로 없어서 패치를 해야한다고..
http://sunsolve.sun.com 을 가보니 또 패치를 받으려면 로그인을 하라고..

ID/PW를 까먹어서 좀 헤매다가...
로그인!

Patch Finder에서 112438을 검색해서 패치 찾음(x86은 112439)
다운로드 후 서버에 Upload
음 되도 못하게 jar 파일일세..

bash-2.03# jar xvf 112438-03.jar
bash-2.03# patchadd 112438-03
ERROR : /postpatch script contains invalid permissions. ( ㅡ.ㅡ 뭐 걸리는게 이리 많은지 )
bash-2.03# chmod 755 112438-03/postpatch
bash-2.03# patchadd 112438-03

Checking installed patches...
Verifying sufficient filesystem capacity (dry run method)...
Installing patch packages...

Patch number 112438-03 has been successfully installed.
See /var/sadm/patch/112438-03/log for details
Executing postpatch script...
Reboot client to install driver.

Patch packages installed:
  SUNWcarx
  SUNWcsr
  SUNWhea
  SUNWmdb
  SUNWmdbx
bash-2.03# sync
bash-2.03# sync
bash-2.03# sync
bash-2.03# reboot --


리부탱

bash-2.03# scp root@xxx.xxx.xxx.xxx:/export/home/nnnnn/htdocs/mmmmm/uu/abcdef.js .
명령어를 쳤더니 이번엔..
The authenticity of host 'xxx.xxx.xxx.xxx (xxx.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is ----------------------------------------
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'xxx.xxx.xxx.xxx' (RSA) to the list of known hosts.
Password:
Password:
Password:
Permission denied (gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive).
bash-2.03#
root로 접속이 안된다...
ㅡ.ㅡ

그래서 일단 localhost로 접속을 해봤다.
bash-2.03# ssh localhost
22번 포트로 access가 거부되었단다.
bash-2.03# /usr/local/sbin/sshd &
했더니 Privileged User sshd가 없단다
/etc/passwd를 보니 사용자가 없다
bash-2.03# useradd -s /bin/false sshd
bash-2.03# /usr/local/sbin/sshd &

했더니 또 /var/empty가 없다나 어쨌다나..
bash-2.03# mkdir /var/empty
bash-2.03# ssh localhost

결국엔 로컬 접속 성공(root로..)

원격서버의 sshd_config를 봤더니

# Are root logins permitted using sshd.
# Note that sshd uses pam_authenticate(3PAM) so the root (or any other) user
# maybe denied access by a PAM module regardless of this setting.
# Valid options are yes, without-password, no.
PermitRootLogin no

ㅡ.ㅡ 내가 이렇게 보안에 신경써서 설정을 했었나?
음 암튼 다른 방법으로 작업을 해야 했다.

반응형
Posted by She쥐포s
내가 리눅스를 만난지도 어언 14년이나 되었네..
동생이 PC통신하면서 지인을 통해 받았던 5.25inch Floppy 약 30장.. Slackware Linux
설치하다가 중간에 뻑나고..

또 동생이 사온 건잠머리연구소(? 클래스데이타?)의 한글리눅스, 영문리눅스 CD로
이리저리 설치하느라 궁리도 해보고 한글리눅스는 잘 되지 않아서 영문리눅스 CD로
설치하고 해상도도 제대로 못잡고 Window Manager도 제대로 설정되지 않아 twm의
껍데기도 없는 회색 체크무늬화면을 보기까지도 근 2주일이 걸렸던거 같은데...

그때만해도 설치완료까지 걸린 시간이 최소 2시간 30분??

그당시만해도 560MB HDD에서 DOS의 한계로 512MB까지 밖에 인식을 못하던 단점을
극복하고 560MB를 Full로 쓸 수 있다는 것에 매력을 느껴... "엇쭈 이것봐라.."했던 기억..

한번 물고 늘어져보자 싶어 하이텔 리눅스 동호회에서 게시판 담당자도 해보고 PC통신을
이용해 밤이면 밤마다 ppp를 이용한 서버꾸미기 놀이를 하며 새벽3시를 안 넘기면 잠이
오지 않던 기억..

그러다가 운이 좋게도 리눅스코리아라는 회사에서 사회 첫발을 디디고..
IRC를 통해 사람들을 만나면서 IO Linux라는 회사를 만들고자 했던 기억...

움 Intel Server 업체로 옮기면서 한동안 Linux와 멀어져 있다가.. 요즘들어 새롭게
공부를 하려고 하니 다시 배워야 하고, 옛기억도 되살려야 할 부분들이 엄청나게 많네..

Red Hat Enterprise Linux도 5.2가 나왔고 CentOS도 5.2가 나왔고.. 해서 CentOS 홈페이지에
있는 Guide를 보니 죄다 Red Hat Enterprise Linux 문서더구만..

Guide를 보면서 하나하나 다시 정리하고자 Linux 카테고리 및 Red Hat Linux 서브 카테고리를
등록하였다.

우리나라 리눅서중에는 나보다도 실력이 좋은 고수들이 많이 계시지만.. 이제 처음 시작하거나
그리 오랜기간 만져보지 않은 리눅서들에게는 분명 좋은 참고가 될 것이라고 본다.

by pgclks
반응형
Posted by She쥐포s

2008. 10. 15. 21:23 Unix

apache rotatelogs

아파치를 관리하다보면 별도의 설정이 없다면 로그 Size가 겉잡을 수 없이 커지게 된다.
로그 Size가 커지면 아파치 서버에 부하를 주게 된다.

이럴 경우 아파치의 rotatelogs를 이용하여 로그를 관리할 수 있다.

1) 일정간격으로 rotate
    CustomLog "|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/access_log 86400" common
    86400초 단위로 logrotate, 파일이름은 access_log.[timestamp]의 형식으로 저장되며 date의 포맷을
    사용할 수 있다. 즉 access_log.%m%d-%H%M%S 의 형태로 access_log.월일-시분초의 포맷으로
    저장할 수 있다.

2) 일정 Size로 rotate
    CustomLog "|/usr/local/apache/bin/rotatelogs /usr/local/apache/logs/access_log.%m%d-%H%M%S 100M" common
    로그의 Size를 100MB로 지정한다.
반응형
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

2008. 9. 29. 22:50 Unix

folding linux lines

fold : 접다, 우리안에 넣다.
80 컬럼이 넘는 긴 라인을 갖는 파일을 80컬럼으로 만들고자 한다면 다음의 명령어를
사용할 수 있다.

$ fold -w 60 filename

60 컬럼으로 만들고 싶은 경우 위의 명령어를 사용할 수 있다.

자세한 옵션은 man page 참조. 원문에는 Linux 명령어라고 하는데 Solaris에서도
명령어가 작동한다.

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

                             UNIX GURU UNIVERSE
                                UNIX HOT TIP

                       Unix Tip 2377 - July  5, 2006

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

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

FOLDING LINUX LINES

If files have long lines
and you want wrap those lines
at 80 columns. Then you can
use the fold command:

$fold my_file

See man for more options.

반응형
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

블로그 이미지
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

최근에 올라온 글

최근에 달린 댓글

글 보관함