LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 05-16-2013, 11:55 AM   #1
rookie11
LQ Newbie
 
Registered: May 2013
Posts: 4

Rep: Reputation: Disabled
Question grep output


i have file of 10MB in size containing long file list.
need to grep output to another file.

i am doing this :
more filelist | grep S_DOC_PPSL* > /tmp/output

more output
Binary file (standard input) matches

please correct my mistake.
 
Old 05-16-2013, 12:08 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
grep S_DOC_PPSL filelist > output

Usage for grep is:

grep <options> pattern file
 
1 members found this post helpful.
Old 05-16-2013, 12:14 PM   #3
cynwulf
Senior Member
 
Registered: Apr 2005
Location: The Black Country, UK
Posts: 1,741
Blog Entries: 1

Rep: Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774Reputation: 774
Code:
# grep S_DOC_PPSL filelist > /tmp/output
No need for more

Is the asterisk intended as a wildcard or part of the string? If it's the former no need for that either...

If it's the latter, then you will probably need to escape it
Code:
# grep S_DOC_PPSL\* filelist > /tmp/output
//edit: You will need to be root to write to /tmp

//edit: just saw the previous post - yes writing the output to a file called output or ~/tmp/output is preferable to becoming root.

Last edited by cynwulf; 05-16-2013 at 12:18 PM.
 
2 members found this post helpful.
Old 05-16-2013, 12:27 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Quote:
Originally Posted by caravel View Post
If it's the latter, then you will probably need to escape it
Code:
# grep S_DOC_PPSL\* filelist > /tmp/output
I usually use double quotes to avoid escaping either SPACE or other wildcards.

Code:
grep "S_DOC_*" filelist
Thanks for pointing out the subtlety which I didn't explain, caravel. I indicated a local file versus targeting the output into the /tmp tree because /tmp is owned typically by root and it's usually best, especially for a simple grep of one file, to place the captured output into a file in the sam directory where you are performing the grep. However you can place it somewhere else, most would recommend that you place it into a directory which your username owns.
 
1 members found this post helpful.
Old 05-16-2013, 12:29 PM   #5
rookie11
LQ Newbie
 
Registered: May 2013
Posts: 4

Original Poster
Rep: Reputation: Disabled
* is used as wildcard. millions of files in filelist but i need only which starts with S_DOC_PPSL name
 
Old 05-16-2013, 12:35 PM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Yes, we understand what you're trying to grep. Did the solutions clarify it for you as to how to do it correctly?
 
1 members found this post helpful.
Old 05-16-2013, 02:26 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Careful here, grep uses regular expressions for it's pattern matching, not globbing (a.k.a. wildcards)!

In regex '*' means 'zero or more of the preceding character', so 'S_DOC_*' will match the text string 'S_DOC', followed by zero or more underscore characters, and only underscores (with no regard to what comes after that--see below)

If you want to match any number of any kind of characters after the explicit text, you need to use the '.' regex, which means any character, in front of the asterix: i.e. 'S_DOC_.*'.

Another difference between globbing and regex though is that globbing patterns must be composed to match the entire string, but regex only needs to match the desired substring. "S_DOC_" is therefore the same as ".*S_DOC_.*" (or the '*S_DOC_*' gobbing pattern), and will match that pattern anywhere in the line.

Finally, however, to make sure the regex only matches the string at the beginning of the file, you need to anchor it with the '^' character (which means "start of line"). The "^S_DOC_" regex is equivalent to the "S_DOC_*" globbing pattern, and probably what you want to use here.

I highly recommend taking some time to learn at least the basics of regex. You won't regret it.

Here are a few regular expressions tutorials:
http://mywiki.wooledge.org/RegularExpression
http://www.grymoire.com/Unix/Regular.html
http://www.regular-expressions.info/

Last edited by David the H.; 05-16-2013 at 02:42 PM. Reason: minor changes for clarity
 
3 members found this post helpful.
Old 05-16-2013, 02:38 PM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,087
Blog Entries: 10

Rep: Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521Reputation: 1521
Well, son of a .... NEVER knew that one. And you are 1000% correct which I see by going through my examples.

Rookie, make sure you listen to David the H there for your pattern.

Code:
desktop:~$ grep --version
GNU grep 2.5.4

Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

desktop:~$ cat color-info.txt 
25x25x425x590
CATV=ff8c00
GAS=ffff00
POWER=ff0000
SEWER=11f411
PHONE=ff8c00
WATER=0000ff
HOOKUP=000000
DAMAGE=ff0000
desktop:~$ grep "*WE*" color-info.txt 
desktop:~$ grep "WE*" color-info.txt 
POWER=ff0000
SEWER=11f411
WATER=0000ff
desktop:~$ grep WE\* color-info.txt 
POWER=ff0000
SEWER=11f411
WATER=0000ff
desktop:~$ grep "WE.*" color-info.txt 
POWER=ff0000
SEWER=11f411
desktop:~$
 
1 members found this post helpful.
  


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
using grep to output non-matches into output file binny959 Linux - General 5 12-25-2010 12:20 AM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
Grep on output of command returns all output traigo Linux - Newbie 2 02-24-2009 06:15 PM
ps -eH | grep java output in a active passive clustered output johnkalikavunkal Linux - Server 2 01-31-2009 12:21 AM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 05:56 AM


All times are GMT -5. The time now is 01:01 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