Wednesday, May 10, 2017

How to use “awk” in Linux–part 1



අද මම කියලා දෙන්නේ Linux වලදී text processing වලට බහුලව භාවිතා වන programming language එකක් වන “AWK” ගැනයි
“AWK” කියන්නේ interpreted programming language එය ඉතාමත් වේගවත් (Vey Powerful) හා විශේෂයෙන්ම Text processing වලට design කරන ලද programming language එකකි



මෙම “AWK” කියන නම හැදිලා තියෙනෙන් එය නිර්මාණය කල නිර්මාණකරුවන්ගේ නම් වලිනි (Alfred Aho, Peter Weinberger, and Brian Kernighan)
“AWK” වර්තමන්යේ ව්‍යාප්තිය ගත් කල ප්‍රධන විබෙදුම් 3ක් යටතේ පවත්වාගෙන යයි, ඒවානම්
  • AWK − Original AWK from AT & T Laboratory.
  • NAWK − Newer and improved version of AWK from AT & T Laboratory.
  • GAWK − It is GNU AWK. All GNU/Linux distributions ship GAWK. It is fully compatible with AWK and NAWK.

“AWK” install කිරීම
“AWK”  සාමාන්‍යයෙන් ඔබ භවිතා කරන Linux මෙහෙයුම් පද්ධතිය සමග Pre-Installed package එකක් ලෙස පැමිණේ.ඔබ භාවිතා කරන Linux මෙහෙයුම් පද්ධතිය AWK නොමැතිනම් පහත ආකාරයට install කරගන්න.
Verify කිරමට (Pre installed status check)
  • Which awk          [or]
  • awk --version
install කිරමට
  • sudo yum install gawk [Redhat/Centos]
  • sudo apt-get install gawk   [Debian/Ubuntu]

“AWK” Usage :
  • මේ සදහා මම පහත දැක්වෙන sample text file එක භාවිතා කරනවා



“AWK” file එකක ඇති content display කිරීම.
Command : awk ‘{print}’ sample.txt



“AWK” Command Program file
“awk" commands file එකක store කර එය මගින් ඉහත file එකේ content display කිරීම.
මේ සදහා මම පළමුව මට execute කිරමට අවශ්‍ය command එක ඇතුලත් file එකක් නිර්මාණය කරනවා පහත පරිදි
  • nano command.awk
  • Add following content and save

(Multiple commands වුවද මේ අකාරයට send කල හැකිය,Multiple commands සදහා line by line ඔබට අවශ්‍ය commands add )
file එකේ content display කිරීමට පහත command එක use කරන්න
Command : awk –f command.awk sample.txt


“AWK” Printing FIle contents
“awk" commands මගින් file content display කිරීමේ තවත් ක්‍රම
  • awk ‘{print $0}’ sample.txt  [Print all rows]

  • awk ‘{print $3 $4}’ sample.txt [Print column 3 and 4]

  • awk '{print $3 "," $4}' sample.txt [Print column 3 and 4 with comma separated]

  • awk '/al/ {print $0}' sample.txt [Print all rows containing ‘al’ letters]

  • awk '/al/ {print $2 "\t"  $3 "," $4 "," $5}' sample.txt [Print Name and Marks with comma separated]

  • awk '/al/{++count} END {print "AL containing words Count = ",count}' sample.txt  [Count No of occurrence which containing ‘al’ characters]

  • awk '{print length($0) "," length($3) }' sample.txt  [Print the characters length of total row and column 3]

  • awk 'length($2)>6' sample.txt [Print all lines which contain more than 6 characters in column 2]

  • awk 'length($2)>6 {print}' sample.txt [Print all lines which contain more than 6 characters in column 2]




-මම හිතනවා ඔබ awk ගැන මුලික දැනුමක් ලබා ගත්තා කියලා ඉතින් අපි දෙවන කොටසින් හමුවෙමු




SITTHAMA

Sinhala Support


Followers

Designed By Seo Blogger Templates