The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
bash and ksh: variable lost in loop in bash? estienne Shell Programming and Scripting 2 08-25-2008 03:09 PM
passing variable from bash to perl from bash script arsidh Shell Programming and Scripting 10 06-04-2008 01:25 PM
question on sed grep awk from variable 3Gmobile Shell Programming and Scripting 9 08-11-2006 01:15 AM
Simple grep - Not sure it makes sense! GNMIKE UNIX for Dummies Questions & Answers 5 10-22-2005 03:51 AM
Linux Benchmarks Makes No Sense philip_38 Linux Benchmarks 0 07-22-2005 11:29 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-22-2009
Quan Quan is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 3
BASH: How do I grep on a variable? (or simmilar question that makes sense)

Hi, I've been running code which very frequently calls books.csv. e.g:


Code:
grep -i horror books.csv > temp

Except, I'm trying to move away from using temporary files or frequently calling books.csv to improve efficiency. So I tried something like


Code:
bookfile=$(cat books.csv)
grep -i horror $bookfile

Needless to say, it explodes (giving me about 40 lines of: "grep [data here] no such file or directory"), that's before I even try and save my grep output as a variable. Don't suppose anyone knows what path I need to be taking? Thanks in advance
  #2 (permalink)  
Old 04-22-2009
jamester76 jamester76 is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 3
Set your variable as follows:
bookfile=`cat books.csv`

Then your grep should get every line that has the word "horror" in it. If you want specific fields from each line, you'll need to do something similar to:

bookname=`echo $bookfile | awk -F"," '{print $1}'`

This assumes the fields are separated by commas (true csv format) and that the first field is the bookname.
  #3 (permalink)  
Old 04-22-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,374

Code:
printf "%s\n" "$bookfile" | grep -i horror

  #4 (permalink)  
Old 04-23-2009
Quan Quan is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 3
Ahh thanks, but I'm pretty sure I'm supposed to be doing this without the awk function.
  #5 (permalink)  
Old 04-23-2009
Quan Quan is offline
Registered User
  
 

Join Date: Apr 2009
Posts: 3
Quote:
Originally Posted by cfajohnson View Post

Code:
printf "%s\n" "$bookfile" | grep -i horror


Thank you loads!!
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:53 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0