LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Loop through lines in file to find specified substring (https://www.linuxquestions.org/questions/linux-newbie-8/loop-through-lines-in-file-to-find-specified-substring-516249/)

dresch 01-04-2007 09:11 AM

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.

Can anyone help with this?

nx5000 01-04-2007 09:30 AM

I'll give you 2 methods:
Code:

grep -n string_to_search filename
Code:

awk '/string_to_search/{print NR" "$0}' filename

colucix 01-04-2007 09:32 AM

You can try this

Code:

nl <filename> | grep <substring>
'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.

nx5000 01-04-2007 09:33 AM

And here's another one:
Code:

cat -n filename | grep string_to_search

pixellany 01-04-2007 11:08 AM

AND---you can also do this with sed!!!

Is this homework by any chance?

colucix 01-04-2007 11:14 AM

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.

osor 01-04-2007 11:24 AM

Quote:

Originally Posted by colucix
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.

http://www.linuxquestions.org/linux/rules.html

colucix 01-04-2007 11:27 AM

Got it. I agree. Thank you.

pixellany 01-04-2007 11:35 AM

Quote:

Originally Posted by colucix
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.

colucix 01-04-2007 11:46 AM

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.

jschiwal 01-04-2007 12:19 PM

My :twocents:

Quote:

I need to loop through a text file, line by line
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.

pixellany 01-04-2007 01:19 PM

Quote:

Originally Posted by jschiwal
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."

makyo 01-04-2007 04:46 PM

Hi.
Quote:


Software Tools Principles

Do one thing well

Process lines of text, not binary

Use regular expressions

Default to standard I/O

Don't be chatty

Generate the same output format accepted as input

Let someone else do the hard part

Detour to build specialized tools

-- discussed in more detail in "Classic
Shell Programming", by Arnold Robbins and Nelson H. F.
Beebe, 2005, O'Reilly Media
cheers, makyo

dresch 01-04-2007 10:44 PM

grep not reading input from loop
 
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

for line in `cat -n $file`; do
grep 'one'
done

jschiwal 01-04-2007 11:39 PM

Quote:

Originally Posted by pixellany
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.


All times are GMT -5. The time now is 11:16 AM.