Sunteți pe pagina 1din 6

AWK Command Syntax: awk option selection_criteria {action} file(s) selection_criteria filters input and selects lines for

r the action component to act upon. The selection_criteria and the action within the curly brace are enclosed within single quotes Both the selection_criteria and action forms an awk program.

Example: > cat DbfeedsTeam.txt Empno,Ename,Role,Rec,Sal 209682,ranvina,CTBLead,CTRL-M,10000 351184,vutajag,Developer,AliceRAN,20000 522475,dasimal,Developer,LCHsummit,30000 480562,brtejes,SL3Lead,CTRL-M,40000

awk -F',' '{print $1, $NF}' DbfeedsTeam.txt Empno Sal 209682 10000 351184 20000 522475 30000 480562 40000

awk -F',' '{print NF}' DbfeedsTeam.txt|head -1 5

Awk '/Lead/' DbfeedsTeam.txt 209682,ranvina,CTBLead,CTRL-M,10000 480562,brtejes,SL3Lead,CTRL-M,40000 awk -F',' '$5>20000' DbfeedsTeam.txt Empno,Ename,Role,Rec,Sal 522475,dasimal,Developer,LCHsummit,30000 480562,brtejes,SL3Lead,CTRL-M,40000

Since printing is the default action of awk, any one of the following three forms can be used: Awk /Lead/ DbfeedsTeam.txt Awk /Lead/ { print } DbfeedsTeam.txt awk /Lead/ { print $0} DbfeedsTeam.txt $0 specifies complete line

Select * from DbfeedsTeam.txt awk -F',' { print $0 } DbfeedsTeam.txt Select col1, col2 from DbfeedsTeam.txt awk -F',' { print $1, $2 } DbfeedsTeam.txt Select col1, col2 from DbfeedsTeam.txt where col2=vutajag awk -F',' $2 == vutajag { print $1, $2 } DbfeedsTeam.txt Select col1, col2 from DbfeedsTeam.txt where $2=vutajag AND $5=20000 awk -F',' '$2 == "vutajag" && $5 == 20000 { print $1, $2 }' DbfeedsTeam.txt Select col1, col2 from DbfeedsTeam.txt where col3 LIKE %taj% awk -F',' '$2 ~ /taj/ { print $1, $2 }' DbfeedsTeam.txt awk F , '{ OFS="~"; ORS="%\n" ; print $2,$3 } DbfeedsTeam.txt

awk -F',' '{ OFS="~";ORS="%\n";print $2,$3 }' DbfeedsTeam.txt Ename~Role% ranvina~CTBLead% vutajag~Developer% dasimal~Developer% brtejes~SL3Lead% awk -F',' '{ if ( $2=="vutajag" || $2=="ranvina" || $2=="dasimal" ) { print $0 } }' DbfeedsTeam.txt > DbfeedsTeam1.txt cat DbfeedsTeam1.txt 209682,ranvina,CTBLead,CTRL-M,10000 351184,vutajag,Developer,AliceRAN,20000 522475,dasimal,Developer,LCHsummit,30000 awk /^hi/ { print $0 } file This prints those lines which starts with hi. (^) awk $2 < $3 { print $0 } file

This prints those lines where 2nd field is lesser than 3rd field ( assumed the fields are numbers ). awk /^Canada/ , /^USA/ {print $0} file prints all those lines starting from the line that starts with Canada till the next line that starts with USA

NF - Number of fileds variable:

The NF can be used to know the number of fields in line awk '{print NF}' input_fileThis will display the number of columns in each row.

NR - number of records variable:

The NR can be used to know the line number or count of lines in a file. awk '{print NR}' input_file This will display the line numbers from 1. awk 'END {print NR}' input_file This will display the total number of lines in the file.

AWK One Liners:


Command awk '{print FNR "\t" $0}' filename awk '{ print $NF }' filename awk 'END{print NR}' filename awk '/Apache/{n++}; END {print n+0}' filename awk '{ sum += $6 } END { print sum }' filename awk '$4 ~ /^[09]/ { print $3 }' filename awk '{sub(/[ \t]+$/, "");print}' filename awk '{gsub(/^[ \t]+|[ \t]+$/,"");print}' filename awk F: '{print $1 " " $2}' filename awk '{tmp = $1; $1 = $2; $2 = tmp; print}' filename awk 'NR <= 5' filename awk 'END{print}' filename awk '{ $1 = ""; print }' filename awk '{sub(/^/, " ");print}' filename awk '{sub(/^[ \t]+/, ""); print}' filename Regular Expressions awk '/regex/' filename Description Print line numbers using a tab instead of a space Print the last field of each line of the file Count the lines in a file. Just like "wc l". Total the number of lines that contain the name Apache Add up the numbers of 6th field, print the total If the 3rd field starts with a number, print that field Delete the trailing whitespace from end of each line Delete leading and trailing whitespace from each line Print the first 2 fields with a space between the output Swap the first 2 fields Print the first 5 lines of a file Print the last line of a file Remove the second field in each line and then print it Insert 6 blank spaces at beginning of each line Delete the leading whitespace (spaces or tabs) from front Print the lines matching the regular expression

awk '!/regex/' filename awk '/regex/{print x};{x=$0}' filename awk '/regex/{getline; print}' filename awk '/Dog/,/Cat/' filename awk '!/pet/{gsub(/dog/, "cat")};{print}' filename awk 'length < 50' filename awk 'NR==20,NR==30' filename awk 'NR==50 {print;exit}' filename Substitution awk '{gsub(/dog|cat|bird,"pet");print}' filename awk '{gsub("dog", "cat", $0); print > FILENAME}' *.txt Counting awk '/virtual/{n++}; END {print n+0}' filename ps aux | awk '/program_name/ && !/awk/ {print $2}' > kill

Print the lines that don't match the regular expression Print the line before the regular expression match Print the line after the regular expression match Print lines between the matches starting one and ending Substitute "dog" with "cat" on lines that don't contain word "pet" Print the lines less than 50 characters Print the lines 20 through 30 Print the line 50 Find/replace dog,cat or bird with pet and print Find/replace dog with cat in every file with extension txt Print the total number of lines that have the name virtual Find a program by name from process listing that is not awk and kill it

Use awk to pull the seventh field of each line of the logfile. Sort the fields then find the unique fields and count them, do a reverse sort on numeric count,filter out anything but JPEG files and only give me the top 10 of that list. unique hostnames or urls from a logfile awk '{print $7}' logfile | sort | uniq c| sort rn| grep "\.jpg" | head

Numbering awk '{print FNR "\t" $0}' files* Precede each line with number for the line

S-ar putea să vă placă și