LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 09-14-2005, 10:15 AM   #1
DaFrEQ
Member
 
Registered: Mar 2002
Location: Earth... for now
Distribution: SuSE9.2 AMD64; LFS; GentooAMD64; Ubuntu10.04; RHEL 5.5; Solaris10(SPARC)
Posts: 418

Rep: Reputation: 30
grep ?? can grep us variables?


Hi all,

I was going thru the man page of grep, and didn't see anything regarding variables, so I need to ask, can you use a variable with grep?

i.e. want to grep a file for all lines starting with H and V, but each time the file name will change. Dont want to have to keep re-typing the command line.

ex.
Code:
grep ^H file1.file && grep ^V file1.file >> outputfile
grep ^H file2.file && grep ^V file2.file >> outputfile
...and so on. Is there a way to have a variable so that all i have to do is throw this grep command into a txt file, slap a chmod +x on it so it will run, and then just simply type the script name with new filename to search?

Hope that all made sense.
Tnx.
L8rz
 
Old 09-14-2005, 10:19 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Why a variable, can't you do something like this:

grep "^[HV]" *.file > outfile

or

grep "^[VH]" file*.file > outfile

Just like you use with ls, the files grep looks in can have wildcards.

Hope this helps.
 
Old 09-14-2005, 10:45 AM   #3
DaFrEQ
Member
 
Registered: Mar 2002
Location: Earth... for now
Distribution: SuSE9.2 AMD64; LFS; GentooAMD64; Ubuntu10.04; RHEL 5.5; Solaris10(SPARC)
Posts: 418

Original Poster
Rep: Reputation: 30
Yes, I could use a wildcard, but, looking back at what I said, I didn't want to type the entire line out each time.
That's why I wanted to just put it in a txt file, make it executable, so I only have to type the executable filename and the filename to search for... which is alot shorter because actually its just the executable filename and clicking on the filename to search for rather than typing it.
Saves alot of time.

Also, I guess I didnt make this clear, the filename to search for will not always be named similar to the previous files. So wildcards will only work sometimes.

Last edited by DaFrEQ; 09-14-2005 at 10:48 AM.
 
Old 09-14-2005, 11:13 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Code:
#!/bin/bash
if [[ -n $1 ]]
then
  grep -H "^[HV]" $@ >> /tmp/outputfile
else
  echo "no inputfile given!"
  exit 1
fi
exit 0
Safe as hvgrep, run as:

$ hvgrep <filename>
<filename> can be multiple filenames and/or wildcards (ie: hvgrep in in2 or hvgrep in*).

You probably want to change the location/name of the outputfile (atm: /tmp/outputfile).

Hope this is what you wanted
 
Old 09-14-2005, 12:22 PM   #5
DaFrEQ
Member
 
Registered: Mar 2002
Location: Earth... for now
Distribution: SuSE9.2 AMD64; LFS; GentooAMD64; Ubuntu10.04; RHEL 5.5; Solaris10(SPARC)
Posts: 418

Original Poster
Rep: Reputation: 30
Quote:
Originally posted by druuna
Hi,

Code:
#!/bin/bash
if [[ -n $1 ]]
then
  grep -H "^[HV]" $@ >> /tmp/outputfile
else
  echo "no inputfile given!"
  exit 1
fi
exit 0
Safe as hvgrep, run as:

$ hvgrep <filename>
<filename> can be multiple filenames and/or wildcards (ie: hvgrep in in2 or hvgrep in*).

You probably want to change the location/name of the outputfile (atm: /tmp/outputfile).

Hope this is what you wanted
Cheers mate. That's pretty much what I was going for.
Tnx alot.

L8rz
 
  


Reply



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
grep help? slinky2004 Linux - Newbie 5 10-16-2004 05:16 PM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 01:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 03:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM
What is grep? Thaidog Linux - Newbie 8 07-24-2003 02:04 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:10 PM.

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
Open Source Consulting | Domain Registration