Some simple uses of the regular expression with awk command are shown in this section.The user can easily perform many types of searching, replacing and report generating tasks by using awk, grep and sed commands.It is a scripting language that can be used from both terminal and awk file.
It supports the variable, conditional statement, array, loops etc. It can read any file content line by line and separate the fields or columns based on a specific delimiter. It also supports regular expression for searching particular string in the text content or file and takes actions if any match founds. How you can use awk command and script is shown in this tutorial by using 20 useful examples. This function can be used with awk command to generate different types of formatted outputs. Create a text file named employee.txt with the content given below where fields are separated by tab (t). If the value of the output is less than 10 characters then the spaces will be added at the front of the value. The input text is passed from echo command in the following example. The text, I like programming will be split by default separator, space, and the third word will be printed as output. Suppose, you have a text file named phone.txt with the following content where: is used as field separator of the file content. FS is input field separator and OFS is output field separator variables. Create a CSV file named customer.csv with the following content to apply awk command. The following awk command will print the name field of the customer.csv file. NR variable contains the line number of the file when awk command parses the file. In this example, the NR variable is used to omit the first line of the file. The output will show the 2 nd, 3 rd and 4 th fields of all lines except the first line. How you can create awk file and run the file is shown in this example. BEGIN keyword is used in the script for informing awk command to execute the script of the BEGIN part first before executing other tasks. Here, field separator ( FS ) is used to define splitting delimiter and 2 nd and 1 st fields will be printed according to the format used in printf() function. Different types of complicated search and replace tasks can be done very easily by using the regular expression.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
December 2020
Categories |