Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Here is a example file where I need to replace blanks with zero

A   2   2   2   2   2
B   2   2   2   2   2
C   2   2   2   2   2
D   A               
E   B               
F
D
R                   
share|improve this question
2  
In the original example where no spaces, those were added as I formatted it. Original example had only tabs as blanks. –  Anthon Dec 21 '13 at 14:01
4  
Please clarify the question by posting what your desired output should look like. –  200_success Dec 22 '13 at 8:52

3 Answers 3

The following should work:

tr '\t' 0 < file 1<> file
share|improve this answer
    
These commands are not working unfortunately and giving me out put file without any change. My file contain 150 rows in total with 96 rows having 4783484 columns and remaining have only 6 columns. So I need to add zero 4783478 times in these rows. –  user55103 Dec 21 '13 at 13:43
    
@user55103 What is the exact command you are executing, including the filename? Do you receive any errors? –  Chris Down Dec 21 '13 at 13:45
    
tr ' ' 0 < ped3.ped< ped4.ped . I donot receive any error message but it returnd me ped4.ped file without any change (no zero added) –  user55103 Dec 21 '13 at 13:57
    
@user55103 Your syntax is wrong. Read the answer again, replace file with the filename. –  Chris Down Dec 21 '13 at 14:09
1  
Thanks for you help but it is not working, I am afraid. May be because my file is tab delimited. –  user55103 Dec 21 '13 at 15:05

Filter it through

perl -pe 's/\t(?=\t|$)/\t0/g'

In Perl regular expressions,

  • \t is a Tab
  • (?=…) is a zero-width look-ahead assertion, which I've used to match only empty fields.

The Perl command line options

  • -e specifies that the next command-line argument contains the Perl program
  • -p inserts boilerplate code to make it act like awk: executing the Perl code for each line in the input and print the processed line.
share|improve this answer

Filter it through

awk '{ while (gsub("\t\t", "\t0\t"));
       sub("\t$", "\t0");
       print }'

The solution is awkward because in general, neither awk nor sed supports zero-width look-ahead assertions. Therefore, I have to look for two consecutive tabs. As a result, I also have to handle tab at the end of the line separately.

share|improve this answer
1  
I think you can use "\t" also in gsub and sub. No need for C-v <tab> –  Håkon Hægland Dec 22 '13 at 9:10
    
@HåkonHægland Amended, thanks! –  200_success Dec 22 '13 at 9:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.