Need help in Shell scripting

HI,

I have a file(file name:NGC_DATA_(date)_(header).txt)
where (date) is in YYYYMMDD format and (header) is a integer with length 3

The content of first line of this file is
HDR(date)(header)

(date)&(header) is same as above.

I need to print message if it matches "file's" (date) and (header) with first line of that file (date) and (header) otherwise print message unsuccessful.

Please help to write this script.

Regards
Akshu

Questions like this are much easier to understand if you provide
sample input file
desired output

#!/bin/bash

FNAMESTUB=NGC_DATA
DATESTR=20110310
HEADER=111

FNAME="${FNAMESTUB}_${DATESTR}_${HEADER}.txt"

if [[ -f $FNAME ]]
then
   FIRSTLINE=$(head -1 ${FNAME} )
   if [[ "$FIRSTLINE" == "HDR${DATESTR}&${HEADER}" ]]
   then
      echo "Success"
   else
      echo "Header mismatch"
   fi
else
   echo "File not found"
fi
1 Like
 
file_name=$1
ds=`echo "$file_name" | awk -F"[_.]" '{ print "HDR"$3$4}'`

if [ -f "$file_name" ] ; then
fds=`head -1 "${file_name}"`
if [ "${fds}" = "${ds}" ];then
echo "successful"
else
echo "un sucessfull"
fi
else
echo "file does not exist"
fi
 
./script_name.sh NGC_DATA_20110310_104.txt
1 Like

Thanks. This code working fine. I have tried cut command to this problem.