2009-04-23 4 views
2

텍스트를 검색하고 주어진 수의 후행 및 다음 줄을 사용하여 결과를 인쇄하기에 좋은 한 줄자가 나타났습니다. 나는 이것에서 스크립트를 만들려고 노력하고있다. 는 지금까지 내가 무엇을 가지고 :nawk가있는 스크립트가 화면에 출력을 인쇄하지 않습니다.

# ********************************************************* 
# This uses nawk to list lines coming prior to and after 
# the given search string. 
# Format: xgrep <string> <file> <before> <after> 
# ******************************************************** 

STR=$1 
FILE=$2 
BEF=$3 
AFT=$4 
export STR 
export FILE 
export BEF 
export AFT 

nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=$BEF a=$AFT s=$STR $FILE 

문제는 nawk 명령의 출력 내가 명령을 입력하면

>xgrep "bin/sh" * 0 3 
> 

는하지만, 내가 얻을 화면에 나타나지 않는다는 것입니다 적절한 출력 :

>nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' b=0 a=3 s="bin/sh" * 
#!/bin/sh 

export AEGIS_PROJECT=$1 
export EDITOR=cat 
#!/bin/sh 

aegis -cpu $pwd 
aegis -dbu $1 
#!/bin/sh 
cd $HOME/ex_7/src/DTTCom 
NRMan alphabuild clean 
NRMan alphabuild library ex_7 8.19 app TEST DTTCom debug -j10 
#!/bin/sh 
cd $HOME/icx/src/DTTCom 
NRMan alphabuild clean 
NRMan alphabuild library icx_1 1.1 app TEST DTTCom debug -j10 
#!/bin/sh 
# ********************************************************* 
# This uses nawk to list lines coming prior to and after 
# the given search string. 

어떤 이유에서 스크립트를 작동시킬 수 있습니까? 사용

답변

2

보십시오 : 대신

xgrep "bin/sh" '*' 0 3 

.

인수가 스크립트로 전달되기 전에이 증명서와 같이 와일드 카드 용어의 확장은 현재 쉘에서 어떤 일이 일어나고

: 이러한 인수는 다음과 같은 간단한 스크립트로 작동하는 방법을

pax: xgrep include *.c 0 3 
#include <stdio.h> 
gawk: (FILENAME=binmath.c FNR=1) fatal: division by zero attempted in '%' 

pax: xgrep include '*.c' 0 3 
#include <stdio.h> 

// Prototypes - should go in separate header file. 
void compBinNum (char*); 
#include <stdio.h> 
#include <string.h> 
#include <string.h> 

#define UTF8ERR_TOOSHORT -1 
#define UTF8ERR_BADSTART -2 
#include <stdio.h> 
#include <errno.h> 
#include <errno.h> 

int main (void) { 
    FILE *file = fopen ("words.txt", "r"); 

당신이 볼 수 있으며, 출력 :

pax: cat argtest 
    #!/bin/bash 
    echo Number of arguments: $# 
    echo Arguments: "$*" 

pax: argtest * 
    Number of arguments: 32 
    Arguments: Makefile a.exe argtest binmath.c binmath.exe dev 
     file.db infile input.txt inputfile limit10 output.txt 
     p2.sh p2expected p2input1 p2input2 qq qq.c qq.cpp qq.exe 
     qq.exe.stackdump qq.pl qq.py qqin qqq.c qqq.s tmpfile 
     words2.txt xgrep xx.c xx.exe xx.pl 

pax: argtest '*' 
    Number of arguments: 1 
    Arguments: * 

업데이트 : 귀하의 질문에

을 바탕으로 댓글 :

감사합니다. 파일을 작은 따옴표로 묶었을 때 작동했습니다. 사용자가 작은 따옴표를 입력 귀찮게하지 않도록 내가 스크립트 내에서이 작업을 수행 할 수있는 방법이 있나요?

아니요. 스크립트가 스크립트를보기 전에 쉘이 그것을 수행하고 있기 때문입니다. 그러나 이렇게 명령 줄의 끝에 파일 스펙을 이동하는 경우 : 당신이 한 번에,뿐만 아니라 그 이후뿐만 아니라 프로세스 인수 번호 4 만 모든 인수 한 스크립트를 수정할 수

xgrep include 0 3 *.c 

. 그런 다음 쉘로 확장했을 때 중요하지 않습니다.

(한 줄에 둔한/nawk와) 같은

뭔가 :

STR=$1 
BEF=$2 
AFT=$3 
while [[ ! -z "$4" ]] ; do 
    echo ========== "$4" 
    gawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--) 
     print r[(NR-c+1)%b];print;c=a}b{r[NR%b]=$0}' 
     b=$BEF a=$AFT s=$STR "$4" 
     | sed "s/^/$4: /" 
    shift 
done 
echo ========== 

쉘이 확장을 처리 분들께도 루프를 사용하면 이러한 각각의 블록과 파일 이름을 인쇄와 같은 트릭을 수행 할 수 있습니다 출력 (또는 라인) :

pax: xgrep include 0 3 *.c 
    ========== binmath.c 
    binmath.c:  #include <stdio.h> 
    binmath.c: 
    binmath.c:  // Prototypes - should go in separate header file. 
    binmath.c:  void compBinNum (char*); 
    ========== qq.c 
    qq.c:  #include <stdio.h> 
    qq.c:  #include <string.h> 
    qq.c:  #include <string.h> 
    qq.c: 
    qq.c:  #define UTF8ERR_TOOSHORT -1 
    qq.c:  #define UTF8ERR_BADSTART -2 
    ========== qqq.c 
    ========== xx.c 
    xx.c: #include <stdio.h> 
    xx.c: #include <errno.h> 
    xx.c: #include <errno.h> 
    xx.c: 
    xx.c: int main (void) { 
    xx.c:  FILE *file = fopen ("words.txt", "r"); 
    ========== 
+0

감사합니다. 그것은 "파일"을 ''로 감쌀 때 효과적이었습니다. 사용자가 작은 따옴표를 입력 귀찮게하지 않도록 내가 스크립트 내에서이 작업을 수행 할 수있는 방법이 있나요? – Gayan

+0

는 @Gayan을 업데이 트를 참조하십시오. – paxdiablo

+0

나는 "나오지도"변경하는 방법들/^/$ 4 :?/"파일 정보가 이미 사라진 이후로 그것을 할 수 없습니다 나오지 – Gayan