LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how i can fetch some strings like words or some words (http://www.linuxquestions.org/questions/linux-newbie-8/how-i-can-fetch-some-strings-like-words-or-some-words-934275/)

Farah_s 03-13-2012 03:47 PM

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

ovenwolv 03-13-2012 04:11 PM

Code:

cat tota.txt | grep -e 'manager' > /tmp/test
maybe.

David the H. 03-13-2012 04:15 PM

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.

sycamorex 03-13-2012 04:23 PM

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

ovenwolv 03-13-2012 04:32 PM

Quote:

Originally Posted by David the H. (Post 4625989)
Edit @ovenwolv. You're correct in general, but useless use of cat. grep and sed can both read files directly.

I win the award.

David the H. 03-13-2012 04:39 PM

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.

Farah_s 03-14-2012 02:23 AM

thanks
 
thanks all i really thank you all


All times are GMT -5. The time now is 01:47 PM.