AWK commands

 


employee.txt 

ajay manager account 45000

sunil clerk account 25000

varun manager sales 50000

amit manager account 47000

tarun peon sales 15000

deepak clerk sales 23000

sunil peon sales 13000

satvik director purchase 80000


awk '{print}' employee.txt

awk '/manager/ {print}' employee.txt

 awk '{print $1,$4}' employee.txt


Built In Variables In Awk

Awk’s built-in variables include the field variables—$1, $2, $3, and so on ($0 is the entire line) — that break a line of text into individual words or pieces called fields. 

NR: NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file. 

NF: NF command keeps a count of the number of fields within the current input record. 

FS: FS command contains the field separator character which is used to divide fields on the input line. The default is “white space”, meaning space and tab characters. FS can be reassigned to another character (typically in BEGIN) to change the field separator. 

RS: RS command stores the current record separator character. Since, by default, an input line is the input record, the default record separator character is a newline. 

OFS: OFS command stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. Whenever print has several parameters separated with commas, it will print the value of OFS in between each parameter. 

ORS: ORS command stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character. print automatically outputs the contents of ORS at the end of whatever it is given to print. 


 awk '{print NR,$0}' employee.txt

 awk '{print $1,$NF}' employee.txt

 awk '{print $2,$NF}' employee.txt

awk '{print $3,$NF}' employee.txt

awk 'NR==3, NR==6 {print NR,$0}' employee.txt

To Print any non empty line if persent



No comments:

Post a Comment