LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 09-25-2008, 05:46 AM   #1
Chrizzieej
Member
 
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:

Code:
#!/bin/bash

FILE="/opt/test.txt"
FS="#"

while read line
do
        F1=$(echo $line| grep '#00010' | cut -d$FS -f7)
        if [ -z $F1 ] ; then
          exit
        else
          echo $F1
        fi
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:

Code:
#!/bin/bash

FS="#"

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

Thanx,
Chrizzieej

Last edited by Chrizzieej; 09-25-2008 at 06:11 AM.
 
Old 09-25-2008, 06:48 AM   #2
clvic
Member
 
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
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
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:
Code:
for files in `find /opt/. -name "*.txt";
Code:
### 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
/opt/./grisoft/avg7/doc/license_cz.txt
/opt/./grisoft/avg7/doc/license_us.txt
/opt/./grisoft/avg7/doc/lgpl.txt
/opt/./grisoft/avggui/doc/license_cz_utf8.txt

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
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
If you have Python, here's an alternative
Code:
#!/usr/bin/env python
import sys,os
directory="/path/to/start"
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:
                    try:
                        f7=lines.split("#")[6]
                    except:            
                        pass
                    else:
                        print "File ", os.path.join(ROOT,fi), " has field 7: ",f7
 
Old 09-25-2008, 07:34 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,344

Rep: Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746Reputation: 2746
@archtoad:

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
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

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):
Code:
awk -F'#' /#00010/ { print $7; getline; }
          { nextfile; } ' `find /opt -name '*.txt' -print`
2 processes in summary for all files.

Jan
 
  


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
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 ChristianNerds.com 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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:56 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