code

지정된 문자열로 두 줄 사이의 Bash, grep

codestyles 2021. 1. 11. 08:15
반응형

지정된 문자열로 두 줄 사이의 Bash, grep


예:

a43
test1
abc
cvb
bnm
test2
kfo

test1과 test2 사이의 모든 줄이 필요합니다. 이 경우 일반 grep이 작동하지 않습니다. 제안이 있습니까?


에서 인쇄 test1test2(트리거 라인 포함)

awk '/test1/{f=1} /test2/{f=0;print} f'
awk '/test1/{f=1} f; /test2/{f=0}' 
awk '/test1/,/test2/'

test1
abc
cvb
bnm
test2

test1~ 사이의 데이터를 인쇄 합니다 test2(트리거 라인 제외).

awk '/test1/{f=1;next} /test2/{f=0} f' 
awk '/test2/{f=0} f; /test1/{f=1}' 

abc
cvb
bnm

다음을 사용할 수 있습니다 sed.

sed -n '/test1/,/test2/p' filename

및가 포함 된 줄을 제외하려면 다음 test1test2같이 말하세요.

sed -n '/test1/,/test2/{/test1/b;/test2/b;p}' filename

grep 만 사용할 수있는 경우 :

grep -A100000 test1 file.txt | grep -B100000 test2 > new.txt

grep -A그런 다음 숫자는 일치하는 문자열 뒤의 줄을 grep -B가져오고 일치하는 문자열 앞의 줄을 가져옵니다. 이 경우 숫자 100000은 앞뒤의 모든 줄을 포함 할 수있을만큼 충분히 커야합니다.

test1 및 test2를 포함하지 않으려면 나중에를 제거 grep -v하여 일치하는 줄을 제외한 모든 항목을 인쇄 할 수 있습니다 .

egrep -v "test1|test2" new.txt > newer.txt

또는 모든 것을 한 줄로 :

grep -A100000 test1 file.txt | grep -B100000 test2 | egrep -v "test1|test2" > new.txt 

네, 일반 grep은이 작업을 수행하지 않습니다. 그러나 -P매개 변수가있는 grep 이이 작업을 수행합니다.

$ grep -ozP '(?s)test1\n\K.*?(?=\ntest2)' file
abc
cvb
bnm

\K이전에 일치 된 문자를 최종 인쇄에서 버리고 긍정적 인 예견 (?=\ntest2)은 일치 뒤에 \n개행 문자와 test2문자열 이 와야한다고 주장 합니다.


다음 스크립트는이 프로세스를 마무리합니다. 이 유사한 StackOverflow 게시물에 대한 자세한 내용

get_text.sh

function show_help()
{
  HELP=$(doMain $0 HELP)
  echo "$HELP"
  exit;
}

function doMain()
{
  if [ "$1" == "help" ]
  then
    show_help
  fi
  if [ -z "$1" ]
  then
    show_help
  fi
  if [ -z "$2" ]
  then
    show_help
  fi

  FILENAME=$1
  if [ ! -f $FILENAME ]; then
      echo "File not found: $FILENAME"
      exit;
  fi

  if [ -z "$3" ]
  then
    START_TAG=$2_START
    END_TAG=$2_END
  else
    START_TAG=$2
    END_TAG=$3
  fi

  CMD="cat $FILENAME | awk '/$START_TAG/{f=1;next} /$END_TAG/{f=0} f'"
  eval $CMD
}

function help_txt()
{
HELP_START
  get_text.sh: extracts lines in a file between two tags

  usage: FILENAME {TAG_PREFIX|START_TAG} {END_TAG}

  examples:
    get_text.sh 1.txt AA     => extracts lines in file 1.txt between AA_START and AA_END
    get_text.sh 1.txt AA BB  => extracts lines in file 1.txt between AA and BB
HELP_END
}

doMain $*

당신도 이와 같은 것을 할 수 있습니다. test.txt콘텐츠가있는 이 파일 예로 들어 보겠습니다 .

a43
test1
abc
cvb
bnm
test2
kfo

넌 할 수있어

cat test.txt | grep -A10 test1 | grep -B10 test2

where -A<n> is to get you n lines after your match in the file and -B<n> is to give you n lines before the match. You just have to make sure that n > number of expected lines between test1 and test2. Or you can give it large enough to reach EOF.

Result:

test1
abc
cvb
bnm
test2

The answer by PratPor above:

cat test.txt | grep -A10 test1 | grep -B10 test2

is cool.. but if you don't know the file length:

cat test.txt | grep -A1000 test1 | grep -B1000 test2

Not deterministic, but not too bad. Anyone have better (more deterministic)?

ReferenceURL : https://stackoverflow.com/questions/22221277/bash-grep-between-two-lines-with-specified-string

반응형