Thursday, May 11, 2017

How to use “awk” in Linux–part 2 [Regular Expression]


අද මම කියලා දෙන්නේ “AWK" programming සමග Regular Expression යොදාගෙන Text files process කරන විදිය
What is Regular Expression

Regular Expression යනු සරලව කියනවනම් several sequence,combination වලින් ඇති characters වලින් represent කරන  String එකක්, එම sequence or combination පහත ආකාරයට නිර්මාණය වේ.
  • Ordinary characters (සාමාන්‍ය භාවිතයේ යෙදෙන)
    • space , underscore(_)
    • A-Z or a-z (අක්ෂර)
    • 0-9 (ඉලක්කම්)
  • Meta characters (විශේෂ භාවිතයන්)
    • (.) it matches any single character except a newline.
    • (*) it matches zero or more existences of the immediate character preceding it.
    • [ character(s) ] it matches any one of the characters specified in character(s), one can also use a hyphen (-) to mean a range of characters such as [a-f], [1-5], and so on.
    • ^ it matches the beginning of a line in a file.
    • $ matches the end of line in a file.
    • \ it is an escape character.

Regular Expression වල Ordinary characters භාවිතා කරන අකාරය ගැන අපි Post 1 දී examples සමග සාකච්චා කලා
අපි අද බලමු කොහොමද Regular Expression වල Meta  characters ,”AWK" සමග යොදාගන්නා අකාරය.
මේසදහා මම Post1 දී භාවිතා කල sample text file එකම යොදාගන්නවා


“AWK” සමග (.) wild card pattern එක භාවිතය

උදාහරණයක් විදියට මට අවශ්‍යයි ‘v’ හා ‘n’ අකුරු ඇති නමුත් එම අකුරු 2ක අතර තවත් unknown character එකක් ඇති row print කිරමට





“AWK” සමග (*) wild card pattern එක භාවිතය

උදාහරණයක් විදියට මට අවශ්‍යයි ‘i’ හා/හෝ  ‘l’ අකුරු ඇති හා unknown character ඇති row print කිරමට (Longest matching possible it can detect)




“AWK” සමග [character(s)] wild card pattern එක භාවිතය

උදාහරණයක් විදියට මට අවශ්‍යයි “s” හෝ  “d" හෝ “8" ඇති rows print කිරමට



උදාහරණයක් විදියට මට අවශ්‍යයි “m” අකුරට පසුව “al” කියන අකුරු ඇති rows print කිරමට



    මෙම ආකාරයටම පහත combinations ද භාවිතා කල හැකිය
  • [0-9] means a single number (එක ඉලක්කමක් හෝ සහිත lines print කිරම)
    • awk ‘/[0-9]/{print}’ sample.txt
  • [a-z] means match a single lower case letter (simple “a" to “z” characters එකක් හෝ සහිත lines print කිරම)
    • awk ‘/[a-z]/{print}’ sample.txt
  • [A-Z] means match a single upper case letter (Capital “A" to “Z” characters එකක් හෝ සහිත lines print කිරම)
    • awk ‘/[A-Z]/{print}’ sample.txt
  • [a-zA-Z] means match a single letter (simple හෝ capital “a" to “z” characters එකක් හෝ සහිත lines print කිරම)
    • awk ‘/[a-zA-Z]/{print}’ sample.txt
  • [a-zA-Z 0-9] means match a single letter or number (simple හෝ capital “a" to “z” characters එකක් සමහ ඉලක්කම් එකක් හෝ සහිත lines print කිරම)
    • awk ‘/[a-zA-Z 0-9]/{print}’ sample.txt


“AWK” සමග (^) wild card pattern එක භාවිතය

උදාහරණයක් විදියට මට අවශ්‍යයි “A” අකුරින් ආරමිභ වන lines print කිරමට





“AWK” සමග ($) wild card pattern එක භාවිතය

උදාහරණයක් විදියට මට අවශ්‍යයි “ts” අකුරින් අවසන් වන lines print කිරමට




“AWK” සමග (\) escape character wild card pattern එක භාවිතය



ඉහත උදාහරණ අකාරයට , මට අවශ්‍යනම් “$39.99" කියන අගය සහිත line එක print කරන්න සමාන්‍ය භාවිතා කරන command syntax එක වනුයේ
awk ‘/$39.99/{print}’ samp.txt
නමුත් මෙහි “$” යනු අවසන් යන්න නිරුපනය වන නිසා ඒ වෙනුවට අපට Escape characters   එකක් භාවිතා කල යුතුය.



මේ ආකාරයට පහත combination භවිතා කල හැකිය
  • Match Specific characters only
    • echo -e "Doll\nFall\nCall\nWall\nTall\nCat\nTell" | awk '/[CT]all/'
  • Exclude Specific characters
    • echo -e "Doll\nFall\nCall\nWall\nTall\nCat\nTell" | awk '/[^CT]all/'
  • Specific words with “OR”
    • echo -e "Doll\nFall\nCall\nWall\nTall\nCat\nTell" | awk '/Call|Cat/'
  • Zero or more occurrence
    • echo -e "Fell\nFall" | awk '/F?ll/'
  • One or more occurrence
    • echo -e "000\n2\n22\n122\n111\n246\n67\n222"  | awk '/1+/'
  • Grouping word or words
    • echo -e "Sea wing\nSea saw\nSea view" | awk '/Sea (wing|saw)/'


SITTHAMA

Sinhala Support


Followers

Designed By Seo Blogger Templates