Operating System - HP-UX
1833730 Members
2601 Online
110063 Solutions
New Discussion

How to suppress printing of a file with a particular pattern?

 
Marko_3
Contributor

How to suppress printing of a file with a particular pattern?

Dear all

I have a bunch of files which I want to use a shell script to filter out for billing. Only those files with line 79 (fixed line number) having the string 'John' will not be sent to the printer for billing. How can I use a combination of awk and other shell functions to do it?

Many thanks
4 REPLIES 4
James A. Donovan
Honored Contributor

Re: How to suppress printing of a file with a particular pattern?

Something like the following should work.

for FILE in `ls /dir/dir/files`; do
PRINT=`cat $FILE |head -n 79 |tail -n 1|grep John`

if [ "X$PRINT" = "XJohn" ]; then
lp -d myprinter $FILE
fi
done
Remember, wherever you go, there you are...
Bruce Regittko_1
Esteemed Contributor

Re: How to suppress printing of a file with a particular pattern?

Hi,

Here is a simple script that should do what you want. It will process each file in the current directory. You may replace the * with a file list if you want.

for filename in *
do
if head -79 $filename | tail -1 | grep -q John
then
: # do nothing
else
lp $filename
fi
done

The -q option to grep will suppress grep's stdout.

--Bruce
www.stratech.com/training
Andreas Voss
Honored Contributor

Re: How to suppress printing of a file with a particular pattern?

Hi,

just another idea:

for file in *
do
awk 'NR==79 && /John/{exit 1}' $file && lp $file
done

Regards
Printaporn_1
Esteemed Contributor

Re: How to suppress printing of a file with a particular pattern?

Hi,
Please use dos2ux command to convert dos file to unix file

#dos2ux dosfile > unixfile

Thanks
enjoy any little thing in my life