0

I am looking for a command that would replace the rows containing ONLY number 0 in their columns, with 'blank' lines.

e.g. I have below input:

Taj_Mahal                                             Agra                       
                                                        0                             
                                                        0               
Chichen_Itza                                          Yucatán              
                                                        0                             
                                                        0               
Christ_the_Redeemer                                   Rio_de_Janeiro              
                                                        0                             
                                                        0               
Colosseum                                             Rome                 
                                                        0                             
                                                        0               
Great_Wall_of_China                                   China                      
                                                        0                             
                                                        0               
Machu_Picchu                                          Cuzco_Region                     
                                                        0                             
                                                        0               
Petra                                                 Ma'an_Governorate                   
                                                        0                             
                                                        0               
Great_Pyramid_of_Giza                                 Egypt                   
                                                        0                             
                                                        0               

Below should be the output:

Taj_Mahal                                             Agra                       


Chichen_Itza                                          Yucatán              


Christ_the_Redeemer                                   Rio_de_Janeiro              


Colosseum                                             Rome                 


Great_Wall_of_China                                   China                      


Machu_Picchu                                          Cuzco_Region                     


Petra                                                 Ma'an_Governorate                   


Great_Pyramid_of_Giza                                 Egypt                   
3

After editing your post, it appears that the lines in your sample input do not contain "ONLY" the zero. They contain many spaces and a zero. If this is the case, with awk:

awk 'NF==1 && $1=="0" { $0="" } { print }'

$0 refers to the entire line. NF refers to the number of fields. $1 refers to the first field. By default, awk strips leading/trailing blanks.

Alternatively, using regular expression matching:

 awk '/^ *0 *$/ { $0="" } { print }'

The solution could be made more succinct (and cryptic) by replacing { print } with 1, but, even so, sed would still be more concise:

sed 's/^ *0 *$//'

Note that the first awk suggestion ignores leading and trailing blanks (which may include tabs and other locale-dependent characters). The second awk and the sed approaches only ignore leading/trailing spaces.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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