LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 05:34 AM   #1
ust
Senior Member
 
Registered: Mar 2003
Location: fasdf
Distribution: Debian / Suse /RHEL
Posts: 1,130

Rep: Reputation: 31
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, 05:40 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 20,244

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

Original Poster
Rep: Reputation: 31
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, 07:18 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,976

Rep: Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181Reputation: 3181
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, 08:05 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

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

Original Poster
Rep: Reputation: 31
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, 09:02 PM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,238

Rep: Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712
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 08:01 PM
C++ character search in text file lindaonline15 Programming 1 06-23-2011 07:07 AM
how to search and replace character from middle of line vishal_titre Linux - General 7 09-30-2008 02:46 AM
java, search character pattern in string howto xemous Programming 2 08-09-2006 07:53 AM
How to search for a character? mimf Linux - Newbie 1 12-03-2003 06:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:19 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
Open Source Consulting | Domain Registration