The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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 07:05 AM
Single column to multiple columns in awk astroDave Shell Programming and Scripting 2 03-27-2008 10:00 PM
I need to extract last column of a file and compare the values vukkusila Shell Programming and Scripting 4 08-04-2007 11:21 AM
replace a column values with the first value in column sumeet UNIX for Advanced & Expert Users 3 02-06-2007 01:13 PM
Print last 4 columns (variable column #) Da_Duck UNIX for Dummies Questions & Answers 19 02-27-2004 10:33 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 04-19-2007
Mandab Mandab is offline
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
  #2 (permalink)  
Old 04-19-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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
  #3 (permalink)  
Old 05-02-2007
Mandab Mandab is offline
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$
  #4 (permalink)  
Old 05-02-2007
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
Mandab,

Try replacing the -F"|" with -F"<tab>" where <tab> is the tab character.
Also printf("/n") should be printf("\n")
  #5 (permalink)  
Old 05-02-2007
Mandab Mandab is offline
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
  #6 (permalink)  
Old 05-02-2007
kahuna's Avatar
kahuna kahuna is offline
Registered User
  
 

Join Date: Apr 2007
Posts: 149
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?
  #7 (permalink)  
Old 05-02-2007
Mandab Mandab is offline
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:06 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0