LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   trying to figure out some output from grep (https://www.linuxquestions.org/questions/linux-newbie-8/trying-to-figure-out-some-output-from-grep-218804/)

naijaguy 08-17-2004 11:32 AM

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?

Dark_Helmet 08-17-2004 11:42 AM

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/*

masand 08-17-2004 11:54 AM

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

naijaguy 08-17-2004 12:04 PM

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.

naijaguy 08-17-2004 12:08 PM

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).

Dark_Helmet 08-17-2004 12:10 PM

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.

naijaguy 08-17-2004 12:20 PM

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

Dark_Helmet 08-17-2004 12:26 PM

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

naijaguy 08-17-2004 12:44 PM

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!).


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