check for not null string in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting check for not null string in file
# 1  
Old 10-23-2006
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
# 2  
Old 10-23-2006
You can try something like this :
Code:
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.
# 3  
Old 10-23-2006
Quote:
Originally Posted by misenkiser
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 use the string length method.
Code:
#! /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.
# 4  
Old 10-23-2006
#!/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
justsam
# 5  
Old 10-23-2006
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
# 6  
Old 10-23-2006
Python alternative:

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

# 7  
Old 10-23-2006
Quote:
Originally Posted by misenkiser
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
Code:
#! /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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Check for not null column in a pipe delimited file

Hi, I have a requirement where I have to check whether the mandatory columns in a pipe delimited file is null and print error message. For eg, I have to check if the 3rd,5th,6th,7th and 8th column are null and print the message "<column name> is null". The data file will have aroung 100,000... (6 Replies)
Discussion started by: reshma15193
6 Replies

2. Shell Programming and Scripting

Check file for string existence before appending it with string

I want to append file with a string but before doing that i want to check if this string already exist in that file.I tried with grep on Solaris 10 but unsuccessful.Man pages from grep seems to suggest if the string is found command status will be 0 and if not 1.But i am not finding it.May be i... (2 Replies)
Discussion started by: sahil_shine
2 Replies

3. Shell Programming and Scripting

NULL Search String & File Name

Hi All, I am writting a Sell Script, that takes Search String & File Name from the terminal and check for Null Status. If either is NULL then pgm should quit. I wrote the following: bash-3.2$ cat null_status_of_parameters.sh #!/bin/sh #WASS that takes Search String & File Name from... (2 Replies)
Discussion started by: manishdivs
2 Replies

4. Shell Programming and Scripting

Check for null

Hi Champs!!! im a newbie in unix, need ur expert help for my problem... I need to search if there are any "NULL" entries in the string String without Null Str1: 203652|1000003653|tellt|RUPV|4649|1|07/28/2011 01:56:12 String with Null (RUPV is removed) Str2:... (5 Replies)
Discussion started by: guruprasad7
5 Replies

5. Shell Programming and Scripting

check for null

hi, i have 3 lines of output , if second line exists then only condition within the if loop has to exeute other wise it has exit from loop. i had tried like this but not getting please help me ... Code: if ; then echo "success" else echo "" Use code tags please,... (8 Replies)
Discussion started by: sreelu
8 Replies

6. Shell Programming and Scripting

Insert string 'NULL' where there is a null value

I have an input file having 7 fields delimited by , eg : 1,ABC,hg,1,2,34,3 2,hj,YU,2,3,4, 3,JU,kl,4,5,7, 4,JK,KJ,3,56,4,5 The seventh field here in some lines is empty, whereas the other lines there is a value. How do I insert string NULL at this location (7th loc) for these lines where... (8 Replies)
Discussion started by: zilch
8 Replies

7. Shell Programming and Scripting

How to check for null value from makefile

My makefile has some code as follows: if ; then \ echo copying tools; \ cp -f `../${TOOLS_ROOT_PATH}/ext_tools.sh 1` ${EXTERNAL_BIN_DIR} || exit $$?;\ fi; \ The ext_tools.sh is as follows: cat ttx.conf | grep external | grep -v ^# | awk '{print $1}' It can sometime result... (1 Reply)
Discussion started by: jake_ryan
1 Replies

8. Shell Programming and Scripting

How to check Null values in a file column by column if columns are Not NULLs

Hi All, I have a table with 10 columns. Some columns(2nd,4th,5th,7th,8th and 10th) are Not Null columns. I'll get a tab-delimited file and want to check col by col and generate seperate error code for each col eg:102 if 2nd col value is NULL and 104 if 4th col value is NULL so on... I am a... (7 Replies)
Discussion started by: Mandab
7 Replies

9. Shell Programming and Scripting

How to check for null or empty string

Hi, I need to check for value not equal (<>) to 21 and not equal empty or null values. Please modify this script if then echo "$VALUE,$BSC_NAME,$BSC_ID" > $OUT_FILE/power_up.out end if TQ (5 Replies)
Discussion started by: doer
5 Replies

10. Shell Programming and Scripting

check for NULL variable

Hello I want to check for NULL variable.. but this is not working..please help thanks in advance esham (2 Replies)
Discussion started by: esham
2 Replies
Login or Register to Ask a Question