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.
just an e.g. i have two files, in one file (top.txt) 1000 random words are present which i want to use. In second file (All.txt), i have the 10000 words and their meanings in two fields. Now, what i want to do is that i want to use the output of my top.txt file in a way that after greping the words from top file i get the meaning of all those words from my other file All.txt. if i use the command % grep -e "(foul|.....|zeal) top.txt i grep all the words but now how to compare it with the other file. Kindly let me know
As grail said. Vague questions tend to get you vague answers, and one concrete example is worth a thousand lines of explanation. Show us a sample of each file, at least, along with what you want the output to look like.
But in any case, it's possible to use a file as a collection of patterns to search for. As long as the first file has only a single search word per line, you can try this:
Code:
grep -f top.txt All.txt
You can also use other options like -F, to search for fixed strings only, and -i to make them case-insensitive. See the grep man and info pages for more options.
My guess would be that the OP wants to grep for a certain pattern(s) in top.txt first and then use the results to grep them in All.txt. Some sort of cascaded (?) filtering process?
Not that it really changes anything from the above other than bypassing the need for a tempfile. It still requires two grep processes. If we knew more about the actual requirements, perhaps we could even come up with a single-step solution.
Also be aware that P.S. is a bash-only extension.
PS: You need grep -E/egrep for a complex regex like that.
Last edited by David the H.; 10-21-2011 at 07:14 AM.
PS: You need grep -E/egrep for a complex regex like that.
Thanks for the hint. I also noticed that I forgot to close the quote in my previous post. I simply copy+pasted that part from the OP's solution without further examining it. Corrected it now.
Thankyou so much for the solutions but its not working. I will try to elaborate by giving more examples.
file TOP.txt
Foul
Tall
blot
grail
House
System
Galaxy
jar
trophy
laptop
This file contains 10 words
Second file all.txt
system ns01
broad ns02
house ns03
laptop ns04
trophy ns05
ginger ns06
foul ns07
dustbin ns08
mugs ns09
blot ns10
pack ns11
butter ns12
jar ns13
knife ns14
kangroo ns15
galaxy ns16
kind ns17
heart ns18
grail ns19
short ns20
tall ns21
table ns22
chair ns23
blot ns24
onion ns25
foul ns26
this file contains 26words with their codes, now what i want to do is to relate the top file words with the codes in all.txt files omiting the words which are not present in the top file. that is i want the result to look like this
Result
Foul ns26
Tall ns21
blot ns24
grail ns19
House ns03
System ns01
Galaxy ns16
jar ns13
trophy ns05
laptop ns04
two things important firstly words are not arranged alphabetically and secondly they are not case sensitive that is similar words might be in small alphabet in top file where as in capital in all file.
I hape i have given the clear e.g. now
your example still needs a bit more explanation. What happens with multiple matches? Do you want to keep the first match or the last match? Or maybe something else? Your sample output suggests that you want to print only the last match.
Hi,
Its still not working. No i dont have multiple matches and i have single word in all.txt file with a single code no repeatation in the top.txt or all.txt files. Since i am a new user so if possible do guide me about using commands as well. Thankyou so much for your answers.
system ns01
broad ns02
house ns03
laptop ns04
trophy ns05
ginger ns06
foul ns07
dustbin ns08
mugs ns09
blot ns10
pack ns11
butter ns12
jar ns13
knife ns14
kangroo ns15
galaxy ns16
kind ns17
heart ns18
grail ns19
short ns20
tall ns21
table ns22
chair ns23
blot ns24
onion ns25
jacket ns26
I am sorry for it. I have changed the repeated word, if any more repeatation then i wish to keep the first match. Well, i tried the commands but in top.txt file i didnt get any codes after my words with out any errors. I checked all.txt file as well but it remained same, and no new file was created as well.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.