Sponsored Content
Top Forums Shell Programming and Scripting Replace null values with dot using awk Post 302979661 by RavinderSingh13 on Wednesday 17th of August 2016 12:43:02 PM
Old 08-17-2016
Hello cmccabe,

You haven't told us if you want to keep the same spaces(as shown in Input_file) into your post or not. Considering if you have only 2 fields and you want to maintain the spaces as it is(though I tried hard to keep the space same), following may help you in same.
Code:
awk -F"[[:space:]]+" 'function quick(Q){match(Q,/[[:space:]]+/);W=substr(Q,RSTART,RLENGTH);return W} NF==1{print $1 quick(Q) ".";Q=$0;next} NF==0{print "." quick(Q) ".";Q=$0;next} NF==2{if($0 ~ /^[[:space:]]+/){print "." quick($0) $2} else {print}}{Q=$0}'  Input_file

Output will be as follows.
Code:
name    test
sam      1
.      .
liz         2
al         .
.             1

If your requirements are different than the shown post, please post complete details and expected output too into your next post.
EDIT: Adding non-one liner form of solution now on same.
Code:
awk -F"[[:space:]]+" 'function quick(Q){
                                        match(Q,/[[:space:]]+/);
                                        W=substr(Q,RSTART,RLENGTH);
                                        return W
                                       }
                      NF==1            {
                                        print $1 quick(Q) ".";
                                        Q=$0;
                                        next
                                       }
                      NF==0            {
                                        print "." quick(Q) ".";
                                        Q=$0;
                                        next
                                       }
                      NF==2            {
                                        if($0 ~ /^[[:space:]]+/){
                                                                        print "." quick($0) $2
                                                                }
                                        else                    {
                                                                        print
                                                                }
                                       }
                                       {
                                        Q=$0
                                       }
                      '   Input_file

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-17-2016 at 02:24 PM.. Reason: Added a non-one liner form of solution successfully now.
This User Gave Thanks to RavinderSingh13 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find and replace a column that has '' to NULL in a comma delimited using awk or sed

Hi this is my first time posting ever. I'm relatively new in using AWK/SED, I've been trying many a solution. I'm trying to replace the 59th column in a file where if I encounter '' then I would like to replace it with the word NULL. example 0 , '' , '' , 0 , 195.538462 change it to 0... (5 Replies)
Discussion started by: gumal901
5 Replies

2. Shell Programming and Scripting

Awk script to replace null columns with blank

I have a file with contents "08011"||20080812 "23348"|20080827|20080924 "23387"|20080829|20080915 "23581"|20081003|20081028 "23748"|20081017|20090114 "24095"|20080919|20081013 "24105"|20070723|20070801 "24118"|20080806|20081013 "24165"|20080820|20080912 "24221"|20080908|20080929 i... (3 Replies)
Discussion started by: sonam273
3 Replies

3. Shell Programming and Scripting

How to replace quote symbol(") and dot(.) with some other values!!

Hi , I have below input file 1.order number is useful. 2.vendor_id is produced. 3.the vandor name is "malawar". I want output file like 1. order number is useful. 2. vendor_id is produced. 3. the vandor name is VmalawarV. in input file line number 1.order number there is no... (4 Replies)
Discussion started by: vinothsekark
4 Replies

4. Shell Programming and Scripting

Selective Replace awk column values

Hi, I have the following data: 2860377|"DATA1"|"DATA2"|"65343"|"DATA2"|"DATA4"|"11"|"DATA5"|"DATA6"|"65343"|"DATA7"|"0"|"8"|"1"|"NEGATIVE" 32340377|"DATA1"|"DATA2"|"65343"|"DATA2"|"DATA4"|"11"|"DATA5"|"DATA6"|"65343"|"DATA7"|"0"|"8"|"1"|"NEG-DID"... (3 Replies)
Discussion started by: sdohn
3 Replies

5. Shell Programming and Scripting

Replace a field where values are null in a file.

Hi, I've a pipe delimited file and wanted to replace the 3rd field to 099990 where the values are null. How can I achieve it using awk or sed. 20130516|00000061|02210|111554|03710|2|205069|SM APPL $80-100 RTL|S 20130516|00000061|02210|111554|03710|2|205069|SM APPL $80-100 RTL|S... (12 Replies)
Discussion started by: rudoraj
12 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

Replace null values in csv with zero

Hi, i have another issue: i have three files: FILE 1 ServiceEventHandler, Processed,Percentage 5285337,100% FILE 2 Wallet, Processed,Percentage 5285337,100% (1 Reply)
Discussion started by: reignangel2003
1 Replies

8. Shell Programming and Scripting

Multiple columns replace with null values.

I am trying to replace the partcular columns(Col3,col5,col20,col44,col55,co56,col59,col60,col61,col62,col74,col75,col88,col90,col91,col93,col94,col95) with empty Input file Col1,col2,col3,col4,col5------,col100 1,2,3,4,5,---------,100 3,4,5,6,7,---------,300 Output : ... (3 Replies)
Discussion started by: onesuri
3 Replies

9. Shell Programming and Scripting

Printing null values in awk

Hi, I have a csv file with given details abc.txt 123,ra,point,,there 232,ba,points,home,pheer I want to get those values and store them in different variables: Code: while read line do echo $line |awk -F"," '{print $1" "$2" "$3" "$4" "$5"}'|read dbt_acct val_dt crncy AMT... (11 Replies)
Discussion started by: rahulsk
11 Replies

10. 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
All times are GMT -4. The time now is 06:08 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy