Linux - NewbieThis 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!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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:
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:
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).
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/*
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.
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).
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.
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:
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:
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.
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!).