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
# 8  
Old 12-16-2012
Code:
sed 's/\(^[^,]*\,.*\),,/\1,NA,/;s/\(^[^,]*\),,/\1,NA,/' infile

For any columns if empty, replace by NA

Code:
awk '{for (i=1;i<=NF-1;i++) if ($i=="") $i="NA"}1' FS=, OFS=,  infile

# 9  
Old 12-17-2012
Quote:
Originally Posted by rdcwayx

. . . For any columns if empty, replace by NA
Code:
awk '{for (i=1;i<=NF-1;i++) if ($i=="") $i="NA"}1' FS=, OFS=,  infile

The request was to replace ONLY $2 and/or $3. If you really want ro replace all empty fields, try
Code:
 awk '{gsub(",,",",NA,")}1' file

# 10  
Old 12-17-2012
Quote:
Originally Posted by RudiC
The request was to replace ONLY $2 and/or $3. If you really want ro replace all empty fields, try
Code:
 awk '{gsub(",,",",NA,")}1' file

In that case you would need to do that twice in this case, for example:
Code:
sed 's/,,/,NA,/; s/,,/,NA,/' file


--
There would be no need for that with perl:
Code:
perl -pe 's/,(?=,)/,NA/g' file


Last edited by Scrutinizer; 12-17-2012 at 03:34 AM..
# 11  
Old 12-17-2012
With Sed..
Code:
sed -e 's/^\([^,]*,\)\(,\)/\1NA\2/' -e 's/\([^,]*,[^,]*,\)\(,\)/\1NA\2/'  inputfile

Or a little shorter version..
Code:
sed -e 's/^\([^,]*,\),/\1NA,/' -e 's/\([^,]*,[^,]*,\),/\1NA,/' inputfile

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