converting empty value into NA


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting converting empty value into NA
# 1  
Old 08-27-2012
converting empty value into NA

I have a set of values separated by a tab

Code:
ch:12     1     3     4
ch:13     3     3     4
ch:25     1            8
ch:23     2     8     1

There is a missing value in the third column and i would like replace it with
NA such that the final output would look like

Code:
ch:12     1     3     4
ch:13     3     3     4
ch:25     1     NA   8
ch:23      2     8    1

Thanks in advance
Moderator's Comments:
Mod Comment Please view this code tag video for how to use code tags when posting code and data.
# 2  
Old 08-27-2012
Code:
awk -F"\t" '{ for(N=1; N<=NF; N++) if($N == "") $N="NA" }' input > output

# 3  
Old 08-27-2012
Code:
perl -alnF"\t" -e 'for($i=0;$i<=$#F;$i++){ if(!$F[$i]){$F[$i]="NA";}} print "@F"' input_fiile

# 4  
Old 08-27-2012
Quote:
Originally Posted by msabhi
Code:
perl -alnF"\t" -e 'for($i=0;$i<=$#F;$i++){ if(!$F[$i]){$F[$i]="NA";}} print "@F"' input_fiile

It just gives you an empty file...
# 5  
Old 08-27-2012
I got the below

Code:
$ perl -alnF"\t" -e 'for($i=0;$i<=$#F;$i++){ if(!$F[$i]){$F[$i]="NA";}} print "@F"' file4
ch:12 1 3 4
ch:13 3 3 4
ch:25 1 NA 8
ch:23 2 8 1

Input :
Code:
ch:12^I1^I3^I4$
ch:13^I3^I3^I4$
ch:25^I1^I^I8$
ch:23^I2^I8^I1$

# 6  
Old 08-27-2012
can I ask you one more question?

ch:12 1 3 4
ch:13 3 3 4
ch:25 1 8
ch:23 2 8 1

let's say that 3rd column has 3 fields, but "8" is actually the fourth field, and third field is the value that is missing. then how would you make the 3rd field NA and 8 as the 4th field?
# 7  
Old 08-27-2012
Code:
 perl -alnF"\t" -e 'if($#F==2){$F[3]=$F[2];$F[2]="NA";print "@F"} else {print "@F"}'  input_file

Try this if its given that always third column value is missing and 4th column is third column.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need Help in converting

I have Excel file with the below three columns, i need your expertise in converting this to .csv file delimiter "|" Excel - Serial Number Serial Name Serial Brand 111 test sample 123 test2 sample1 134 ... (9 Replies)
Discussion started by: kiran_hp
9 Replies

2. SCO

library converting

Hi everybody Is there any sco unix command to convert .so library to .a (under sco unix openserver.5.0.6) tnx (2 Replies)
Discussion started by: javad1_maroofi
2 Replies

3. Shell Programming and Scripting

Need to empty

Hi, How can I empty the files of *.txt.... in one command. (6 Replies)
Discussion started by: gsiva
6 Replies

4. UNIX for Dummies Questions & Answers

Getting same exit status for empty and non empty file

Hi All, I am checking for a empty input file to do some further action , but I am getting exit status 0 in both the cases , for empty and non empty file both. The value of $? is coming 0 in if part also and else part too. #!/bin/ksh if ]; then echo "data" # exit 0 echo "$?" else... (4 Replies)
Discussion started by: mavesum
4 Replies

5. UNIX for Dummies Questions & Answers

how to find empty folders without using -empty

hi all: my solaris FIND does not support find myFolder -type d -empty how can i find the empty folders? thanks! (7 Replies)
Discussion started by: lasse
7 Replies

6. Shell Programming and Scripting

Converting \0 to a \n

Hi - I have seen some similar posts but I am a bit stumped here below is the first line of a 'od -c filename' command. I want to change the \0 to \n 0000000 l s \0 c d - \0 c d . . \0 l s I have tried a sed construct in a script......... sed... (2 Replies)
Discussion started by: ajcannon
2 Replies

7. SCO

Converting

I use Sco_Sv 3.2v5.0.5 with parellel conection using dump terminals and i want to convert them to desktop pc. Anybody knows what hardware and other thing that would be involved? (3 Replies)
Discussion started by: seeyou
3 Replies

8. Programming

need help on some converting command

Hi all, i am trying to use C in unix platform to convert a double variable (floating point) to a string variable. i tried using sprintf, ecvt, fcvt,gcvt. but all gave me funny output or altered the content. does anyone know how to convert the data and keep the original content. for... (3 Replies)
Discussion started by: szeliat
3 Replies

9. Shell Programming and Scripting

Converting to Uppercase

I want to convert string into uppercase string. How can i do that ? Ex: Enter the user name: read name show=upper(name) echo $show --- This output should be the uppercase output. Thanks (3 Replies)
Discussion started by: dreams5617
3 Replies

10. UNIX for Dummies Questions & Answers

converting kb to mb

When I create filesystems in AIX i often get confused(using smit) When you specify size in aix, it is asked like this SIZE of file system (in 512-byte blocks) I never seem to grasp this, what is the equation to get say 500mb? Or is there a program anyone knows of that does this, like a... (1 Reply)
Discussion started by: csaunders
1 Replies
Login or Register to Ask a Question