LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-05-2006, 01:21 PM   #1
rsmccain
Member
 
Registered: Apr 2004
Location: Louisiana
Distribution: SUSE
Posts: 154

Rep: Reputation: 30
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
 
Old 12-05-2006, 01:44 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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?
 
Old 12-05-2006, 01:46 PM   #3
rsmccain
Member
 
Registered: Apr 2004
Location: Louisiana
Distribution: SUSE
Posts: 154

Original Poster
Rep: Reputation: 30
The first character of the first line is a "U".

Thanks, Ryan
 
Old 12-05-2006, 01:56 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 12-05-2006, 05:16 PM   #5
rsmccain
Member
 
Registered: Apr 2004
Location: Louisiana
Distribution: SUSE
Posts: 154

Original Poster
Rep: Reputation: 30
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.
 
Old 12-05-2006, 05:26 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

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


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
synchronize contents two files with a bash script paul_mat Linux - Software 5 01-17-2007 07:31 AM
Deleting Contents of File abdul_zu Linux - Software 3 12-04-2005 03:12 PM
Bash script to compare dir contents Boffy Programming 2 08-02-2005 07:08 AM
Script to check for contents in a file darin3200 Programming 7 04-25-2004 06:26 PM
Delete contents of the file, but not file? mikeshn Linux - General 9 05-28-2003 07:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:39 PM.

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