0% found this document useful (0 votes)
36 views

Regex, Grep, Awk, Sed

This document provides an overview and examples of common Linux commands for searching text: regex, grep, sed, and awk. Regex allows defining search patterns to match text. Sed is a stream editor that performs find/replace on stdin/files. Awk turns text into records/fields and lets you select/manipulate them like a database. Both sed and awk use patterns to select lines and support commands to modify, delete, print, or redirect output.

Uploaded by

eran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Regex, Grep, Awk, Sed

This document provides an overview and examples of common Linux commands for searching text: regex, grep, sed, and awk. Regex allows defining search patterns to match text. Sed is a stream editor that performs find/replace on stdin/files. Awk turns text into records/fields and lets you select/manipulate them like a database. Both sed and awk use patterns to select lines and support commands to modify, delete, print, or redirect output.

Uploaded by

eran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Regex,grep,awk,sed

Objectives

Linux commands for searching

– REGEX

– grep

– sed

– awk
REGEX - Regular Expression

"Regex" stands for "regular expression." It is a sequence of


characters that defines a search pattern. Regular expressions are
used in various programming languages and applications for
pattern matching within strings. They provide a powerful and
flexible way to search, match, and manipulate text based on certain
patterns.
sed - stream editor

sed takes a stream of stdin and pattern matches and

returns to stdout the replaced text.

– Think amped-up Windows Find & Replace.

• syntax:

– using stdin: cat file | sed ‘command’

– using files: sed ‘command’ file


common sed uses:

• 4d : delete line 4

• 2,4d : delete lines 2-4

• 2w foo : write line 2 to file foo

• /here/d : delete line matching here

• /here/,/there/d : delete lines matching here to there

• s/pattern/text/ : switch text matching pattern

• s/pattern/text/g: switch text matching pattern globally

• /pattern/a\text : append line with text after matching pattern

• /pattern/c\text : change line with text for matching pattern


sed - Examples
awk

command/script language that turns text into records and fields

which can be selected to display as kind of an ad hoc database.

With awk you can perform many manipulations to these fields or

records before they are displayed.

• syntax:

– using stdin: cat file | awk ‘command’

– using files: awk ‘command’ file


awk -cont

concepts:

– Fields:

• fields are separated by white space, or by regex FS.

• The fields are denoted $1, $2, ..., while $0 refers to• A pattern-action statement has the form: • A missing
{action} means print the line • A missing pattern always matches. the entire line.

• If FS is null, the input line is split into one field per character.

– Records:

• records are separated by \n (new line), or by regex RS.


awk -cont

• A pattern-action statement has the form:

pattern {action}

• A missing {action} means print the line

• A missing pattern always matches.


awk -cont

Pattern-action statements are separated by newlines or semicolons. There are three separate action blocks:

BEGIN {action}

{action}

END {action}
Simple awk example
awk - built in variables
The awk program has some internal environment variables that are

useful (more exist and change upon platform)

– NF – number of fields in the current record

– NR – ordinal number of the current record

– FS – regular expression used to separate fields; also settable by option -Ffs

(default whitespace)

– RS – input record separator (default newline)

– OFS – output field separator (default blank)

– ORS – output record separator (default newline)


awk - built in variables - example
awk - statements

An action is a sequence of statements. A statement can be one of

the following:

– if (expression) statement [ else statement ]

– while (expression) statement

– for (expression ; expression ; expression) statement

– for (var in array) statement

– do statement while (expression)


awk - statements - example
awk - variables

• Using variables:
– You can use the stock $1, $2, $3, … fields and set them to
variables in the action
block.
awk - variables - example
sed vs awk vs grep
Conclusion

sed and awk are great utilities for modifying data


By utilizing regex, we can leverage these powerful utilities
Manipulating and filtering data empowers you to solve problems
in new and concise ways

You might also like