Dean
Dean

Reputation: 1153

How to use grep to find data conveniently and quickly

I am asking for general opinions on how to find files quickly.

One typical scenario is that I often need to grep some names from a PeopleNameFile.txt from Dir1. When I'm in a different directory, I am forced to grep the file with a long directory path. It would be nice to just do GREP "Warren Buffett" PeopleNameFile.txt.

BTW, I'm using Cygwin but I welcome any suggestions.

Upvotes: 0

Views: 1053

Answers (4)

Ahmed Masud
Ahmed Masud

Reputation: 22372

You can easily write a simple bash function in your ~/.bashrc:

function grnm() {
     grep "$@" /path/to/peoplenamefile.txt
}

Then later on, at command line you can type:

$ grnm "Warren Buffet"

the nice thing is that you can actually include other grep parameters if you like as in:

$ grnm -i "warren buffet"

(The $ characters represent your shell prompt, not part of the command you type.)

When you edit the .bashrc file FOR THE FIRST TIME you may have to source it in your existing open cygwin windows:

$ source ~/.bashrc

But if you open a new window you should not have to do that.

Good luck, have fun

Upvotes: 1

stnr
stnr

Reputation: 455

Probably the most common way to do these kind of things is to use environment variables

e.g.

PNF='/very/long/path/PeopleNameFile.txt'
grep "Warren Buffett" $PNF

Upvotes: 0

souser
souser

Reputation: 6120

Simplest option would be to setup an alias which would grep for that file using the absolute path. Not sure whether cygwin allows aliases though.

Upvotes: 0

fghj
fghj

Reputation: 9394

You can create script my_grep.sh and add it somewhere to you path, with content like this:

#!/bin/bash
grep $1 path/to/Dir1/PeopleNameFile.txt

than you just type

my_grep.sh "Warren Buffett" 

also you can use alias and bash's function, but this require to edit "~/.bashrc".

Upvotes: 1

Related Questions