Search code examples
androidkotlin

remove english characters and keep only foreign unicode characters


I have this regex grep command that is working as expected. It separates all english characters and punctuation from foreign characters.

head -300  tb.txt | grep  -h '[A-Za-z0-9[:punct:]]'

But I am not sure how to remove those characters and keep only foreign characters in a text file.

For e.g. if I have this text:

  "@context": "https://schema.org",
  "@type": "WebPage",
  "name": "गृहनिर्माण सोसायटीच्या माजी सचिवांना तीन लाखांचा दंड; उच्च न्यायालयाचा निर्णय, वाचा सविस्तर",
  "description": "<p> नगर सहकारी गृहनिर्माण संस्थेचे माजी सचिव चिंतामण पांडे यांना सहकारी संस्थेच्या डी/ई-प्रभागाच्या उपनिबंधकाने ३ मार्च २०२२ रोजी याचिकाकर्त्याला पाच वर्षांसाठी गृहनिर्माण संस्थेची निवडणूक लढवण्यास पात्र ठरवले. गृहनिर्माण संस्थेचे सचिव म्हणून वार्षिक सर्वसाधारण सभा आयोजित न केल्याबद्दल याचिकाकर्त्याला दोषी ठरवून ठरवून उपरोक्त कारवाई केली होती.</p>
",
  "image":"https://s.navarashtra.com/wp-content/uploads/2020/06/cropped-navarashtra-logo.png",

The expected is this:

गृहनिर्माण सोसायटीच्या माजी सचिवांना तीन लाखांचा दंड; उच्च न्यायालयाचा निर्णय, वाचा सविस्तर

नगर सहकारी गृहनिर्माण संस्थेचे माजी सचिव चिंतामण पांडे यांना सहकारी संस्थेच्या डी/ई-प्रभागाच्या उपनिबंधकाने ३ मार्च २०२२ रोजी याचिकाकर्त्याला पाच वर्षांसाठी गृहनिर्माण संस्थेची निवडणूक लढवण्यास पात्र ठरवले. गृहनिर्माण संस्थेचे सचिव म्हणून वार्षिक सर्वसाधारण सभा आयोजित न केल्याबद्दल याचिकाकर्त्याला दोषी ठरवून ठरवून उपरोक्त कारवाई केली होती.

Update:

This highlight the expected text. But how do I return only the highlighted/ selected text?

grep "$(printf '[\u0900-\u097F]')" tb.txt

Update 2:

I am looking for a way to re-write the above statement in Kotlin.


Solution

  • Pipe it through sed to remove all ascii except the dot:

    | sed 's,[!--/-~],,g'
    

    In situ:

    head -300  tb.txt | grep  -h '[A-Za-z0-9[:punct:]]' | sed 's,[!--/-~],,g'
    

    This character class is two ranges; one from ! (the first printable ascii character) to - (the character before the dot) and a second range from / (the character after the dot) to ~ (the last ascii char).