The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 03:05 AM
Single column to multiple columns in awk astroDave Shell Programming and Scripting 2 03-27-2008 06:00 PM
I need to extract last column of a file and compare the values vukkusila Shell Programming and Scripting 4 08-04-2007 08:21 AM
replace a column values with the first value in column sumeet UNIX for Advanced & Expert Users 3 02-06-2007 09:13 AM
Print last 4 columns (variable column #) Da_Duck UNIX for Dummies Questions & Answers 19 02-27-2004 06:33 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-19-2007
Registered User
 

Join Date: Apr 2007
Posts: 22
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 newbie to Unix and any help will be appreciated.


Regards,
Mandab
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 04-19-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Quote:
Originally Posted by Mandab
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 newbie to Unix and any help will be appreciated.


Regards,
Mandab
Code:
awk -F"|" ' { 
       if ( $2 ~ /^ *$/ ) printf("102 ")
       if ( $4 ~ /^ *$/ ) printf("104")
       printf("\n")
} ' file
Reply With Quote
  #3 (permalink)  
Old 05-02-2007
Registered User
 

Join Date: Apr 2007
Posts: 22
I tried with the sample data but it is not giving the exact output:
The sample data is :
545689512<tab>20070424<tab>20070414<tab>456.25<tab>20061121<tab>pqr
<tab>20060726<tab>20060524<tab>800.12<tab><tab>abc
24<tab><tab>05242006<tab>22.15<tab>20050815<tab>xyz
57<tab>20040425<tab>20041214<tab><tab>20040628<tab>stv

Data will be from 3rd row in the file, the following is the script I tried:

#!/bin/ksh

awk -F"|" 'NR>=3 { ## data is from 3rd row onwards
if ( $2 ~ /^ *$/ ) printf("102")
if ( $4 ~ /^ *$/ ) printf("104")
printf("/n")
}' $1 ## filename

The out put I got is:
$ test6.ksh test1.txt
102104/n102104/n102104/n102104/n$
Reply With Quote
  #4 (permalink)  
Old 05-02-2007
kahuna's Avatar
Registered User
 

Join Date: Apr 2007
Posts: 148
Mandab,

Try replacing the -F"|" with -F"<tab>" where <tab> is the tab character.
Also printf("/n") should be printf("\n")
Reply With Quote
  #5 (permalink)  
Old 05-02-2007
Registered User
 

Join Date: Apr 2007
Posts: 22
Thank you for your quick response, I tried replacing as you said but still I am getting errors. The script is :
#!/bin/ksh

awk -F"<tab>" 'NR>=3 {
if ( $2 ~ /^ *$/ ) printf("102")
if ( $4 ~ /^ *$/ ) printf("104)
printf("\n")
}' $1

Errors I am getting:
$ test7 test1.txt
awk: newline in string near line 3
awk: syntax error near line 4
awk: illegal statement near line 4
Reply With Quote
  #6 (permalink)  
Old 05-02-2007
kahuna's Avatar
Registered User
 

Join Date: Apr 2007
Posts: 148
Quote:
Originally Posted by Mandab
awk -F"<tab>" 'NR>=3 {
I had intended that <tab> be replaced literally by the tab character - CTNL(H).

Quote:
Originally Posted by Mandab
if ( $4 ~ /^ *$/ ) printf("104)
You are missing a close " after 104.

I am confused. Is your file delimited by the tab character or by set of characters "<tab>" shown in your post?
Reply With Quote
  #7 (permalink)  
Old 05-02-2007
Registered User
 

Join Date: Apr 2007
Posts: 22
Excellent !! its working fine now.
But there is one small problem, If there is an error in field one and also field two then I should have only error to be printed as "102". but not both "102" and "104" to be printed, basing on the serial order of fields like field1, field2. Also can I use the error value in a variable so that I can use it for different purpose in my script(outside awk)?

Here is the script:
#!/bin/ksh

awk -F"," 'NR>=3 {
if (length($2)!= 8 || $2 ~ /[^0-9]/ || $2 ~ /^ *$/ ) {var1="102"}
if ( $4 ~ /^ *$/ ) {var1="104"}
{printf var1}
printf("\n")
}' $1


The output:


102
104
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0