LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Need help with a script that looks for contents of a file (https://www.linuxquestions.org/questions/linux-general-1/need-help-with-a-script-that-looks-for-contents-of-a-file-507677/)

rsmccain 12-05-2006 12:21 PM

Need help with a script that looks for contents of a file
 
Inside of the directory /etc/lp/printers we have many subdirectories which define printers, ie:

/etc/lp/printers/printer1
/etc/lp/printers/printer2
/etc/lp/printers/printer3
/etc/lp/printers/printer4
...
...
/etc/lp/printers/printer50

within each of those subdirectories is a file called "comment". ie:

/etc/lp/printers/printer1/comment

I need a script that will go through all the "comment" files for all of the printers and output the contents of every "comment" files that start with the letter U .

I tried messing around with some bash for a couple hours and i'm back at square 1.

Thanks, Ryan

matthewg42 12-05-2006 12:44 PM

Quote:

Originally Posted by rsmccain
I need a script that will go through all the "comment" files for all of the printers and output the contents of every "comment" files that start with the letter U .

What do you mean? Where the first character of the first line of the file is a 'U', or any line in the file?

rsmccain 12-05-2006 12:46 PM

The first character of the first line is a "U".

Thanks, Ryan

matthewg42 12-05-2006 12:56 PM

Well, you can find the list of files using find. The -name option will do this:
Code:

find /etc/lp/printers -name comment
You can pipe this into a little shell loop:
Code:

... | while read filename; do ... done
And for each file you could do the test like this. There'll be lots of methods for this. You can use grep (with head), I sure you can use sed too, or any number of methods. I like perl. I use perl.
Code:

perl -ne 'if ( $_ =~ /^U/ ) { exit 0; } else { exit 1; }'
...and then you can test $? to decide what to do with the file.

rsmccain 12-05-2006 04:16 PM

Quote:

Originally Posted by matthewg42
Well, you can find the list of files using find. The -name option will do this:
Code:

find /etc/lp/printers -name comment
You can pipe this into a little shell loop:
Code:

... | while read filename; do ... done
And for each file you could do the test like this. There'll be lots of methods for this. You can use grep (with head), I sure you can use sed too, or any number of methods. I like perl. I use perl.
Code:

perl -ne 'if ( $_ =~ /^U/ ) { exit 0; } else { exit 1; }'
...and then you can test $? to decide what to do with the file.


thanks.. that did the trick. i need to really take a week off and learn bash. :study:

matthewg42 12-05-2006 04:26 PM

bash is the glue, the standard utilities do most of the work. It's just such a useful system, I feel the daily need to thank my lucky stars for the *nix way.


All times are GMT -5. The time now is 05:58 AM.