LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 06-27-2012, 06:34 AM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 30
script to search character


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 .

#vi matching.txt
aaa file1.txt
bbb file2.txt
ccc file3.txt
ddd "
eee "
fff "
ggg "

can advise what can i write the script ?

Thanks in advance.
 
Old 06-27-2012, 06:40 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
look at the man page of grep, and take care about the flags, especially -f and -l
 
Old 06-27-2012, 06:58 AM   #3
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
thx reply ,

I know grep is help to do it , but I am not familiar with script writing , can advise the script ?


Thanks.
 
Old 06-27-2012, 08:18 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,246

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
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:

http://tldp.org/LDP/abs/html/
 
Old 06-27-2012, 09:05 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
ust;

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
 
Old 06-27-2012, 07:04 PM   #6
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Original Poster
Rep: Reputation: 30
thx reply ,

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 .
 
Old 06-27-2012, 10:02 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
At least have a try and then show us where you got stuck; you've been here a long time.
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
search last character of many in a line rperezalejo Programming 2 11-05-2011 09:01 PM
C++ character search in text file lindaonline15 Programming 1 06-23-2011 08:07 AM
how to search and replace character from middle of line vishal_titre Linux - General 7 09-30-2008 03:46 AM
java, search character pattern in string howto xemous Programming 2 08-09-2006 08:53 AM
How to search for a character? mimf Linux - Newbie 1 12-03-2003 07:20 AM


All times are GMT -5. The time now is 10:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration