2009-04-23 92 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 

代替。

如图这份成绩单通配符项的膨胀是在当前shell发生的参数传递给您的脚本之前:

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: * 

更新:根据在你的问题

意见:

谢谢。它在我用单引号包装文件时起作用。有没有一种方法可以在脚本中做到这一点,以便用户不必打扰单引号?

不,这是因为在您的脚本看到它之前shell正在执行它。但是,如果你移动文件规范到命令行的末尾这样的:

xgrep include 0 3 *.c 

,你可以修改你的脚本后,并非只是工艺参数号4,但说法为好,一次一个。然后,当它们被shell扩展后,它就不会有问题了。

喜欢的东西(与单行GAWK/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 2009-04-23 07:18:50