Finding null column value using array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding null column value using array
# 1  
Old 06-09-2015
Finding null column value using array

hi,
Am trying to find a solution for finding a null column value inside a loop using array.


Code:
for eg:

two
three

five

From the above array myarray[0],myarray[3] and myarray[5] having null values. But when am trying to check that space using some condition its not working.
for (( i=0; i<"${#myarray[@]}"; i++ ))
do
space=""
if [[ "${myarray[$i]}" == "$space" ]]    
then
echo "space is there"
else
echo "no space"
fi
done

rogerben
# 2  
Old 06-09-2015
You are aware that there is a huge difference between a space and an empty string, aren't you?

What shell are you using?
What operating system are you using?
Show us how myarray is set.
Show us what output you get when you run your script?
# 3  
Old 06-09-2015
Code:
#!/bin/sh
myarray0=( `cat abc` )
space=""
for (( i=0; i<"${#myarray[@]}"; i++ ))
do
space=""
if [[ "${myarray[$i]}" == "$space" ]]
then
echo "space is there"
else
echo "no space"
fi
done

This is my code and below is the file content current shell sh, and Linux OS
cat abc

two
three

five

Last edited by rogerben; 06-09-2015 at 03:19 AM.. Reason: updating the input file content
rogerben
# 4  
Old 06-09-2015
Quote:
Originally Posted by rogerben
Code:
#!/bin/sh
myarray0=( `cat abc` )
space=""
for (( i=0; i<"${#myarray[@]}"; i++ ))
do
space=""
if [[ "${myarray[$i]}" == "$space" ]]
then
echo "space is there"
else
echo "no space"
fi
done

This is my code and below is the file content current shell sh, and Linux OS
cat abc

two
three

five
OK. There are two main problems here:
  1. You can't set myarray0 and use myarray in the rest of your code.
  2. And, setting an array using myarray=( `cat filename` ) throws away all of your empty lines.
After that, it isn't clear whether multiple words on a line in your input file should be treated as a single element in your array or as one element for each word. Maybe the following will show you what you did wrong and give you a way to get what you seem to be trying to do:
Code:
#!/bin/bash
myarray0=( $(cat abc) )
myarray=( )
myarray2=( )
while read line
do	myarray=( "${myarray[@]}" "$line" )
done < abc
unset line
while read -a line
do	if [ ${#line[@]} -eq 0 ]
	then	myarray2=( "${myarray2[@]}" "" )
	else	myarray2=( "${myarray2[@]}" "${line[@]}" )
	fi
done < abc
space=""
for (( i=0; i<${#myarray0[@]}; i++ ))
do	if [[ "${myarray0[$i]}" == "$space" ]]
	then	echo "myarray0: empty line"
	else	echo "myarray0: data present on line: ${myarray0[i]}"
	fi
done
echo
for (( i=0; i<${#myarray[@]}; i++ ))
do	if [[ "${myarray[$i]}" == "$space" ]]
	then	echo "myarray: empty line"
	else	echo "myarray: data present on line: ${myarray[i]}"
	fi
done
echo
for (( i=0; i<${#myarray2[@]}; i++ ))
do	if [[ "${myarray2[$i]}" == "$space" ]]
	then	echo "myarray2: empty line"
	else	echo "myarray2: data present on line: ${myarray2[i]}"
	fi
done

Note that although this script specifies bash, it also works unchanged with recent version of ksh93 (and with older versions of ksh93 if you change read -a to read -A).

With the input file abc containing:
Code:
one

three

five
six seven eight

it produces the output:
Code:
myarray0: data present on line: one
myarray0: data present on line: three
myarray0: data present on line: five
myarray0: data present on line: six
myarray0: data present on line: seven
myarray0: data present on line: eight

myarray: data present on line: one
myarray: empty line
myarray: data present on line: three
myarray: empty line
myarray: data present on line: five
myarray: data present on line: six seven eight

myarray2: data present on line: one
myarray2: empty line
myarray2: data present on line: three
myarray2: empty line
myarray2: data present on line: five
myarray2: data present on line: six
myarray2: data present on line: seven
myarray2: data present on line: eight

# 5  
Old 06-09-2015
Quote:
Originally Posted by rogerben
Code:
#!/bin/sh
.
.
.

This is my code and below is the file content current shell sh, and Linux OS . . .
I'd be surprised if you're running shas it doesn't provide arrays. Assuming bash, did you consider its mapfile (or readarray) command?
Code:
mapfile myarray <file
IFS= ""
echo "${myarray[*]}"
one

three

five
six seven eight

---------- Post updated at 11:08 ---------- Previous update was at 10:19 ----------

As for the testing for NULL values in your first post, did you consider the -z test?
Code:
for i in "${myarray[@]}"; do [ -z "$i" ] && echo empty || echo $i; done
one
empty
three
empty
five
six seven eight

Use mapfile's -t option (mapfile -t <file myarray) to remove the newline chars from the array.

Last edited by RudiC; 06-09-2015 at 06:47 AM..
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Divide the value of the column if not NULL.

File 1 --------- N_ACCT,CARD_TYPE,CARD_BAL,CUST_CODE --------------------------------------------------- 0301,38,0.00,10 0319,38,54422.92,10 0392,38,0.00,10 0418,38,35254.87,10 0442,38,0.00,10 0491,38,0.00,10 0558,38,45988.76,10 0616,38,0.00,10 0665,38,0.00,10 0699,38,0.00,10 File 2... (3 Replies)
Discussion started by: suresh_target
3 Replies

2. Linux

Issue in inserting null string in array

I am getting some values from a file and putting them in an array..but the null strings are not getting passed to the array. So during printing the elements ,the null string is not showing in the output. during array size calculation it is also excluding null.Please let me know how to do it. # cat... (2 Replies)
Discussion started by: millan
2 Replies

3. Shell Programming and Scripting

UNIX scripting for finding duplicates and null records in pk columns

Hi, I have a requirement.for eg: i have a text file with pipe symbol as delimiter(|) with 4 columns a,b,c,d. Here a and b are primary key columns.. i want to process that file to find the duplicates and null values are in primary key columns(a,b) . I want to write the unique records in which... (5 Replies)
Discussion started by: praveenraj.1991
5 Replies

4. Shell Programming and Scripting

How to find null column?

hi Gurus, I need find the null column in a file. my file like below abc, ,cde,def abc,ded,cdd,def abc, ,ddd,ccd aaa,bbb,ccc,ddd basic, I need to find the lines which second column is null Thanks in advance (3 Replies)
Discussion started by: ken6503
3 Replies

5. Shell Programming and Scripting

working with null elements in an array

i have an array (with each element length "n") which is dynamic and has alphanumeric characters. i want to check if any of the elements of the array are null and replace it with a string of "n" zeros for that element. can you suggest me a code for the same. (1 Reply)
Discussion started by: mumbaiguy07
1 Replies

6. Programming

Checking an array for NULL in C++

Hello All, I have this question that how to check for an array for NULL? For eg. #include<iostream.h> using namespace std; int main() { char arr; if(arr == NULL) { cout<<"NULL"; } } Thanks a lott in advance!!! (3 Replies)
Discussion started by: mind@work
3 Replies

7. Shell Programming and Scripting

Remove null in certain column

Hi, gurus, I need remove lines in a file which contains null value in certain column eg. 123, ,abc,def,cde 234,abc,cde,def 456,cde, ,bcd in this file I need remove lines which second column contains null. expected result: 234,abc,cde,def 456,cde, ,bcd :wall: Thanks in advance (2 Replies)
Discussion started by: ken002
2 Replies

8. Shell Programming and Scripting

how to find null column

Hi, everyone I have a requirement as following: source file 1, abc, def, caaa 2, , cde, aaa 3, bcd, , adefefg I need find columns which contains null value, in above example, I need get two rows 2, , cde, aaa 3, bcd, , adefefg anybody has idea how to achive this ... (5 Replies)
Discussion started by: ken002
5 Replies

9. 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

10. Shell Programming and Scripting

finding null records in data file

I am having a "|" delimited flat file and I have to pick up all the records with the 2nd field having null value. Please suggest. (3 Replies)
Discussion started by: dsravan
3 Replies
Login or Register to Ask a Question