LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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 03-13-2012, 02:47 PM   #1
Farah_s
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Rep: Reputation: Disabled
how i can fetch some strings like words or some words


dears
i need fast command to get some word in documents and copy the lines which contain these words to another file

so example if i have file called tota.txt and hold many lines contain word " manager"
how i can filtarized the lines which hold word manager and copy it to another file
i use some command like

sed -n -e '/manager/p' > /tmp/test =====> as example only is that right
 
Old 03-13-2012, 03:11 PM   #2
ovenwolv
LQ Newbie
 
Registered: Mar 2012
Posts: 3

Rep: Reputation: 1
Code:
cat tota.txt | grep -e 'manager' > /tmp/test
maybe.
 
Old 03-13-2012, 03:15 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
grep is generally the program to use for matching and printing lines.

sed is a more powerful program that allows you to modify the text as well, so it's really overkill here, but yes, it can also be used as a grep substitute.

Code:
grep 'string' infile > outfile

sed -n '/string/p' infile > outfile
Edit @ovenwolv. You're correct in general, but useless use of cat. grep and sed can both read files directly.

Last edited by David the H.; 03-13-2012 at 03:17 PM.
 
Old 03-13-2012, 03:23 PM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,572
Blog Entries: 1

Rep: Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027Reputation: 1027
Just to elaborate on the sed solution:

Code:
sed -n '/string/p' infile
will not only match the lines containing 'string', but also 'strings'.

For example:

Code:
sed -n '/love/p' file
will match:
Quote:
love, loves, loved, lover, beloved, etc.
If you want the lines with the exact string, you'd have to use:

Code:
sed -n '/\<love\>/p' file

Last edited by sycamorex; 03-13-2012 at 03:24 PM.
 
Old 03-13-2012, 03:32 PM   #5
ovenwolv
LQ Newbie
 
Registered: Mar 2012
Posts: 3

Rep: Reputation: 1
Red face

Quote:
Originally Posted by David the H. View Post
Edit @ovenwolv. You're correct in general, but useless use of cat. grep and sed can both read files directly.
I win the award.
 
Old 03-13-2012, 03:39 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949Reputation: 1949
Good catch. But that's true of both commands. It's really the unbounded regular expression that's matching the substring.

grep however will still match substrings even if you use the -F option to disable regex. In which case you can also use the -w option to have it match whole words.
 
Old 03-14-2012, 01:23 AM   #7
Farah_s
LQ Newbie
 
Registered: Mar 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
thanks

thanks all i really thank you all
 
  


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
[SOLVED] Identify words having repeated character strings danielbmartin Programming 22 02-23-2012 11:51 AM
[SOLVED] Select words with alphabetical-order character strings danielbmartin Programming 7 02-17-2012 12:08 AM
[SOLVED] Array of words(strings)- please help! stefanolima Programming 4 07-12-2010 04:15 AM
Removing white spaces between words and joining the words in a given format Priyabio Linux - General 4 08-20-2009 07:42 AM
Search and Replace: Asian Words to English Words ieeestd802 Linux - Software 0 10-27-2004 07:48 PM


All times are GMT -5. The time now is 02:09 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration