LinuxQuestions.org
Help answer threads with 0 replies.
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 08-17-2004, 11:32 AM   #1
naijaguy
Member
 
Registered: Aug 2004
Posts: 88

Rep: Reputation: 15
trying to figure out some output from grep


I have a collection of several .DAT files (text files), and I"m searching for a number string in them. At first I tried this:

Code:
grep '173924738' mydir/*
However, I just saw file contents being outputted to the screen, not the clean results of file and line numbers and line text that are supposed to be returned. So then I tried going through them one by one, like this:

Code:
grep '173924738' mydir/myfile.DAT
Most of the time grep returned with nothing and I was taken immediately to another prompt. However, a couple of times it once again just outputted the entire contents of the file (like cat or more would).

What's going on?
 
Old 08-17-2004, 11:42 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
By default, grep does not output the line numbers of a match. Also, grep will not print the filename that matches unless more than one file is provided to check against on the commandline. In other words, if you only tell grep to search one file, it assumes you know that any matching lines it returns are in that one file; there's no need to be redundant and display it.

If grep gives you no output at all, then the text you're checking for does not exist in the file(s) specified.

To force grep to display both the filename and the line number that contains matching text:
grep -Hn '173924738' mydir/*
 
Old 08-17-2004, 11:54 AM   #3
masand
LQ Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 58
if the above metjhod does not work out then u can try this one

i hope this works
#ls -al | grep 'your string here'

regards
gaurav
 
Old 08-17-2004, 12:04 PM   #4
naijaguy
Member
 
Registered: Aug 2004
Posts: 88

Original Poster
Rep: Reputation: 15
Thanks, I appreciate it. However, my problem still remains. When I used those parameters, I still didn't get any line numbers, and when I ran it with the search string on the whole directory, since the search string showed up in 2 files it outputted the text of those 2 files. I'm using Red Hat 8 and it's GNU grep 2.5.1 and I'm just wondering if my file is somehow confusing grep.
 
Old 08-17-2004, 12:08 PM   #5
naijaguy
Member
 
Registered: Aug 2004
Posts: 88

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by masand


#ls -al | grep 'your string here'


I changed directory to the one containing the files and tried that line above, but nothing was outputted and it just returned to a prompt (and I know from using Windows Grep on the files that the search string does in fact appear in a couple of them).
 
Old 08-17-2004, 12:10 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
The line numbers should be embedded in the output itself. For instance, something like:
some_file.dat:15:blah, blah, blah... 173924738 ... blah, blah, blah

some_file.dat is the file containing the matching text. 15 is the line number, and the "blah" stuff is the text on that line.

I'm on a Red Hat 8 system, and I've got the exact same version of grep you do, so we should be able to figure this out. Post the grep command you used, and the output (or a snippet if it's too long), and then explain what you were hoping to see. Then we can go from there.
 
Old 08-17-2004, 12:20 PM   #7
naijaguy
Member
 
Registered: Aug 2004
Posts: 88

Original Poster
Rep: Reputation: 15
Thanks, ok, here's what I typed:

Code:
-Hn '776194769' archive/Test813.DAT
This is a fixed width text file that we parse to load info into a database. It's about 2MB, so it's a lot of text and it takes a while to pump this out onto the terminal, and for that reason I'm pretty sure it's just outputting the contents of the file, because this is a sample of what I get:

067067067 0077502132300776744209 C4 NA60LN64 067067067067067067067067067067067067067067 0077502132300776744209 C4 NA70LN74 0670670670670 67067067067067067067067067067 00775 02132300776744209 C4 NA80LN84 067067067067067067067067067067067067067067 0077502132300776744209 C4 SD 30LS34 067067067067067067067067067067067067067067 0077502132300776744209 C4 SD40LS44 06706706706706706706706 7067067067067067067 007750213230077 6744209 C4 SD50LS54 067067067067067067067067067067067067067067 0077502132300776744209 C4 SD60LS64 067 067067067067067067067067067067067067067 0077502132300776744209 C4 SD70LS74 067067067067067067067067067067067 067067067 0077502132300776744209 C4 SD80LS84 067067067067067067067067067067067067067067 0077502132300776744209 C4 GN2LG24 0100100100100 10010010010010010010010010010 00775 02132300776744209 C4 GN3LG34 010010010010010010010010010010010010010010 0077502132300776744209 C4 GN 4LG44 010010010010010010010010010010010010010010 0077502132300776744209 C4 GN5LG54 01001001001001001001001 0010010010010010010 007750213230077 6744209 C4 GN6LG64 010010010010010010010010010010010010010010 0077502132300776744209 C4 GN7LG74 010 010010010010010010010010010010010010010 0077502132300776744209 C4 GN8LG84 010010010010010010010010010010010
 
Old 08-17-2004, 12:26 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Ok, I think I realize what's causing the confusion. The text being displayed wraps across multiple lines. Your shell has a limit to how many characters it can display horizontally. A line of text in a file has no such limitation. Each "line" of text you see in the terminal is actually just a continuation of one really, really long line in the file.

Here's another command to try and clear things up.
grep -Hn '173924738' mydir/* | cut -f 1-2 -d ':'

That will limit the output to only:
filename:line number

If you'd like to make its a little more readable beyond that, then this might help:
grep -Hn '173924738' mydir/* | cut -f 1-2 -d ':' | sed 's/:/ - line /'

In that case, the output will be changed to:
filename - line line number

Last edited by Dark_Helmet; 08-17-2004 at 12:33 PM.
 
Old 08-17-2004, 12:44 PM   #9
naijaguy
Member
 
Registered: Aug 2004
Posts: 88

Original Poster
Rep: Reputation: 15
Oh, duh, you're absolutely right! Using that first line you mentioned, it said that the search string could be found on line 1. However, it is actually really far down in the file, so I guess whatever is terminating these lines is not considered a new line by grep (but at least it quickly answers the question of whether or not it is in the file!).
 
  


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
grep input/output error on slackware 10.1 install dpp777 Linux - Newbie 1 09-07-2005 09:25 AM
output to a file - cat? grep? Godsmacker777 Linux - Newbie 6 12-08-2004 10:06 AM
utf , standard output / input and grep too Fascistchicken Linux - Software 2 11-05-2004 08:52 AM
sort grep output wijnands Linux - Newbie 4 10-09-2004 07:14 AM
How can I filter the output of grep to exclude certain cases? QtCoder Linux - General 1 03-28-2004 12:05 AM

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

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