LinuxQuestions.org
Register a domain and help support LQ
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 04-17-2013, 12:19 AM   #1
komalp
LQ Newbie
 
Registered: Apr 2013
Posts: 10

Rep: Reputation: Disabled
Find Particular line in file in Shell Script


Hii,

In my program, i want to find a line in a file which have my given line. this given line is line of another file.


for example:

if file1 is :

abc cde efg ko is , fddg
konnn jjji hjhu
abc fdfd ghg l

and

file2
abc cde efg ko is
yuyuy jjji hjhu
hgjghj fdfd ghg l

from file2 , given line is : abc cde efg ko is
(it may have more than one line)

then o/p is : abc cde efg ko is , fddg
 
Old 04-17-2013, 12:32 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
I think you just need grep:
Code:
grep -f file2 file
Use -F if you mean plain patterns only, or fgrep
Code:
grep -F -f file2 file
fgrep -f file2 file
 
1 members found this post helpful.
Old 04-17-2013, 01:58 AM   #3
eklavya
Member
 
Registered: Mar 2013
Posts: 619

Rep: Reputation: 136Reputation: 136
If you want to search a particular line in multiple files, you can use following code.
Code:
grep -r "abc cde efg ko is" /path/of/file1 /path/of/file2
It will give you the lines with file name where searched text is appeared.

If you know the directory but you do not know the files where text is appeared then grep -r will do that for you

Code:
grep -r "abc cde efg ko is" /path/of/directory1 /path/of/directory2
Now it will list the files with searched text where the text abc cde efg ko is is appeared in the files of these two directories,

In above example, it will search text abc cde efg ko is in all files of directory1 & directory2 and display a list in the output.

Last edited by eklavya; 04-17-2013 at 01:59 AM.
 
Old 04-17-2013, 04:11 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
If your purpose is to search all lines from file2 in file1, then a simple script would help you:
Code:
#!/bin/bash
while read -r line
do
echo "Searching for line $line"
grep "$line" file1
done < file2
 
1 members found this post helpful.
Old 04-17-2013, 11:48 AM   #5
komalp
LQ Newbie
 
Registered: Apr 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
konsolebox
It's work...but actually i need individual match for each line. grep command give for all lines of file. Also i want to perform some operation on matched line so i need matched line individually...thank you for yr help.
 
Old 04-17-2013, 11:50 AM   #6
komalp
LQ Newbie
 
Registered: Apr 2013
Posts: 10

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by shivaa View Post
If your purpose is to search all lines from file2 in file1, then a simple script would help you:
Code:
#!/bin/bash
while read -r line
do
echo "Searching for line $line"
grep "$line" file1
done < file2
yehhh..it's work... thank you..
 
Old 04-17-2013, 12:06 PM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by komalp View Post
konsolebox
It's work...but actually i need individual match for each line. grep command give for all lines of file. Also i want to perform some operation on matched line so i need matched line individually...thank you for yr help.
You mean you want them unique? Perhaps:
Code:
fgrep -f file2 file1 | sort -u
?
 
  


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
bash shell script read file line by line. Darren[UoW] Programming 57 04-17-2016 07:07 PM
how to replace line of file with another line using awk in shell script amit_pansuria Programming 3 03-29-2009 10:43 AM
shell script to find data that's all in one line wattz Programming 5 07-10-2008 05:48 PM
shell script find a line and the next line (grep?) metalx1000 Programming 5 07-24-2007 09:41 PM


All times are GMT -5. The time now is 10:38 PM.

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