ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
#!/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.
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"`;
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 ...".
#!/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
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.
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):
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.