Operating System - HP-UX
1833437 Members
3376 Online
110052 Solutions
New Discussion

checking for the first line of the file

 
Pando
Regular Advisor

checking for the first line of the file

Dear Gurus,

I would like to ask how would i check the content of first line the file (grep?). Normally, the first line of the file would be

Test Center, XXXXX XXXX XXXX XXXX
...
...

However, some file have different pattern. So what i would like to do is check first the first line of the file if it contains the Test Center word and do nothing. If the first line does not contain the word Test Center, then it will be move to a temp directory.

Maximum point for all correct replies!
9 REPLIES 9
twang
Honored Contributor

Re: checking for the first line of the file

use head command to list the first line of the file:
# head -n 1 source_file
A. Clay Stephenson
Acclaimed Contributor

Re: checking for the first line of the file

There are several approaches but 2 that come to mind immediately are the shell built-in read command and the line command.
I think we'll use read

#!/usr/bin/sh

typeset MYFILE=""
typeset X=""
typeset -i STAT=0
typeset -i GREPSTAT=0

MYFILE=/xxx/yyy/myfile.txt
if [[ -r "${MYFILE}" ]]
then
read X < "${MYFILE}"
echo "${X}" | grep -q -i "Test Center"
GREPSTAT=${?}
if [[ ${GREPSTAT} -eq 0 ]]
then
echo "File should be moved"
else
echo "No match"
fi
else
echo "File ${MYFILE} not found" >&2
STAT=1
fi
exit ${STAT}

------------------

read when used like this will read 1 line from stdin. Note the use of quotes. Grep -q is used because we are only interested in the exit status (${?})of the command. 0 => match.
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: checking for the first line of the file

Here's a shell script that you could use:


#!/bin/sh

for i in $(ls -1 )
do
awk 'NR==1 && $0!~/^Test Center,/ {system("mv "FILENAME" /tmp")}' $i
done
Hein van den Heuvel
Honored Contributor

Re: checking for the first line of the file


perl -ne 'exit ( /^Test Center,/' file


now test status.

-n = loop through input
-e = perl script to follow
exit immediately on reading the first line with the status being the match condition.

hth,
Hein.
Peter Nikitka
Honored Contributor

Re: checking for the first line of the file

Hi,

to add a sed solution:

for file in *
do
first=$(sed 1q $file)
case "$first" in
TestCenter,*) ;;
*) mv $file tempdir ;;
esac
done

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Hein van den Heuvel
Honored Contributor

Re: checking for the first line of the file

Had a missing closing paren on the first example, and forgot about the rename request itself:

perl -ne 'close ARGV; rename $ARGV, "temp" unless /^Test Center,/' file

Hein.

Mark Fenton
Esteemed Contributor

Re: checking for the first line of the file

an alternate version with head and grep:

#!/bin/sh
cd /place_where_the_files_live
for file in `ls` ; do
mytest=`head -1 $file|grep "Test Center"`
if [ -z $mytest ]
then
mv $file /temp_directory
fi
done
Mark Fenton
Esteemed Contributor

Re: checking for the first line of the file

Forgot quotes around $mytest; corrected.

#!/bin/sh
cd /place_where_the_files_live
for file in `ls` ; do
mytest=`head -1 $file|grep "Test Center"`
if [ -z "$mytest" ]
then
mv $file /temp_directory
fi
done
Geoff Wild
Honored Contributor

Re: checking for the first line of the file

head -1 $MYFILE |grep "Test Center"
if [ $? -eq 0 ]
then
exit 99
else

put code here you want to do

fi
exit 0


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.