Printing null values in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Printing null values in awk
# 1  
Old 11-04-2016
Printing null values in awk

Hi,

I have a csv file with given details
abc.txt
Code:
123,ra,point,,there
232,ba,points,home,pheer

I want to get those values and store them in different variables:

Code:
Code:
while read line
do
echo $line |awk -F"," '{print $1" "$2" "$3" "$4" "$5"}'|read dbt_acct val_dt crncy AMT dbt_GOP
print $AMT
done <abc.txt

Required Output:
Code:
home

But the output I am getting is:
Code:
there
home

# 2  
Old 11-04-2016
Try:
Code:
while IFS=, read -r dbt_acct val_dt crncy AMT dbt_GOP
do      
        print "$AMT"
done <abc.txt

This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 11-04-2016
This works because the fourth field of the first line is empty; that loop prints the empty string "" immediatedly followed by "home". With any string in he first line's fourth field it would print
"any stringhome"
If you really want to print the second line's fourth field ONLY, try
Code:
{ IFS=, read; IFS=, read -r X X X AMT X; printf "%s\n" "$AMT"; } < abc.txt
home

This User Gave Thanks to RudiC For This Post:
# 4  
Old 11-04-2016
Quote:
Originally Posted by RudiC
This works because the fourth field of the first line is empty; that loop prints the empty string "" immediatedly followed by "home". With any string in he first line's fourth field it would print
"any stringhome"
If you really want to print the second line's fourth field ONLY, try
Code:
{ IFS=, read; IFS=, read -r X X X AMT X; printf "%s\n" "$AMT"; } < abc.txt
home

Hi RudiC,

Thanks for the details..
Can you please help me in knowing what is the problem in the code what I have written?

---------- Post updated at 05:03 PM ---------- Previous update was at 04:33 PM ----------

Quote:
Originally Posted by Don Cragun
Try:
Code:
while IFS=, read -r dbt_acct val_dt crncy AMT dbt_GOP
do      
        print "$AMT"
done <abc.txt


Hi Don,

Thanks for the command.

Can you please help me in knowing what is the problem in the code what I have written?

Last edited by rahulsk; 11-04-2016 at 08:31 AM..
# 5  
Old 11-04-2016
in awk, you could try

Code:
awk -F, '$4 != "" {print $4}' file

awk reads line based so no need to go for while loop and awk.
Just use while as Don & RudiC shared or just awk as mentioned above.

Quote:
Can you please help me in knowing what is the problem in the code what I have written?
I dont think your command output instead it should error due to following:
Code:
echo $line |awk -F"," '{print "$1" "$2" "$3" "$4" "$5"}'|read dbt_acct val_dt crncy AMT dbt_GOP

Code:
print $AMT

# 6  
Old 11-04-2016
I assume you are using ksh, because in other shells (e.g. bash), piped commands are executed in subshells whose variables are not conveyed back to the parent, so $AMT would not be defined. On top, ksh's print builtin is not generally known in other shells.

You code has two problems:
- the double quote after $5 is unpaired, i.e. it opens but doesn't close a string
- when printing an empty variable between default OFS (space), awk outputs two spaces that are interpreted as just one field delimiter by the followingread, thus advancing "there" into fourth field.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 11-04-2016
Hi,
In bash with pipeline work under many conditions, just a silly example to present bash pipeline:
In general, not work by default:
Code:
$ while read line; do echo $line | IFS="," read dbt_acct val_dt crncy AMT dbt_GOP ; echo $AMT; done <abc.txt 


$

man bash says:
Quote:
lastpipe
If set, and job control is not active, the shell runs the last command of a pipeline not executed in the background in the current
shell environment.
So, set lastpipe option:
Code:
$ shopt -s lastpipe

disable job control:
Code:
$ set +m

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

home

Regards.
These 2 Users Gave Thanks to disedorgue 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

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