你怎么能让grep从指定的行号开始搜索

时间:2014-03-05 19:11:32

标签: bash

如果我需要开始从文件行1293一直到文件末尾的文件,我该怎么办呢?

如有帮助,请提供更详细的信息:
我试图在我的bashrc中添加一个快速功能,让我快速搜索vim片段文件,查找一个特定的片段,回显片段名称和相关命令到屏幕。所以我没有probs获取片段名称的行号,甚至在以下行号上打印出命令。但是如果代码片段是一个多行命令,那么我需要grep为下一行以片段“^ snippet”开头,然后返回所有行之间,但是我找不到任何细节我怎么去获取grep来开始搜索从特定的行号开始

第二个问题是如何在.bashrc函数中提前退出函数?当我使用'exit'命令时  目前在终端下方的功能中注释退出/关闭,而不仅仅是退出功能。

function vsls() {
if [[ "$2" =~ ^(html|sh|vim)$ ]] ; then 
    sPath="$2".snippets
elif [[ "$2" =~ ^(html|sh|vim).snippets$ ]] ; then 
    sPath="$2"
else
    echo "\nExiting. You did not enter a recognized vim snippets file name."
#   exit 69
fi      
    lnN=$(more $HOME/.vim/snippets/"$sPath"|grep -nm 1 $1|sed -r 's/^([0-9]*):.*$/\1/') ; echo "\$lnN: ${lnN}"
    cntr="$lnN"
    sed -n "$cntr"p "$HOME/.vim/snippets/$sPath"
    ((cntr++))
    sed -n "$cntr"p "$HOME/.vim/snippets/$sPath"
}

@chepner 我不知道为什么(可能缺乏技术诀窍)但没有指定“更多”我收到权限错误:

03:43 ~ $ fLNum=$($HOME/.vim/snippets/"$sPath"|grep -nm 1 tdotti|sed -r 's/^([0-9]*):.*$/\1/') ; echo "\$fLNum: ${fLNum}"
bash: /home/user/.vim/snippets/html.snippets: Permission denied
$fLNum: 
03:43 ~ $ fLNum=$(more $HOME/.vim/snippets/"$sPath"|grep -nm 1 tdotti|sed -r 's/^([0-9]*):.*$/\1/') ; echo "\$fLNum: ${fLNum}"
$fLNum: 1293

现在按照需要工作
因为我觉得使用sed最舒服,所以我坚持使用sed。我之前使用过-n print opt,但不经常这样,所以我完全逃脱了尝试这样的事情。

function vsls() {
if [[ "$2" =~ ^(html|sh|vim)$ ]] ; then 
    sPath="$2".snippets
elif [[ "$2" =~ ^(html|sh|vim).snippets$ ]] ; then 
    sPath="$2"
else
    echo "\nExiting. You did not enter a recognized vim snippets file name."
#   exit 69
fi      

    fLNum=$(more $HOME/.vim/snippets/"$sPath"|grep -nm 1 "snippet $1"|sed -r 's/^([0-9]*):.*$/\1/') ; echo "\$fLNum: ${fLNum}"  #get line number of the snippet name searched, entered as input $1
    ((tLNum1 = fLNum+=1)) ; echo "\$tLNum1: ${tLNum1}"  # tmpLineNum is next line num from which to start next grep search for lineNum of next snippet entry to determine where commands of desired snippet end
    tLNum2=$(sed -n "${tLNum1},$ p" $HOME/.vim/snippets/"$sPath"|grep -nm 1 "snippet"|sed -r 's/^([0-9]*):.*$/\1/') ; echo "\$tLNum2: ${tLNum2}"    #lineNum of next 'snippet entry'
    let sLNum=tLNum2+fLNum sLNum-=1 ; let sLNum-=1 ; echo "\$sLNum: ${sLNum}"   #tmpLineNum2 is not actual line num in file, but rather the number of lines since the start of the second search, that is necessarily somewhere within the file: so if second search begins on line 1294, for all intents and purpose actual line num 1294 is line 1 of the new (second) search; therefore I need to add the tLNum2 with fLNum to determine actual lineNum in the of the next snippet entry 

    echo ""
    sed -n "${fLNum},${sLNum} p" "$HOME/.vim/snippets/$sPath"
    echo ""
}

但很奇怪为什么我需要这样做:

let sLNum=tLNum2+fLNum sLNum-=1 ; let sLNum-=1

获取第二次grep搜索的正确行号。我只是幸运地四处闲逛,b / c我会想:

let sLNum=tLNum2+fLNum sLNum-=1

或: 设sLNum = tLNum2 + fLNum;让sLNum- = 1 应该做的伎俩;也就是说,secondLineNum = tmpLNum2 + firstLineNum然后secondLineNum - 1.但结果永远不会少1但总是等于tLNum + fLNum。很高兴知道为什么它不能按预期工作。

但它的工作。谢谢。

3 个答案:

答案 0 :(得分:2)

awk更适合此

awk '/search_pattern/ && NR > 1292' filename

答案 1 :(得分:2)

或者像这样的sed:

sed -n "1293,$ p" yourfile | grep xyz

或者,如果行号位于名为line的变量中:

sed -n "${line},$ p" yourfile | grep xyz

或者,如果您希望grep在前1292行中找不到任何内容,但在使用grep -n时仍然报告正确的行号,则可以获取(空)保持缓冲区为grep查看第1到1292行

sed "1,1292g" yourfile | grep -n xyz

答案 2 :(得分:1)

tail -n +1293 file | grep ....