Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
i am trying to write a script (which I havent done since college) that will grep each line from a file named Backlist which contains the titles of about 850 ebooks against another file named HQNlist which contains about 5500 lines which are book titles plus their corresponding ISBN codes. I want to be able to find those 850 matching titles and write them to a new file because I need to make a list of those ISBNs.
I wrote this script but it just writes the entire HQNlist many times to the new file:
Code:
#!/bin/bash
for line in `cat Backlist`
do
grep -i $line HQNlist >> results.omfg
done
cat Backlist | while read line; do
grep '$line' HQNlist >> results.omfg
done
That should basically do it; if the stuff in the first file (which ends up in the $line variable) contains anything other than "normal" characters like letters & numbers, we may need to enhance the grep a little, but this is the basic idea.
I put your code into [CODE] tags to make it easier to read.
Sasha suggested the same approach that I was going to. To see why the original approach was not working, you could try inserting an echo statement to see the value of "line" each time thru the loop.
I tried Sasha's approach. The results were just a blank file. Some of the lines have punctuation.
Is there a way to ignore special characters or should I try to remove all the punctuation from these files?
How about show us a snippet of what's inside each file; that'd be the best way we can ensure that the grepping will work when it encounters odd characters.
echo "$(cat Backlist)" | while read line; do
grep -e "${line}" HQNlist >> results.omfg
done
Using the data you gave, I've adjusted it and find that the above now works for me. I suspect a problem with newlines and/or spaces, particular spaces, were causing troubles before.
See if that works for you?
SORRY - I initially left out the quotes around the `echo $(..)` which should be there -- please note that and correct..
Last edited by GrapefruiTgirl; 07-23-2010 at 09:21 AM.
Using the data you gave, I've adjusted it and find that the above now works for me. I suspect a problem with newlines and/or spaces, particular spaces, were causing troubles before.
Sasha, your previous code (post #2) could work, but you inadvertently embedded $line in single quotes, preventing shell expansion. ghostdog's solution works for me. Cheers!
ghost*'s -f idea does not work for me, and neither does my initial suggestion which as you suggest should work with double-quotes (I changed the quotes and still no go).
echo "$(cat Backlist)" | while read line; do
grep -e "${line}" HQNlist >> results.omfg
done
Using the data you gave, I've adjusted it and find that the above now works for me. I suspect a problem with newlines and/or spaces, particular spaces, were causing troubles before.
See if that works for you?
SORRY - I initially left out the quotes around the `echo $(..)` which should be there -- please note that and correct..
this returned 5795 lines which is kind of strange being its a few hundred more than contained in the HQNlist
I wish grep -f worked, it was the first thing i tried last night
$ Backlist
You've Got Male
The Darkest Facts: A Lords of the Underworld Companion
The Darkest Fire
The Amazon's Curse
$ cat HQNlist
Back on Blossom Street 9781426814686
The Amazon's Curse 9781426816753
Last Known Victim 9781426814693
The Healer 9781426814709
Rogue 9781426814716
The Darkest Facts: A Lords of the Underworld Companion 9781426814728
$ grep -f Backlist HQNlist
The Amazon's Curse 9781426816753
The Darkest Facts: A Lords of the Underworld Companion 9781426814728
and
Code:
$ cat test.sh
#!/bin/bash
cat Backlist | while read line; do
grep "$line" HQNlist
done
$ ./test.sh
The Darkest Facts: A Lords of the Underworld Companion 9781426814728
The Amazon's Curse 9781426816753
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.