Replace with NA if blank is there


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace with NA if blank is there
# 1  
Old 12-16-2012
Replace with NA if blank is there

Hi All,

please help me below request.



i am having a file XXXX.dat. in Unix.

example-
Code:
plan_dk,imsmodelvar,paymt_type_az,mmars_plan,mmars_payer,mmars_pbm,account,botpbm,formulary_Crestor
0000000000,,3,MAIL ORDER,MAIL ORDER DATA,N/A,MAIL ORDER,N/A,
0000000000,6,,MAIL ORDER,MAIL ORDER DATA,N/A,MAIL ORDER,N/A,
0000000000,,,MAIL ORDER,MAIL ORDER DATA,N/A,MAIL ORDER,N/A,


in above lines 1st line is header (column names) and below are the values.
my requirement is --> if only 2nd and 3rd columns value are Null then it should replace with NA.Another columns value should not be changed keep as it is.
how to do in unix either command or a shell script.


Result should look like below mentioned.
Code:
0000000000,NA,3,MAIL ORDER,MAIL ORDER DATA,N/A,MAIL ORDER,N/A,
0000000000,6,NA,MAIL ORDER,MAIL ORDER DATA,N/A,MAIL ORDER,N/A,
0000000000,NA,NA,MAIL ORDER,MAIL ORDER DATA,N/A,MAIL ORDER,N/A,

Advance Thanks...

Thanks,
Krupa

Last edited by Scott; 12-16-2012 at 07:59 AM.. Reason: Please use code tags and less formatting
# 2  
Old 12-16-2012
Try:
Code:
awk -F, -vOFS=, '{$2=$2!=""?$2:"NA";$3=$3!=""?$3:"NA"}1' file.dat

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 12-16-2012
A solution 4 characters shorter than bartus11's:
Code:
awk -F, -vOFS=, '{$2=$2""?$2:"NA";$3=$3""?$3:"NA"}1' file.dat

This might not work with some old awk implementations.
This User Gave Thanks to elixir_sinari For This Post:
# 4  
Old 12-16-2012
Thanks

it's working now
# 5  
Old 12-16-2012
One char shorter than elixir_sinari's:
Code:
awk -F, -vOFS=, '!($2""){$2="NA"}!($3""){$3="NA"}1' file

or, even 2 chars less
Code:
awk -F, -vOFS=, '$2==""{$2="NA"}$3==""{$3="NA"}1' file

This User Gave Thanks to RudiC For This Post:
# 6  
Old 12-16-2012
How about this one?
Code:
awk '!$2{$2="n/a"}!$3{$3="n/a"}1' FS=, OFS=, file

Did I win the contest of who has the shortest? ^_^
This User Gave Thanks to ripat For This Post:
# 7  
Old 12-16-2012
Quote:
Originally Posted by ripat
How about this one?
Code:
awk '!$2{$2="n/a"}!$3{$3="n/a"}1' FS=, OFS=, file

Did I win the contest of who has the shortest? ^_^
A 0 (zero) shouldn't replaced by n/a IMHO.
This User Gave Thanks to Franklin52 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

In a file, replace blank line by the last line not blank above

Dear All, In a CSV file, say that a given column has been extracted. In that column, information is missing (i.e. blank lines appear). I would like to replace the blank lines by the last valid line (not blank) previously read. For example, consider the extract below: 123 234 543 111... (7 Replies)
Discussion started by: bagvian
7 Replies

2. Shell Programming and Scripting

How to replace blank tab with zero in a file?

hi, i need to replace a blank tab output in a file to zero. input file: 2015/08/04 00:00:00 171 730579 27088 <blank> 3823 30273 1621778 ... (6 Replies)
Discussion started by: amyt1234
6 Replies

3. UNIX for Dummies Questions & Answers

Replace character by blank

Hi all, I have 89 columns,1200 rows in a flat file, some of the values are just '.' (the character dot). I want to replace them by nothing (blank), but when I do so, it affects the decimal numbers too. so 12.34 becomes 1234. How can I just replace values which are only '.' with 1 white... (13 Replies)
Discussion started by: newbie83
13 Replies

4. Shell Programming and Scripting

Need help to replace a pattern with a blank line

Need help to replace the line beginning with tcp_sendspace with a blank line. # cat if en0: flags=1e080863,480<UP,BROADCAST,NOTRAILERS,RUNNING,SIMPLEX,MULTICAST,GROUPRT,64BIT,CHECKSUM_OFFLOAD(ACTIVE),CHAIN> inet 10.27.53.21 netmask 0xffffff00 broadcast 10.207.52.255 inet... (11 Replies)
Discussion started by: sags007_99
11 Replies

5. Shell Programming and Scripting

replace blank line number

hlow all i need help how can i replace blank number with awk input.txt 300::|355264313178490 301::|358814003239510 302::|358316038113400 303::|357954002633660 304::|354072040694090 305::|356956015214190 306::|352943020525180 307::|359574033836610 308::|381810990023580 so will be like... (4 Replies)
Discussion started by: zvtral
4 Replies

6. Shell Programming and Scripting

Find and replace blank in the last field

Hi all, I have a huge file and I need to get ride of the fields 6-11 and replace the blanks in field 5 with a missing value(99999). 159,93848,5354,343,67898,45,677,5443,434,5545,45 677,45545,3522,244, 554,54344,3342,456, 344,43443,2344,444,23477... (12 Replies)
Discussion started by: GoldenFire
12 Replies

7. Shell Programming and Scripting

How to replace the blank in unix

Hi, I have data like below: The columns MARKET ,STORE, WEEK1 will be present always. After that, there can be week2, week3 columns or some times there wont be no columns after week1. The week1 or week2 columns can have data or blank. I need to replace the blank with NIS. MARKET,STORE,STORE... (6 Replies)
Discussion started by: bharathappriyan
6 Replies

8. Shell Programming and Scripting

how to replace a character with blank in a file

hi, I have a doubt in replacing characters with blank. My requirement is that, i have one file and looks like below 4:ALTER SYSTEM DISCONNECT SESSION '193,191' IMMEDIATE; 6:ALTER SYSTEM DISCONNECT SESSION '205,7274' IMMEDIATE; 5:ALTER SYSTEM DISCONNECT SESSION '206,34158' IMMEDIATE;... (4 Replies)
Discussion started by: sridhusha
4 Replies

9. Shell Programming and Scripting

Replace exact word with blank

i have a file with the below content file1.txt ALERTADMIN.FIELD ALERTADMIN.TX_ALERTS_LOG i have another file file2 ALERTADMIN.FIELD ALERTADMIN.FIELD_WS ALERTADMIN.SECTION_FIELD_WS ALERTADMIN.TX_ACCT_PROCESSING_WORK_TABLE ALERTADMIN.TX_ACCT_REVIEW_EXEC_METRICS... (2 Replies)
Discussion started by: lavnayas
2 Replies

10. Shell Programming and Scripting

Replace two blank line with a single blank line

Hi Guys, I have a file in which each set of records are separated by two blank line. I want to replace it with a single blank line. Can you guys help me out? Regards, Magesh (9 Replies)
Discussion started by: mac4rfree
9 Replies
Login or Register to Ask a Question