Documente Academic
Documente Profesional
Documente Cultură
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.
awk -F',' '{print $1, $NF}' DbfeedsTeam.txt Empno Sal 209682 10000 351184 20000 522475 30000 480562 40000
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
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.
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 '!/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