BASH: How do I grep on a variable?

  • Thread starter Thread starter Zurtex
  • Start date Start date
  • Tags Tags
    Variable
AI Thread Summary
The discussion revolves around optimizing the process of searching for terms in a CSV file, specifically "books.csv," without relying on temporary files. The initial attempt to store the contents of the CSV in a variable and then grep from it resulted in errors, as the command was misinterpreted to search for files named after the content of the CSV. The correct approach suggested includes directly referencing the file in the grep command, such as using "bookfile=books.csv" or using a wildcard to search through all CSV files. An alternative solution provided involves using printf to pipe the contents of the CSV file into grep, which successfully filters the desired output without creating temporary files.
Zurtex
Science Advisor
Homework Helper
Messages
1,118
Reaction score
1
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 director), 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?
 
Technology news on Phys.org
bookfile=$(cat books.csv)
would expand to the contents of the file, which when executed with
grep -i horror $bookfile
will try to grep from files represented by the content of the csv file, in which case most probably the files don't exist.

If you want to use a parameter to represent the csv file, you could try:
Code:
bookfile=books.csv
grep -i horror $bookfile
or better still, if you want to grep from all .csv files (if you have many of them)
Code:
bookfiles=`ls *.csv`
grep -i horror $bookfiles
 
Oh that's cool, I'll try it out :smile:

I also got another solution:

Code:
bookfile=$(cat books.csv)
printf "%s\n" "$bookfile" | grep -i horror
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...
Back
Top