Printing null values in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing null values in awk
# 8  
Old 11-04-2016
As the OP is running ksh, no need to set bash specific options, the same loop works as is:

Code:
while read line
do
        echo $line | IFS="," read dbt_acct val_dt crncy AMT dbt_GOP
        print $AMT
done <abc.txt

# 9  
Old 11-04-2016
Hi Jlliagre,
I have not said otherwise, I just made a remark about the response RudiC about bash.

Regards.
# 10  
Old 11-04-2016
Adding some debug code to your original script
Code:
#!/bin/ksh
debugfile=debug.txt
while read line
do
  echo "$line" |
   awk -F"," '{print $1" "$2" "$3" "$4" "$5}' |
   tee -a $debugfile |
   read dbt_acct val_dt crncy AMT dbt_GOP
  print "$AMT"
done <abc.txt

Now look at the debug.txt file, and you should see the problem!
Code:
123 ra point  there
232 ba points home pheer

# 11  
Old 11-04-2016
Quote:
Originally Posted by rahulsk
....

Hi Don,

Thanks for the command.

Can you please help me in knowing what is the problem in the code what I have written?
Hi rahulsk,
In case it isn't obvious from what others have already said, your code didn't work because your awk script produces output (when there are one or more empty fields in the inpu) that your second read statement interprets to be part of a field separator instead of an empty field.

Furthermore, your use of echo (with an unquoted variable expansion) would convert any sequence of one or more <space> and <tab> characters to a single <space> character. (This doesn't matter with the supplied sample data, but if your input was something like:
Code:
123,ra,point	x y,,there
232,ba,points	a b	c d	e f,home,pheer

it could make a huge difference. And, with input like the above, the echo and awk pipeline converting <comma>s to <space>s would further exacerbate the problem changing <space> and <tab> characters in your input data into field separators and may add lots of words to the contents of the last data field.)

Using a single read statement with a <comma> as the field separator instead of a read statement, an echo statement, an awk command, and another read statement gets rid of two unnecessary writes of your data, gets rid of two unnecessary reads of your data, gets rid of the invocation of two unneeded shell built-in utilities, and gets rid of one unneeded external utility invocation as well as getting rid of several places in the pipeline where data can be corrupted.
# 12  
Old 11-04-2016
A little trick to work with awk|read:
Code:
$ while read line; do echo $line |awk -F"," '{gsub(/,,/,",\\\000,");print $1" "$2" "$3" "$4" "$5}'|read dbt_acct val_dt crncy AMT dbt_GOP; echo $AMT; done <abc.txt

home

But, it is not the best solution, just to show it is possible...

Regards.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Count null values in a file using awk

I have the following a.txt file A|1|2|3|4|5| A||2|3|0|| A|1|6||8|10| A|9|2|3|4|1| A|0|9|3|4|5| A||2|3|4|5| A|0|av|.9|4|9| I use the following command to count null values for 2nd field awk -F"|" '!$2 { N++; next } END {print N}' a.txt It should give the result 2, but it is giving... (2 Replies)
Discussion started by: RJG
2 Replies

2. Shell Programming and Scripting

Replace null values with dot using awk

Using awk I am trying to replace all blank or null values with a . in the tad delimited input. I hope the awk is close. Thank you :). input name test sam 1 liz 2 al 1 awk awk 'BEGIN{FS=OFS="\t"}{for(i=1;++i<NF;)$i=$i?$i:"."}1'input awk 'BEGIN { FS =... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Printing $values using awk

Hi All I had requirement where I need to re-order columns in a file by using a control file. here is the ctrl file c1 c2 c3 source file c3 | c1 | c2 a | b| c I should create output file based on the ctrl file columns o/p should look like this c1 | c2 | c3 b| c|a I wrote some... (9 Replies)
Discussion started by: gvkumar25
9 Replies

4. Shell Programming and Scripting

Awk: Comparing arguments with in line values of file and printing the result

I need to develop a script where I will take two date arguments as parameter date1 and date2 which will in format YYYYMM. Below is the input file say sample.txt. sample.txt will have certain blocks starting with P1. Each block will have a value 118,1:TIMESTAMP. I need to compare the... (7 Replies)
Discussion started by: garvit184
7 Replies

5. Programming

NULL printing a value 0xbf940000 and not a zero

hello all, in the following code a -> next should print zero, but it is giving the output as 0xbf940000 #include<iostream.h> #include<conio.h> #include<stdio.h> class Node { public: int data; Node *next; Node(int n=1) { data=10; next=0; } }; void main()... (3 Replies)
Discussion started by: zius_oram
3 Replies

6. Shell Programming and Scripting

Handle null values-awk

I am using below code to validate the source file,code working fine but if any column contains null value then below code throwing error actually it should not.how to customize the below code to handle null null values also. When I run the script with below source data getting “date error”, as... (2 Replies)
Discussion started by: srivalli
2 Replies

7. Shell Programming and Scripting

Perl: Printing null hash values as a " "?

I'm filling in a table of values for grades. I decided to go with reading into a hash from the files but I'm coming up with an error when printing a value that does not exist. I need to know if I can on-the-fly print a space (" ") or blank in place of the grade. Here's what the output should... (2 Replies)
Discussion started by: D2K
2 Replies

8. Shell Programming and Scripting

How to use sort with null values?

Hello everyone I am doing a join command. Obviously, before I need two files sorted first. ( Both files have headers and have about 2 million lines each one ) The problem is, one of the files has null values in the key to sort (which is the first filed ). For example I have the original... (4 Replies)
Discussion started by: viktor1985
4 Replies

9. Shell Programming and Scripting

sorting null values

Hi I have a file with the values abc res set kls lmn ops i want to sort this file with the null values at the bottom of the file OUTPUT should look like this abc kls lmn ops (6 Replies)
Discussion started by: vickyhere
6 Replies

10. UNIX for Advanced & Expert Users

How to Compare Null values??

Hi, Can someone help me comparing Null values. Scenario is as follows: I have a variable which "cache_prd" which can have either some integer or nothing(Null) if it is integer I have to again do some comparision but these comparisons give me this error:( "line 32: [: 95: unary operator... (3 Replies)
Discussion started by: Yagami
3 Replies
Login or Register to Ask a Question