Linux Command Line Tips and Tricks Text Editing For mostly all examples we use sed utility with substitution command. The syntax is pretty simple: sed -i 's/SEARCH-WORD/REPLACMENT-WORD/gI' input # or for regular expressions: sed -i 's/SEARCH-REGEXP/REPLACMENT-WORD/gi' input The -i option edit and update text file in place. For piped input from another utility it is not necessary The / are delimiters between words or regexp. sed -i 's/SEARCH-REGEXP/REPLACMENT-WORD/gI' - I flag make search case insensitive sed -i 's/SEARCH-REGEXP/REPLACMENT-WORD/gI' - for text files input use g flag to make search global across the whole file print first N chars We want print first N chars. Linux
Awk BuiltIn Variables The following is a list of variables that awk sets automatically on certain occasions in order to provide information to your program. The variables that are specific to gawk are marked with a pound sign (#). These variables are gawk extensions. In other awk implementations or if gawk is in compatibility mode (see section Command-Line Options ), they are not special. VARIABLE NAME DESCRIPTION FS input field separator variable OFS Output Field Separator RS Input Record Separator variable ORS Output Record Separator Variable NR Number of Records NF Number of Fields in a record FILENAME Name of the current input file FNR Number of Records relative to the current input file RLENGTH length of the substring matched by the match() function RSTART first position in the string matched by match() function FS - input field separator variable It represents the (input) field separator and its default value is space. Linux Linux Utilities Bash Development
Awk If Statement In this awk tutorial, let us review awk conditional if statements with practical examples. Normally conditional statement checks the condition, before performing any action. If the condition is true action(s) are performed. Similarly action can be performed if the condition is false. Conditional statement starts with the keyword called "if". Awk supports three different kind of if statement. Awk Simple If statement Awk If-Else statement Awk If-Else-If statement awk If Statement Single Action: Simple If statement is used to check the conditions, if the condition returns true, it performs its corresponding action(s). Linux Linux Utilities Bash Development
AWK as Grep Structure of AWK programs AWK reads the input a line at a time. A line is scanned for each pattern in the program, and for each pattern that matches, the associated action is executed. — Alfred V. Aho[13] An AWK program is a series of pattern action pairs, written as: BEGIN { # init code goes here } # "body" of the script follows: condition 1 or /pattern-1/ { # action1 - what to do with the line matching the pattern? Linux