Skip to main content
Post Undeleted by Timothy Martin
Revised answer after OP clarification in Comment.
Source Link
Timothy Martin
  • 8.9k
  • 1
  • 37
  • 40

Your command is almost thereEDIT: After reading your comment, I realize I didn't understand the question initially. Adding Here is a hyphen tosimple script which takes your command seemssearch term as an argument and prompts for the path to dosearch. If the trickresult is found in only one file, it opens the file for editing at the location of the search result.

#!/bin/bash

SRCHTRM="$1"

read -p "Where to search: " SRCHPATH

FILEFOUND=$(grep foo"$SRCHTRM" file_names$SRCHPATH)
FILEFOUNDCNT=$(grep "$SRCHTRM" $SRCHPATH | vimwc -l)

FILEAWK=$(echo "$FILEFOUND" | awk -F":" '{ print $1 }')

if [ "$FILEFOUNDCNT" -eq "1" ];then
    vi +/"$SRCHTRM" "$FILEAWK"
else
    echo "$1 was found in more than one file"
fi

Your command is almost there. Adding a hyphen to your command seems to do the trick.

grep foo file_names | vim -

EDIT: After reading your comment, I realize I didn't understand the question initially. Here is a simple script which takes your search term as an argument and prompts for the path to search. If the result is found in only one file, it opens the file for editing at the location of the search result.

#!/bin/bash

SRCHTRM="$1"

read -p "Where to search: " SRCHPATH

FILEFOUND=$(grep "$SRCHTRM" $SRCHPATH)
FILEFOUNDCNT=$(grep "$SRCHTRM" $SRCHPATH | wc -l)

FILEAWK=$(echo "$FILEFOUND" | awk -F":" '{ print $1 }')

if [ "$FILEFOUNDCNT" -eq "1" ];then
    vi +/"$SRCHTRM" "$FILEAWK"
else
    echo "$1 was found in more than one file"
fi
Post Deleted by Timothy Martin
Source Link
Timothy Martin
  • 8.9k
  • 1
  • 37
  • 40

Your command is almost there. Adding a hyphen to your command seems to do the trick.

grep foo file_names | vim -