-1

I have a csv file with X amount of lines in the following format

P26439,341,P,L,,P26439,,-0.41

where everytime there is a double comma i.e. ,, I want to insert the word NA in between them so it would appear as ,NA,

would anyone have a sed, tr, perl or awk solution?

I know that I can replace a character using tr in the following way:

tr ",," ",NA,"

however this replaces any instance of a comma.

brucezepplin
  • 9,202
  • 26
  • 76
  • 129

5 Answers5

2

Try doing this with :

sed 's/,,/,NA,/g' file.csv

is for just one character at a time.

And if you want to edit the file in place, add the -i switch :

sed -i.bak 's/,,/,NA,/g' file.csv
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
2

If you have the possibility for sequential missing fields you will need to do:

$ cat file
P26439,341,P,L,,P26439,,-0.41
P26439,341,P,L,,,P26439,,-0.41
P26439,341,P,L,,,,P26439,,-0.41

$ sed ':a;s/,,/,NA,/;ta' file
P26439,341,P,L,NA,P26439,NA,-0.41
P26439,341,P,L,NA,NA,P26439,NA,-0.41
P26439,341,P,L,NA,NA,NA,P26439,NA,-0.41
Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
1

Perl solution:

$ echo "P26439,341,P,L,,P26439,,-0.41" | perl -pe 's/,,/,NA,/g'
P26439,341,P,L,NA,P26439,NA,-0.41
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
0
sed ':cycle
s/,,/,NA/g;t cycle
s/^,/NA,/;s/,$/,NA/' YourFile
  • set NA for any field, including first and last.
  • The cycle is needed for multiple following occurance ,,, where only the first ,, is treated at first cycle leaving the second (and any other multitple of 2 occurences) unchanged
NeronLeVelu
  • 9,908
  • 1
  • 23
  • 43
0

Through awk:

awk -F',' '{for (i=1;i<=NF;i++) if ($i=="") $i="NA" }1' OFS=','  file
  • Replace each empty fields in first,middle,last or if it was multiple occurrences with NA string.

Input:

,P26439,341,P,L,,P26439,,-0.41
P26439,341,P,L,,P26439,,-0.41
P26439,341,P,L,,P26439,,-0.41,
P26439,341,P,L,,,,P26439,,-0.41

Output:

NA,P26439,341,P,L,NA,P26439,NA,-0.41
P26439,341,P,L,NA,P26439,NA,-0.41
P26439,341,P,L,NA,P26439,NA,-0.41,NA
P26439,341,P,L,NA,NA,NA,P26439,NA,-0.41
αғsнιη
  • 2,627
  • 2
  • 25
  • 38