'sed'에 해당되는 글 2건

  1. 2009.11.05 13. 파일 비교 스크립트 4 2
  2. 2009.03.31 7. sed 란? 2
2009. 11. 5. 21:31

13. 파일 비교 스크립트 4

원본 파일(a.txt)에서  특정한 파일의 숫자들(b.txt) 을 뺀 나머지 라인들을 구하는 것

우선 원본 파일 변경되므로 원본파일은 다른 이름으로 변경시켜 놓는다.
#!/bin/bash
dir=$PWD
count=0
cp $dir/a.txt $dir/a.txt_org

for mdn in `cat $dir/b.txt | awk -F, '{print $1}'`
do
      sed -e "/"$mdn"/d" $dir/a.txt > $dir/imsy.txt
      cat $dir/imsy.txt > $dir/a.txt
      count=`expr $count + 1`
done
echo "count is =" $count

rm $dir/imsy.txt
작업할 디렉토리 지정
반복문이 몇번 실행되었는지 체크하기 위한 count 변수 지정
원본 a.txt 파일을 a.txt_org 으로 백업한다.

(2) for mdn in `cat /home/bjh/homepcssn/test/b.txt | awk -F, '{print $1}'`
for 반복문을 사용
mdn 이란 변수에 b.txt 파일에서 숫자가 있는 필드만 awk 로 골라내어서 for 문을 사용해 차례로 입력하도록 한다
( 나중에는 cat 부분만 변경해서 사용 / 숫자만 있는 파일이면 cat 만 써도 됨 )
awk 에서 -F 옵션은 구분자를 지정할때 사용 ( 지정하지 않으면 빈칸으로 필드 구분 )
즉 쉼표(,) 로 구분했을때 첫번째 필드가 숫자가 되기 때문에 사용
여기서 $mdn 이란 변수에는 숫자(전화번호) 만 들어간다

(3) sed -e "/"$mdn"/d" $dir/aa.txt > $dir/imsy.txt
변수 mdn이 포함된 라인만 지운 나머지 모든 라인을 임시파일 imsy.txt 파일에 저장한다.

(4) cat imsy.txt > a.txt
임시파일을 원본 a.txt 에 덮어쓴다
이때 첫번재 mdn 값을 제외한 모든 라인이 a.txt 에 덮어쓴다.

(5) count=`expr $count + 1`
반복문이 1번 반복될때마다 count 변수는 하나씩 증가한다.

(6) done
mdn 변수의 개수만큼 돌아가고 나서 반복문 종료

(7) echo "count is =" $count
반복문이 몇번 실행되었는지 확인
즉 mdn 변수가 몇번 변했는지도 알 수 있다.
즉 b.txt 에서 숫자가 몇라인이 들어있는지 알 수 있다

(8) rm $dir/imsy.txt
임시파일 삭제




최종 파일 비교 스크립트 -> http://darkrang.tistory.com/219


2009. 3. 31. 17:01

7. sed 란?


첫번째 라인 출력

sed -n '1p' filename

마지막 라인 출력

sed -n '$p' filename

두번째 라인 출력

sed -n '2p' filename

첫번째부터 세번째 라인까지 출력 ( 1번 라인 부터 3번 라인까지 포함 )

sed -n '1,3p' filename

첫번째라인 부터 세번째 라인까지 삭제하고 출력
sed '1,3d' filename

특수한 문자열(예를들어 root)을 검색해서 그 라인만 출력

sed -n '/root/p' filename

love가 나오는 행과 peace가 나오는 행 사이의 모든 행들이 출력된다. love가 peace 다음에 나오면 love가 나오는 행부터 마지막 행까지 출력된다.

sed -n ‘/love/,/peace/p’ datafile


파일 안의 빈줄들만 모조리 삭제하고자 할때 .

sed '/^$d' 파일명 > 새파일명

특정 단어를 찾아서 변환하고자 할때
sed 's/TEST/test/g' > 새파일명

특정 단어를 찾아서 지우고자 할때
sed 's/TEST//g' > 새파일명



sed 에 관한 도움말

# sed --help
Usage: sed [OPTION]... {script-only-if-no-other-script} [input-file]...

  -n, --quiet, --silent
                 suppress automatic printing of pattern space
  -e script, --expression=script
                 add the script to the commands to be executed
  -f script-file, --file=script-file
                 add the contents of script-file to the commands to be executed
  -i[SUFFIX], --in-place[=SUFFIX]
                 edit files in place (makes backup if extension supplied)
  -c, --copy
                 use copy instead of rename when shuffling files in -i mode
                 (avoids change of input file ownership)
  -l N, --line-length=N
                 specify the desired line-wrap length for the `l' command
  --posix
                 disable all GNU extensions.
  -r, --regexp-extended
                 use extended regular expressions in the script.
  -s, --separate
                 consider files as separate rather than as a single continuous
                 long stream.
  -u, --unbuffered
                 load minimal amounts of data from the input files and flush
                 the output buffers more often
      --help     display this help and exit
      --version  output version information and exit