![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
|||
|
Quote:
Code:
awk -F"|" ' {
if ( $2 ~ /^ *$/ ) printf("102 ")
if ( $4 ~ /^ *$/ ) printf("104")
printf("\n")
} ' file
|
|
|||
|
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$ |
|
|||
|
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 |
|
|||
|
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 |
|||
| Google The UNIX and Linux Forums |