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!
Notices
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Loop through lines in file to find specified substring
I need to loop through a text file, line by line looking for a specified substring. If the substring is found in a line then echo that line and line number.
'nl' add line numbers to the text file and 'grep' search for occurrences of a string or substring in the whole file. Use 'grep -i' if you want to ignore upper/lowercase.
To pixellany: you often talk about homework, but I don't understand what is the problem. Sorry for post a message not related to the thread, but I am curious about this issue. Thank you.
To pixellany: you often talk about homework, but I don't understand what is the problem. Sorry for post a message not related to the thread, but I am curious about this issue. Thank you.
To pixellany: you often talk about homework, but I don't understand what is the problem. Sorry for post a message not related to the thread, but I am curious about this issue. Thank you.
OFTEN????
I usually just respond unless the OP seems unable or unwilling to do anything on their own.
Note that the rules don't prohibit asking or answering homework questions. They just say that their can be no expectation of help.
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.
because of the "Everything is Text" principle. So you'd be better served thinking about the processing of a stream of characters, instead of ad hoc procedures.
I am not aware of any Linux (or Unix) principle like this.
I thought the paradigm was: "Everything is a file."
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?
#!/bin/bash
# Find each line in a file that contains a
# specified substring
name=${0##*/}
file=${1-'pwd'}
if [ ! -f $file ]; then
echo "'$file' is not a file"
echo "Usage: $name [file]"
exit 1
fi
# Check if file has information.
if [ ! -s $file ]; then
echo "'$file' contains no information"
exit 2
fi
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.
Code:
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
sparse device.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.