Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading data from a file

Status
Not open for further replies.

LGJ

Programmer
Mar 7, 2003
50
GB
Hi all,

I need to read data from a file like e.g.

thread:1 file1.c:200
thread:1 file1.c:255
thread:1 file1.c:300
thread:2 file1.c:300
thread:2 file1.c:455
thread:2 file1.c:100

I know how to get each item by e.g.

for data in `cat data`
do
#do something
done

BUT how do I know when the data I get is on the next line? The above will just keep getting the data but I can't tell if there from the same lines or not (say if one line had 3 bits of data and next had 2).

Thanks for any help or pointers

LGJ
 
You may consider something like this:
while read thread file1 file2 junk
do
if [ -z "$file2" ]; then
echo "Only 2 bits of data in this line"
fi
done</path/to/file

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Your delimiters are a bit strange, its hard to understand your data, perhaps you might clarify what you see as an individual field. Are you using the colon to delimit your fields? All your records are all the same length. More info might help.
 
I had to write a sript that read in a file that contained the results of a directory listing to check if the file name existed in a different directory and if it did, did it have the correct permissions, owner and group.
When you do a read each column group of data becomes a field

Here's a short example of what I did:
Hope it helps

ls -l $testsrc > $workdir/$testsrc
ls -l $devsrc > $workdir/$devsrc
ls -l $prodsrc > $workdir/$prodsrc
while read a b c d e f g h i
do
if test "$i" = ' '
then
continue
fi
if [ -a $prodsrc/$i ]
then
echo 'do nothing' > $workdir/junklog
else
continue
fi
if test "$c" = 'root'
then
echo 'do nothing' > $workdir/junklog
else
echo 'In prod source file' "$i" ' is not owned by root' >> $workdir/scriptlog
fi
if test "$d" = 'root'
then
echo 'do nothing' > $workdir/junklog
else
echo 'In prod source file' "$i" 'does not belong to group root' >> $workdir/scriptlog
fi
if test "$a" = '-rw-r--r--'
then
echo 'do nothing' > $workdir/junklog
else
if test "$a" = 'd*********'
then
echo 'do nothing' > $workdir/junklog
else
echo 'In prod source file' "$i" 'has the wrong permissions' >> $workdir/scriptlog
fi
fi

if [ -d $prodsrc/$i ]
then
if [ -d $testsrc/$i ]
then
continue
else
echo 'directory ' "$i" 'does not exist in testsrc' >> $workdir/scriptlog
continue
fi
fi
if [ -f $testsrc/$i ]
then
cp /dev/null $workdir/findlog
find $prodsrc/$i -newer $testsrc/$i > $workdir/findlog
if [ -s $workdir/findlog ]
then
echo 'file ' "$prodsrc/$i" ' is newer than ' "$testsrc/$i" >> $workdir/scriptlog
rm $workdir/findlog
else
rm $workdir/findlog
else
cp /dev/null $workdir/findlog
find $testsrc/$i -newer $prodsrc/$i > $workdir/findlog
if [ -s $workdir/findlog ]
then
echo 'file ' "$testsrc/$i" ' is newer than ' "$prodsrc/$i" >> $workdir/scriptlog
rm $workdir/findlog
fi
fi
cp /dev/null $workdir/findlog
diff $prodsrc/$i $testsrc/$i > $workdir/findlog
if [ -s $workdir/findlog ]
then
echo 'files ' "$prodsrc/$i" ' do not match ' "$testsrc/$i" >> $workdir/scriptlog
fi
else
echo 'file ' "$i" 'does not exist in testsrc' >> $workdir/scriptlog
fi
###NOW COMPARE PROD SOURCE TO DEV SOURCE
if [ -d $prodsrc/$i ]
then
if [ -d $devsrc/$i ]
then
continue
else
echo 'prod source directory ' "$i" 'does not exist in devsrc' >> $workdir/scriptlog
continue
fi
fi
if [ -f $devsrc/$i ]
then
cp /dev/null $workdir/findlog
find $prodsrc/$i -newer $devsrc/$i > $workdir/findlog
if [ -s $workdir/findlog ]
then
echo 'file ' "$prodsrc/$i" ' is newer than ' "$devsrc/$i" >> $workdir/scriptlog
rm $workdir/findlog
else
cp /dev/null $workdir/findlog
else
cp /dev/null $workdir/findlog
find $devsrc/$i -newer $prodsrc/$i > $workdir/findlog
if [ -s $workdir/findlog ]
then
echo 'file ' "$devsrc/$i" ' is newer than ' "$prodsrc/$i" >> $workdir/scriptlog
rm $workdir/findlog
fi
fi
cp /dev/null $workdir/findlog
diff $prodsrc/$i $devsrc/$i > $workdir/findlog
if [ -s $workdir/findlog ]
then
echo 'files ' "$prodsrc/$i" ' do not match ' "$devsrc/$i" >> $workdir/scriptlog
fi
else
echo 'prod source file ' "$i" 'does not exist in devsrc' >> $workdir/scriptlog
fi
done < $workdir/prodsrc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top