2

我一直在使用 tail、head 和 grep 的组合来打印出与我相关的代码。它可以帮助我找到功能并直入主题。我每天这样做大约 30 到 40 次,所以我想我会在这方面寻求一些帮助,因为我不擅长 sh 脚本。

我最终想要的命令格式类似于 giveme "searchstring" -nn << 其中 nn 是要给 head 的数字。错误..继续

以下是我目前正在做的事情

grep -rl searchitem ./
cat ./filename | grep -n searchitem
tail -n +line ./filename | head -numberoflinestodisplay 


例子:

jezzy@forum:/var/www/sgs# grep -rl insert_into_selectbox ./
./bin/ext/js/functions_edit.js
./src/ext/js/functions_edit.js

jezzy@forum:/var/www/sgs# cat ./bin/ext/js/functions_edit.js | grep -n insert_into_selectbox
195:    for (key in data) insert_into_selectbox(selectbox, data[key], key, 0);
232:        insert_into_selectbox(selectbox, data[key], key,1);
273:    for (key in data) if (key!="_overload_") insert_into_selectbox(selectbox, data[key], key, 0);
289:function insert_into_selectbox(selectbox,text,value,selected) {
323:      insert_into_selectbox(id,right.value,right.value,1);
334:      insert_into_selectbox(id,options_right[i].text,options_right[i].value,1);


然后我选择一个适合我的 grep。我不介意 bash 脚本是否对所有出现的 grep 都执行此操作。它会教我更具体地进行搜索。


jezzy@forum:/var/www/sgs# tail -n +289 ./bin/ext/js/functions_edit.js | head -30
function insert_into_selectbox(selectbox,text,value,selected) {
  if (text=="" || value=="") return;
  var obj = getObj(selectbox);
  var index = _selectbox_find(obj, value);
  if (index == -1) {
        index = obj.options.length;
        obj.options[index] = new Option(text,value);
  }
  if (!obj.multiple && selected) obj.options[index].selected = true;
}


如果我知道如何获取找到的项目的所有实例,我想我可以弄清楚。

谢谢,谢谢

4

2 回答 2

2

试试这个,如果我理解正确的话,它应该是你想要的:

# Usage: giveme searchstring [numlines]

find . -type f -exec awk -v n=${2:-10} '
/'"$1"'/ {
  printf("==== %s ====\n",FILENAME);
  for(i=0;i<n;i++)
  {
    printf("%4d: %s\n",FNR,$0);
    getline
  }
}' {} +
于 2013-09-23T12:46:38.730 回答
0

你可以试试这个脚本:

#!/bin/bash

shopt -s extglob

SEARCHDIR_DEFAULT="./"

read -ep "Enter search directory [$SEARCHDIR_DEFAULT]: " SEARCHDIR
[[ $SEARCHDIR =~ ^[[:space:]]*$ ]] && SEARCHDIR=$SEARCHDIR_DEFAULT

for (( ;; )); do
    read -p "Enter search item: " SEARCHITEM
    read -p "Enter number of lines to display: " NUMLINES

    readarray -t MATCHEDFILES < <(exec grep -rl "$SEARCHITEM" "$SEARCHDIR")

    echo "Please select a file to search into. Press C to cancel, or X to exit."

    select FILE in "${MATCHEDFILES[@]}"; do
        if [[ -n $FILE ]]; then
            readarray -t MATCHEDLINES < <(exec grep -n "$SEARCHITEM" "$FILE")
            echo "Please select starting line."
            select LINE in "${MATCHEDLINES[@]}"; do
                if [[ -n $LINE ]]; then
                    LINENUM=${LINE%%:*}
                    tail -n "+$LINENUM" "$FILE" | head -n "$NUMLINES"
                elif [[ $REPLY == [cC] ]]; then
                    break
                elif [[ $REPLY == [xX] ]]; then
                    break 3
                fi
            done
        elif [[ $REPLY == [cC] ]]; then
            break
        elif [[ $REPLY == [xX] ]]; then
            break 2
        fi
    done
done

新版本:

#!/bin/bash

shopt -s extglob

SEARCHDIR_DEFAULT="./"
NUMLINES_DEFAULT=10

for (( ;; )); do
    until
        read -ep "Enter search directory [$SEARCHDIR_DEFAULT]: " SEARCHDIR
        [[ $SEARCHDIR =~ ^[[:space:]]*$ ]] && SEARCHDIR=$SEARCHDIR_DEFAULT
        [[ -d $SEARCHDIR ]]
    do
        echo "Please enter a valid directory."
    done
    SEARCHDIR_DEFAULT=$SEARCHDIR

    until
        read -p "Enter search item: " SEARCHITEM
        [[ ! $SEARCHITEM =~ ^[[:blank:]]*$ ]]
    do
        echo "Please enter a valid search item."
    done

    until
        read -p "Enter number of lines to display [$NUMLINES_DEFAULT]: " NUMLINES
        [[ $NUMLINES =~ ^[[:space:]]*$ ]] && NUMLINES=$NUMLINES_DEFAULT
        [[ $NUMLINES =~ ^[[:digit:]]+$ && NUMLINES -gt 0 ]]
    do
        echo "Please enter a valid number of lines."
    done
    NUMLINES_DEFAULT=$NUMLINES

    readarray -t MATCHEDFILES < <(exec grep -rle "$SEARCHITEM" "$SEARCHDIR")

    P1="Please select a file to search into. [ENTER = show list again, C = cancel, X = exit]: "
    P2="Please select starting line. [ENTER = show list again, C = cancel, X = exit]: "

    PS3=$P1

    select FILE in "${MATCHEDFILES[@]}"; do
        if [[ -n $FILE ]]; then
            readarray -t MATCHEDLINES < <(exec grep -ne "$SEARCHITEM" "$FILE")
            if [[ ${#MATCHEDLINES[@]} -eq 0 || ! ${MATCHEDLINES[0]} =~ ^[[:digit:]]+: ]]; then
                echo "No matching lines were extracted. Perhaps file is a binary."
            elif [[ ${#MATCHEDLINES[@]} -eq 1 ]]; then
                LINENUM=${MATCHEDLINES[0]%%:*}
                echo "----------------------------------------"
                tail -n "+$LINENUM" "$FILE" | head -n "$NUMLINES"
                echo "----------------------------------------"
            else
                PS3=$P2

                select LINE in "${MATCHEDLINES[@]}"; do
                    if [[ -n $LINE ]]; then
                        LINENUM=${LINE%%:*}
                        echo "----------------------------------------"
                        tail -n "+$LINENUM" "$FILE" | head -n "$NUMLINES"
                        echo "----------------------------------------"
                    elif [[ $REPLY == [cC] ]]; then
                        break
                    elif [[ $REPLY == [xX] ]]; then
                        break 3
                    fi
                done

                PS3=$P1
            fi
        elif [[ $REPLY == [cC] ]]; then
            break
        elif [[ $REPLY == [xX] ]]; then
            break 2
        fi
    done
done
于 2013-09-23T11:35:06.200 回答