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 have a master file (master.txt), the content is as below
#vi master.txt
aaa
bbb
ccc
ddd
eee
fff
ggg
I also have some file (eg. file1.txt , file2.txt , file3.txt ... ) , these files may be have the characters as above , for example
#vi file1.txt
123456 aaa 54
#vi file2.txt
1456456 bbb 452
#vi file3.txt
667766 ccc 123
like above , file1.txt have aaa , file2.txt have bbb , file3.txt have ccc , I would like to sort it out , to output a file (matching.txt) that match the file name with the character as below .
I think the point was you could just use grep, ie no extra scripting required. Generally a script is devised to alleviate a repetitive task. As this would be a single line entry
you probably wouldn't waste time to then place it in a script (your choice of course).
If you are not sure where to start, have a look at the link below to help kick you off:
Writing a script does not have to be complex and difficult. It starts with finding the commands that will do the task---and then simply combining them in one file. Suppose you want to search in a file for the word "fred":
Code:
grep "fred" filename
Now you want to search in all the files in a directory:
Code:
grep "fred" *
This will try to search directory names also, so you can provide for more control using:
Code:
for filename in *; do grep fred $filename; done
This does the same thing, but now you can add more logic---eg. a test for a directory
When you have a set of commands that does what you want, then add "#!/bin/bash" at the beginning, make the file executable, and now you have a script....
Code:
#!/bin/bash
for file in *; do
if [ -f $file ]; then
grep abc $file
fi
done
may be I simpifiy my requirement , if I just would like to list out the line ( in the master file ) that do not appear in any files , eg. if aaa is not exist in any files , then output aaa to the output file ; if bbb is exist in file1.txt (any files would be ok , assume exist in file1.txt now ) , then do not output bbb to the output file ; if ccc is not exist in any files , then output ccc to the output file ..
I just would like to output a file to show all lines that do NOT appear in any file .
the output file as below
#vi output.txt
aaa
ccc
can advise what can i do ?
I just would like to output a file to show all lines that do NOT appear in any file .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.