LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-04-2007, 09:11 AM   #1
dresch
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Rep: Reputation: 0
Unhappy 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?
 
Old 01-04-2007, 09:30 AM   #2
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
I'll give you 2 methods:
Code:
grep -n string_to_search filename
Code:
awk '/string_to_search/{print NR" "$0}' filename
 
Old 01-04-2007, 09:32 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 01-04-2007, 09:33 AM   #4
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
And here's another one:
Code:
cat -n filename | grep string_to_search
 
Old 01-04-2007, 11:08 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
AND---you can also do this with sed!!!

Is this homework by any chance?
 
Old 01-04-2007, 11:14 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 01-04-2007, 11:24 AM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
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
 
Old 01-04-2007, 11:27 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Got it. I agree. Thank you.
 
Old 01-04-2007, 11:35 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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.
 
Old 01-04-2007, 11:46 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 01-04-2007, 12:19 PM   #11
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
My

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.

Last edited by jschiwal; 01-04-2007 at 11:37 PM.
 
Old 01-04-2007, 01:19 PM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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."
 
Old 01-04-2007, 04:46 PM   #13
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 735

Rep: Reputation: 76
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
 
Old 01-04-2007, 10:44 PM   #14
dresch
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Original Poster
Rep: Reputation: 0
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
 
Old 01-04-2007, 11:39 PM   #15
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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.

Last edited by jschiwal; 01-04-2007 at 11:53 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
shell program, how to find substring plz madhugp Programming 1 12-29-2006 10:43 AM
how to find an exact substring match? ldp Programming 7 02-22-2005 06:28 AM
[c shell] How do I find how many lines a file has? saiz66 Programming 5 10-08-2004 03:01 PM
Python - Set vars and loop over lines in file jnoller Programming 6 02-07-2004 10:32 AM
Loop over lines in text file? amaze Linux - General 2 08-12-2003 07:15 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration