Loop through lines in file to find specified substring
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 agree with you and with this rule. In the research world, where I'm working, I am expected to answer to all the questions from people who never wanted to waste a minute by figure out a solution by himself! It is part of my job and it is very frustrating, indeed. Anyway thank you for your answer. I have got the point, now.
There may be a philosophical point that you haven't caught on to yet.
Processing in Unix/Linux is often handled as a flow of textual data, passing though a number of specialized filters. The data goes from one utility to another through pipes until you get the result that you want. This works so well in Linux, even for audio or video, because of the "Everything is a File" principle. So you'd be better served thinking about the processing of a stream of characters, instead of ad hoc procedures.
While a tool like sed does read in a file line by line, this detail is handled (hidden) in sed itself.
With the following code taking input from a file using a loop and the cat command, the grep command does not appear to be searching through the input. When executing the script it brings up a blank line waiting for input.
What am I doing wrong?
# Find each line in a file that contains a
# specified substring
if [ ! -f $file ]; then
echo "'$file' is not a file"
echo "Usage: $name [file]"
# Check if file has information.
if [ ! -s $file ]; then
echo "'$file' contains no information"
I am not aware of any Linux (or Unix) principle like this.
I thought the paradigm was: "Everything is a file."
Yes, you are right. I was thinking ahead to much and typed "Text" by mistake. Thanks for the correction.
However, a file in *nix is a stream of characters, whereas in other OS's there is Meta data externally associated with files. You can even set up a sparse drive with an echo command.
To create a sparse device, start by creating a dm-zero device that's the
desired size of the sparse device. For this example, we'll assume a 10TB
TEN_TERABYTES=`expr 10 \* 1024 \* 1024 \* 1024 \* 2` # 10 TB in sectors
echo "0 $TEN_TERABYTES zero" | dmsetup create zero1
Ironically, an OS that uses hidden info on a harddrive to store file metadata is BSD based Mac. ( Which is carried over from before OS X.) The Windows NTFS filesystem was designed to support this, so NT servers could be sold to Mac users. But someone must have forgotten about it. It wasn't until recently that someone working on a damaged drive off line discovered that virus writers were using this feature to invisibly store their payloads without the Windows OS taking notice.