grep files without header and move to new dir

Hi, i have number of files in a directory to be processed. the problem is some of the files does not have a header and the process is giving an error of no header found.

example of good file :

file1
HDR|20080803233401
record 1
record 2
TRA|2

example of a bad file in the same dir

file2
record 1
record 2
TRA|2

i need to move this files without header to a different directory.

please help

Hope this works for you:

if [[ ! -d nohead ]]
then
        mkdir nohead
fi
for filen in $(ls file*)
do
        head=$(awk -F"|" 'NR==1 {print $1}' $filen)
        if [[ $head != "HDR" ]]
        then
                mv $filen nohead
        fi      
done

You can avoid the Useless Use of ls and avoid reading the rest of each file with awk; all the above script really does is examine the first line, and print the text before any "|" (or the whole line otherwise). Assuming that logic as such is good, the following script is slightly simpler.

for filen in file*
do
  if awk '/^HDR|/ { exit 0 } { exit 1}' "$filen"
  then
    mv "$filen" nohead
  fi
done

thanks guys... problem solved...

I have seen many scripts where exit status is set to '0' at the last line of the program. Is there any significance to it
Say this program
echo "Hello $USER"
echo "Today is \c ";date
echo "Number of user login : \c" ; who | wc -l
echo "Calendar"
cal
exit 0

what my doubt is,
setting the exit status to some non-zero value makes sense.
Whats the use in setting it to 0, if the script is run without any error message it'll automatically set to 0 right ?

This is the 'appropriate way' of writing a script.
(a) there is a clear end to the program
(b) it returns a value of zero

The second part probably requires more explanation.

# main program
blah-blah commands

# calls to another script
chk_headers
if [ "$?" -ne 0 ]
   then
   echo "Error in processing chk_headers"
   exit
fi
# calls to another script
blah-blah more stuff

exit

My main program calls and executes the 'chk_headers' script. When 'chk_headers' completes, it may return a non-zero condition. My main program checks for this. The standard is to return 0 (zero) unless an error.

Sometimes, clever programmers use this passed variable to have a sub-program (child) give some value back to the calling (parent) program.

One of the main purposes of the exit code of a program is that this is what the shell's conditionals like while and if examine. So the exit code should reflect whether or not there was success (0 means yes, any other number means no).

And as usual, the proper idiomatic way to write the above conditional is to use the command directlly in the if, and avoid the Useless Use of Test, like this:

if chk_headers; then
    : nothing
else
    exitcode=$?
    echo $0: Error $exitcode in chk_headers >&2
    exit $exitcode
fi

exit 0

Notice the redirection of the error message to standard error, and the propagation of the exit code from chk_headers to any calling script. Also the error message indicates which program printed it, for somewhat improved usability.