How to Use Awk to Find and Replace Text

|

Awk is a unique computer processing language that can be used to find and replace text. It is typically used with Unix-based systems and reformats output.

Use Awk to Find and Replace Text

Step1 Use awk to scan for lines that match the parameters that you've previously specified in the program file in order to find and replace relevant text.

Step2 Input files in the order you want them to be read. The string of input files you use should be noted by using single quotes. If you fail to input any files, the standard input will be used as a default. The standard input will show the file name as '-'.

Step3 Know that each input line consists of fields separated by blank spaces.

Step4 Become intimately familiar with pattern-action statements. They are extremely important when using awk to find and replace text. A pattern-action statement looks like this:
pattern { action }

Step5 Separate multiple patterns with a comma.

Step6 Know the shorthand for variables you will encounter. Common variables include the number of fields (NF), output format of numbers (OFMT), input record separator (RS) and output record separator (ORS). There are many other variables that will be encountered, and a complete list of them, as well as brief explanations, are available in the online version of the Unix Manual (see Resources below).

Step7 Use the arithmetic functions. Sin (x) produces the sine of x. Cos (x) produces the cosine of x. Exp (x) provides the exponential function of x which is useful for rapid growth. Find the natural logarithm of x with log (x). The square root of x is found with sqrt (x).

Step8 Brush up on your string functions. String functions are incredibly useful in awk, but if you're not a mathematician or a regular Unix user, you might need to do some homework first. The string functions you'll encounter are as follows:
Index function finds the point in one string where another first appears.
Split function splits the string into component elements.

Identify substrings within strings using the substring function.
Step9Refer to the largefile section of the manual to learn how to use awk when the files you are examining are greater than or equal to 2 gigabytes.

0 comment:

Post a Comment

 

©2009 computer technology World | Template Blue by TNB