Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-25-2008, 05:46 AM   #1
Registered: Jan 2007
Location: Netherlands, The
Posts: 51

Rep: Reputation: 15
Unhappy Search values within multiple files line to line

Hello guys (and girls),

I've got the following script:



while read line
        F1=$(echo $line| grep '#00010' | cut -d$FS -f7)
        if [ -z $F1 ] ; then
          echo $F1
done < $FILE
In this script it reads one file, but I want to read a lot of files. I've got one directory with multiple files and directories. So this script has to read al those files and process line to line.

I was puzzeling with this script, I've got this:



for files in `find /opt/. -name "*.txt";
	while read line
			F1=$(echo $line| grep '#00010' | cut -d$FS -f7)
			if [ -z $F1 ] ; then
				echo $F1
	done < $files
This doesn't work, can anyone help me please?!


Last edited by Chrizzieej; 09-25-2008 at 06:11 AM.
Old 09-25-2008, 06:48 AM   #2
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
I don't know if it is the only problem, or maybe a mistake while typing, but the row
for files in `find /opt/. -name "*.txt";
is wrong, you forgot a ` and in /opt/. the dot is pointless. That should be
for files in `find /opt -name "*.txt"`;
Old 09-25-2008, 07:06 AM   #3
Senior Member
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
You're on the right track to wrap the while loop which processes lines in the for loop which processes the files; but, at the very least, you forgot the closing '`' in:
for files in `find /opt/. -name "*.txt";
### output from my command line:
$ for files in `find /opt/. -name "*.txt";
> do
>         echo $files
> done
### ^C to regain control of the xterm

$ for F in `find /opt/. -name "*.txt"`;
> do
>    echo $F
> done

If I may be so bold, let me offer some suggestions about programming style:
  • Indenting with tabs spreads your code too much, making it hard to read. The point of indenting, even in Python, is to make the code easier for humans to read &, therefore, to understand. I've found that 3 spaces is ideal for (most) bash scripts.

  • The bash convention is to use all upper case for variable names.

  • Long, (lower case), self documenting variable names may work in C, but where all instances of a variable are on 1 page, how hard is it to figure out what it means? If I start a loop w/ "for F in <list_of_files>", does it take a degree in rocket science to figure out that "$F" is the current file?

  • In any case "files" is plural & should not be used to stand for the, singular, current instance. You should at least be able to use "for FILE in $FILES ...".
Old 09-25-2008, 07:16 AM   #4
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
If you have Python, here's an alternative
#!/usr/bin/env python
import sys,os
for ROOT,DIR,FILES in os.walk(directory):
    for fi in FILES:
        if fi.endswith(".txt"):
            for lines in open(os.path.join(ROOT,fi)):
                if "#00010" in lines:
                        print "File ", os.path.join(ROOT,fi), " has field 7: ",f7
Old 09-25-2008, 07:34 PM   #5
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395

Actually, style is purely subjective

But I would say (slightly different)

Bash CONSTANTS in uppercase, other vars in lower-with-underscores.
Re the latter, just about all real progs I've worked on get longer/more complex sooner or later, and consistency is better.
Old 09-26-2008, 04:11 PM   #6
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46

about your syntax error the others always wrote. But I'm confused with your read loop: As far as I understand, you want to print field 7 as long as the lines contain the "#00010" character sequence and you want to stop reading the file at the first line, which does not contain this sequence?

Using your solution you are starting 2 sub shells and 2 external programs for each line - a lot of processes for large files!

You should better think about a specialised program like sed or awk (untested):
awk -F'#' /#00010/ { print $7; getline; }
          { nextfile; } ' `find /opt -name '*.txt' -print`
2 processes in summary for all files.



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
BASH: read every line in the files and use the line as parameters as another program tam3c36 Programming 10 12-07-2010 01:42 PM
how do I write a script using sed to delete the first and last line in multiple files Rikki D Programming 4 05-02-2008 11:01 PM
awk Question? Search by line, using 2 files? micksul Linux - Software 4 06-06-2007 06:34 AM
Search and Replace with multiple-line strings Programming 4 08-21-2005 02:32 PM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 01:56 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:55 PM.

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