LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-23-2014, 02:29 AM   #1
gabyz
LQ Newbie
 
Registered: Oct 2006
Posts: 21

Rep: Reputation: 0
Using grep alias with two variables


hi all,
What I'm trying to do is find a pattern #2 in a list of files that contain another pattern #1.

I have the following alias that works just fine:
alias nsgrep='grep -nr $1 --include=*.cc --include=*.c --include=*.h > ../test.txt'

Now, I want to do a subsequent search in the files that have $1 so I use two commands, one to grep for the first pattern in all the files and another to extract the filenames and search within those files the second pattern:
>> nsgrep tokens
>> grep -nr rate $(cat ../test.txt | awk -F":" '{print $1}'| uniq) >../test2.txt
This works just fine and grep finds the string "rate" in the files that it previously found "tokens" in.

HOWEVER!
If I use
alias nsgrep3='grep -nr $1 $(cat ../test.txt | awk -F":" '\''{print $1}'\''| uniq) >../test2.txt'
(note the escape on \' works fine)
I get an error saying that
"grep: rate: No such file or directory"
Using ">> set -x" I can see the cause, but don't know how to fix it.
What happens is that when using the command line directly grep executes:
grep [switches] [patern] [filelist]
but when I use the alias it executes
grep [switches] [filelist] [patern]

Appreciate the help
Thanks
Gaby
 
Old 01-23-2014, 05:48 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Quote:
Originally Posted by gabyz View Post
I have the following alias that works just fine:
Code:
alias nsgrep='grep -nr $1  --include=*.cc --include=*.c --include=*.h > ../test.txt'
I seriously doubt if that alias or the grep command works....

- $1 isn't recognized inside an alias,
- the grep command doesn't provide any starting point to search.

This would be a valid grep command:
Code:
grep -nr search_pattern --include=*.cc --include=*.c --include=*.h .
Mind the last dot! You could use a * instead, but that would exclude the directory you are standing in when executing the grep command.

You can alias that like this:
Code:
alias nsgrep='grep -nr --include=*.cc --include=*.c --include=*.h'
You can now use: nsgrep tokens . to look for tokens in all the files mentioned in the includes.

Quote:
Originally Posted by gabyz
Now, I want to do a subsequent search in the files that have $1 so I use two commands, one to grep for the first pattern in all the files and another to extract the filenames and search within those files the second pattern:
>> nsgrep tokens
>> grep -nr rate $(cat ../test.txt | awk -F":" '{print $1}'| uniq) >../test2.txt
This works just fine and grep finds the string "rate" in the files that it previously found "tokens" in.
Using the above alias as a starting point will make things needlessly complicated (due to the -n switch used).

If you would change the -n switch to the -l switch it can be done.

Here's an example, first the alias:
Code:
$  alias nsgrep='grep -lr --include=*.cc --include=*.c --include=*.h'
$ nsgrep tokens .
./yyy.c
./Tmp/foobar.h
./Tmp/bar.c
./Tmp/foo.cc
And with that alias in place you can do:
Code:
$ grep -l rate $( nsgrep tokens . )
./Tmp/bar.c
 
1 members found this post helpful.
Old 01-23-2014, 06:23 AM   #3
gabyz
LQ Newbie
 
Registered: Oct 2006
Posts: 21

Original Poster
Rep: Reputation: 0
Quote:
I seriously doubt if that alias or the grep command works....
Well it does (try it , but I understand my mistake).

Quote:
- $1 isn't recognized inside an alias,
didn't know that. I guess that's the problem!
Apparently it is not just not recognized but also ignored.
I have similarly additional alias (see below) that work -- guess I need to remove all the $n in the aliases
This is also the reason why the nsgrep2 alias doesn't work! It doesn't replace the pattern with $1 but appends it to the input command after the filename list. So grep matches the first filename string in the rest of the filename list + the requested pattern where it fails.

Thanks
Gaby
 
Old 01-23-2014, 06:26 AM   #4
gabyz
LQ Newbie
 
Registered: Oct 2006
Posts: 21

Original Poster
Rep: Reputation: 0
PS,
I circumvented the problem by turning nsgrep2 into a script with and alias pointing to it.

BR
G.
 
Old 01-23-2014, 06:33 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Quote:
Originally Posted by gabyz View Post
Quote:
Originally Posted by druuna
I seriously doubt if that alias or the grep command works....
Well it does (try it
I have, and it doesn't work (both the $1 issue and not having a target for grep).

Quote:
Apparently it is not just not recognized but also ignored.
Yep, it is ignored.

You could use a function instead of an alias. This would allow the use of $1 ($2, $3 etc):
Code:
function nsgrep() {
  grep -nr "$1" --include=*.cc --include=*.c --include=*.h "$2"
}
If you add the above to your ~/.bashrc file (and log out and in again or parse ~/.bashrc) you can use:
Code:
nsgrep token .
 
Old 01-23-2014, 06:45 AM   #6
gabyz
LQ Newbie
 
Registered: Oct 2006
Posts: 21

Original Poster
Rep: Reputation: 0
Thanks for the suggestion.
I'm using KDE which has ~/.bash_aliases. For cleanness it can go there right?

Quote:
I have, and it doesn't work (both the $1 issue and not having a target for grep).
">> man grep" gives "GNU grep 2.14" at the end. which version do you have?
I guess its default target is current location.
 
Old 01-23-2014, 06:59 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Quote:
Originally Posted by gabyz View Post
I'm using KDE which has ~/.bash_aliases. For cleanness it can go there right?
If you want it to be clean I wouldn't use .bash_aliases (which is for aliases).

You could create a new file specifically for functions (say: .bash_functions) and parse that file from .bashrc:
Code:
# Function definitions.
if [ -f ~/.bash_functions ]; then
    . ~/.bash_functions
fi
Quote:
">> man grep" gives "GNU grep 2.14" at the end. which version do you have?
I guess its default target is current location.
It does indeed depend on the grep version used. I've tried on some different machines and grep versions 2.4 and 2.6 do not support this, versions 2.12 (and your 2.14) are smart enough to use the current directory as target.

I would, however, use the syntax that is shown in the manual page, which would include a target (main reason: being backwards compatible).
 
1 members found this post helpful.
Old 01-23-2014, 07:03 AM   #8
gabyz
LQ Newbie
 
Registered: Oct 2006
Posts: 21

Original Poster
Rep: Reputation: 0
Slight problem with the usage of function instead of alias/script is:
>> which nsgrep
gives nothing. With an alias I can find it with which.
(alias which='alias | ~/Work/.scripts/which --tty-only --read-alias --show-dot --show-tilde'
where the .scripts/which I copied from Fedora)

Do you know how I can see where/how a function is defined similarly to alias?
 
Old 01-23-2014, 07:04 AM   #9
gabyz
LQ Newbie
 
Registered: Oct 2006
Posts: 21

Original Poster
Rep: Reputation: 0
Thanks for the ~/.bash_function suggestion. Will be applied
 
Old 01-23-2014, 07:24 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
Quote:
Originally Posted by gabyz View Post
Do you know how I can see where/how a function is defined similarly to alias?
You can use the type command. Here are some examples from my box:
Code:
$ type cl
cl is aliased to `clear ; ls -la'

$ type ALL2LOWER
ALL2LOWER is a function
ALL2LOWER () 
{ 
    /bin/ls * | awk '{ print "mv \""$0"\" \""tolower($0)"\"" }' | bash
}
 
Old 01-23-2014, 07:33 AM   #11
gabyz
LQ Newbie
 
Registered: Oct 2006
Posts: 21

Original Poster
Rep: Reputation: 0
thanks
 
  


Reply

Tags
alias, grep, variables


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
grep variables in perl realnoname Programming 10 02-28-2009 05:59 AM
Grep the variables - Script rajaniyer123 Solaris / OpenSolaris 3 08-09-2008 04:48 PM
Can you use grep / awk on variables instead of files? zest n zeal Linux - Newbie 2 02-11-2008 12:37 PM
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 12:22 PM


All times are GMT -5. The time now is 08:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration