파일에서 라인 지우는 스크립트

kenshin.1223의 이미지

#!/bin/sh
 
declare PATTERN
declare -a FILES
declare -i f_count=0
declare -i grep=0
declare -i show_reduced=0
 
[[ ${#PIPESTATUS[@]} -gt 1 ]] && pipestatus=1 || pipestatus=0
 
# Prevent aliases from causing portage to act inappropriately.
# Make sure it's before everything so we don't mess aliases that follow.
unalias -a
 
# Setup the colors so our messages all look pretty - from baselayout(functions.sh)
GOOD=$'\e[32;01m'
WARN=$'\e[33;01m'
BAD=$'\e[31;01m'
HILITE=$'\e[36;01m'
BRACKET=$'\e[34;01m'
NORMAL=$'\e[0m'
 
ewran() {
  if [[ ${RC_QUIET_STDOUT} == "yes" ]] ; then
    echo " $*"
  else
    echo -e " ${WARN}*${NORMAL} ${RC_INDENTATION}$*"
  fi
 
  return 0
}
 
eerror() {
  if [[ ${RC_QUIET_STDOUT} == "yes" ]] ; then
    echo " $*" >/dev/stderr
  else
    echo -e " ${BAD}*${NORMAL} ${RC_INDENTATION}$*"
  fi
}
 
declare -i total_bytes_reduced=0
__reduced_size() {
  [[ $show_reduced -eq 1 ]] || return 1
  local file_before="$1"
  local file_after="$2"
  local bytes_before=0
  local bytes_after=0
  local reduced=-1
 
  bytes_before=`stat --format '%s' "$file_before"`
  bytes_after=`stat --format '%s' "$file_after"`
  reduced=$(( $bytes_before - $bytes_after ))
  total_bytes_reduced=$(( $total_bytes_reduced + $reduced ))
 
  echo "the size of '${file_before}' that is reduced in byte from ${bytes_before} to ${bytes_after}"
 
  return 0
}
 
add_file() {
  local f="$1"
  if [ ! -e "$f" ]; then
    ewran "${f} was not found"
    return 1
  fi
  let f_count++
  FILES[$f_count-1]=$f
  return 0
}
 
declare chk_buff
chk_aleady_exists() {
  local fn="$1"
  if [ $f_count -eq 0 ]; then
    add_file $fn
    return 0
  elif [ "$chk_buff" = "$fn" ]; then
    return 1
  elif [ -z $chk_buff ]; then
    chk_buff=$fn
  fi
  for f in ${FILES[@]}; do
    if [ "$fn" = "$f" ]; then
      return 1
    fi
  done
 
  add_file $fn
  chk_buff=
}
 
filename_from_pipeline() {
  local f
  [[ $pipestatus -eq 0 ]] && return 0
  while read f && [[ -n $f ]]; do
    if [ $grep -eq 1 ]; then
      f=`echo "$f" | cut -f1 -d':'`
    fi
    chk_aleady_exists "$f"
  done
}
 
process_arguments() {
  while getopts gsf: opt; do
    case $opt in
      g) grep=1 ;;
      s) show_reduced=1 ;;
      f) chk_aleady_exists $OPTARG ;;
    esac
  done
 
  filename_from_pipeline
 
  if [ $OPTIND -gt $BASH_ARGC ]; then
    eerror "the pattern is not given"
    exit 1
  elif [ -z ${BASH_ARGV[`let $BASH_ARGC-$OPTIND`]} ]; then
    eerror "the pattern is not given"
    exit 1
  else
    PATTERN=${BASH_ARGV[0]}
  fi
}
 
main() {
  process_arguments $@
  if [ $f_count -eq 0 ]; then
    ewran "the target file is not given"
    exit 0
  fi
  for f in ${FILES[@]}; do
    ff=`echo $f | md5sum | cut -f1 -d' '`
    sed -e "/${PATTERN}/d" $f > "/dev/shm/${ff}"
    __reduced_size $f "/dev/shm/${ff}"
    mv -f "/dev/shm/${ff}" $f
  done
}
 
if [ $# -lt 1 ]; then
  exit 1
fi
 
main $@

텍스트 파일에서 특정 내용이 들어간 줄을 지워주는 스크립트입니다.

사용방법
파일이름 - delline.sh
옵션 -
-f 파일이름
-s 줄어든 바이트수 표시
-g : find -type f | xargs grep '????' | delline.sh '????'
위와 비슷한 형식으로 사용할 때 선택하는 옵션입니다.

기본적인 사용방법은 아래와 같습니다.
delline.sh -f 파일 [ -f 파일 ..] '내용'

오늘 재미삼아 연습삼아 만들어 본 스크립트 입니다. -__-;
심심해서 올려 봤습니다. =3==3;

ㅡ,.ㅡ;;의 이미지

음....

특정 내용들어간거 지우는거라구요?

그런거라면..

cat 파일명 | grep -v "어쩌고..저쩌고.." > 새파일명

이정도로도 되지않나요?


----------------------------------------------------------------------------