check for not null string in file

Hi,

If, in a text file a string is expected at a certain fixed position(for eg at position 5 or from 5-10 on every line)
how to check whether data is present on that position or not?

Thnx in advance

You can try something like this :

awk '
     substr($0,5,6) ~ /^[[:space:]]*$/ {
        printf("File %s Line %d : Missing data", FILENAME, FNR);
        err += 1;
     }
     END {
        exit err;
     }
    ' input_file
if [ $? -ne 0 ]
then
   echo "Invalid file !"
fi

Jean-Pierre.

You can use the string length method.

#! /bin/ksh

while read line
do
  if [[ ${#line} -lt 5 ]] ; then
    echo "No data present"
    continue;
  fi;
  echo "perform your op on the $line"
done < input.txt

I assume that your data would be at the end of the line. If not, show us your input.

#!/bin/sh
i=1
cat file1 | while read line
do
str1=`echo $line | cut -c5-10 `
if [ -z "$str1" ]
then
echo "Line No. $i -- No String in position 5-10 "
else
echo "Line No. $i -- String in position 5-10 : $str1"
fi
i=`expr $i + 1`
done

Output:

$ ./test.sh
Line No. 1 -- No String in position 5-10
Line No. 2 -- String in position 5-10 : 12345a
Line No. 3 -- String in position 5-10 : 12345a
Line No. 4 -- No String in position 5-10
Line No. 5 -- No String in position 5-10
Line No. 6 -- String in position 5-10 : 12345a
Line No. 7 -- No String in position 5-10
Line No. 8 -- No String in position 5-10
Line No. 9 -- No String in position 5-10

$ cat file1
aaaa
absd12345asaa
defg12345asd
asas
asas
aeas12345asdd
asas

$
Note: Line 9 and 10 does not have any data in the above file

In all the responses it has been assumed that the position 5-10 will be the last portion in the file
however the line length may be upto 80 chars
whereas the search should be only for 5-10 position

Python alternative:

for linenumber, line in enumerate(open("inputfile.txt")):
 	if line[4:10] == '': 
               print "No data at line: %d" % linenumber    
#! /bin/ksh

typeset -L10 leftend
typeset -R5 rightend

while read line
do
  # Get the first 10 characters of the line
  leftend=$line
  # Get the last 5 characters of the above match
  rightend=$leftend

  if [[ ${#rightend} -gt 0 ]] ; then
  echo "We have data"
  else
  echo "Continue to next line"
  fi ;
done < input.txt

You could also try out bash. It has a builtin construct of the form ${line:x:y}. That will collect the string starting from x, to x+y position.