Link to home
Start Free TrialLog in
Avatar of SRG041808
SRG041808Flag for United States of America

asked on

cat command read one file at a time

Currently I have a cat command that reads all the files in a directory, grep the data I want, then output it to a file.

Is there a way to have each file processed 1 at a time?

Below is my code and example output.

I would like the output to be
 
042811104531$  <--this is BEGIN:VCAL output
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN$  <---this is PROID: output
042811104531$ <---this is VERSION:2.0 output
042811104531$   <--this is BEGIN:VCAL output
PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN$    <---this is PROID: output
042811104531$   <---this is VERSION:2.0 output

#process each file
for f in $todaydir
do
	echo "Processing file(s)..."
	#cat -E dfp* | egrep -i -e "VCAL|BEGIN:VEVENT|VERSION|SUMMARY|DESCRIPTION|DTSTART;TZID|DTEND;TZID|LOCATION|VEVENT|TZNAME|TRANSP:OPAQUE" > /root/Desktop/test.txt
	cat -E dfp* | egrep -i -e "BEGIN:VCAL" > /root/Desktop/test.txt
	cat -E dfp* | egrep -i -e "PRODID:" >> /root/Desktop/test.txt
	cat -E dfp* | egrep -i -e "VERSION:2.0" >> /root/Desktop/test.txt
done

Open in new window

#current output below
042811104531$
042811104531$
PRODID:-//Mozilla.org/NONSGML Mozilla Calendar V1.1//EN$
PRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN$
042811104531$
042811104531$

Open in new window

Avatar of farzanj
farzanj
Flag of Canada image

Try something like this:


#!/bin/bash

FOLDER=/path/folder
FILE=/root/Desktop/test.txt
cd $FOLDER

for f in $(ls $FOLDER)
do
   echo "Now processing $f"
   egrep -i -e "VCAL|BEGIN:VEVENT|VERSION|SUMMARY|DESCRIPTION|DTSTART;TZID|DTEND;TZID|LOCATION|VEVENT|TZNAME|TRANSP:OPAQUE" $f >> $FILE
egrep -i -e "BEGIN:VCAL" > /root/Desktop/test.txt
   egrep -i -e "PRODID:" $f >> $FILE
   egrep -i -e "VERSION:2.0" $f >> $FILE
done

Open in new window

Avatar of SRG041808

ASKER

I think I see what we are trying to do but when I run it the script finds the correct directory and starts "processing files filenamehere" and appears to hang.
when I comment out the egrep commands it completes
Which egrep? All of them?

I have provided $f filenames in egrep.  This would hang if the filename is missing OR is an empty string.  Since you see the filename, I assume you are missing the filenames.

Let me know.
Yes, I commented out all of them and re-add them as I figure out who is the troubled line...  Attached current script running output and modifications i have made to code.  in the FOR statement using ls or dir does not appear to have an effect...   the files that start with qfp do not need to be read.  
[root@myserver Desktop]# ./reademail.sh 
/data/MailArchive/archivecal/20110428
Now processing dfp3SDu5Fd009202
Now processing dfp3SE1aZ3012340
Now processing /data/MailArchive/archivecal/20110428:
egrep: /data/MailArchive/archivecal/20110428:: No such file or directory
Now processing dfp3SDu5Fd009202
Now processing dfp3SE1aZ3012340
Now processing qfp3SDu5Fd009202
Now processing qfp3SE1aZ3012340
script complete
[root@myserver Desktop]#

Open in new window

todaydir="/data/MailArchive/archivecal/"`eval date +%Y%m%d`
#make sure we found correct directory
echo $todaydir
#go into the directory
cd $todaydir
#process each file
FILE=/root/Desktop/test.txt

for f in $(dir dfp* $todaydir)
do
   echo "Now processing "$f
   cat -E dfp* |egrep -i -e "VCAL|BEGIN:VEVENT|VERSION|SUMMARY|DESCRIPTION|DTSTART;TZID|DTEND;TZID|LOCATION|VEVENT|TZNAME|TRANSP:OPAQUE" $f >> $FILE
	#egrep -i -e "BEGIN:VCAL" > $f
  	#egrep -i -e "PRODID:" $f >> $FILE
  	#egrep -i -e "VERSION:2.0" $f >> $FILE
done

Open in new window

There is a problem in the first commented egrep

It should be
egrep -i -e "BEGIN:VCAL" $f >>$FILE
Good call.  I fixed that line...  It appears somewhere that is it trying to process the folder name instead of the file name.  

It is also still trying to read the qfp* files instead of the dfp* files
ASKER CERTIFIED SOLUTION
Avatar of farzanj
farzanj
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did the trick thanks!