1 of 40

Linux Summer Training 23

By Ahmed Khaled, 29/Aug/23

2 of 40

Text processing

3 of 40

yees?

yes please?

4 of 40

Outcomes

  • Increase productivity (By automating tasks)
  • Manipulate text (Search, Filter, Replace, Transform etc.)
  • Write advanced bash scripts
  • Validate user input in a smarter way
  • Understand new concepts

5 of 40

Agenda

Simple commands

Regex

02

Awk

05

Grep

03

Summary

06

01

04

Sed

6 of 40

Simple commands

7 of 40

Sort

Options

  • -n -> numerical
  • -r -> reverse

8 of 40

Rev

9 of 40

Uniq

Options

  • -c -> count
  • -d -> duplicates
  • -u -> unique

10 of 40

Cut

Options

  • -c -> characters
  • -f -> fields
  • -d -> delimiter

11 of 40

Example

12 of 40

Tr

Options

  • -d -> delete
  • -s -> replace sequence with char

13 of 40

Hands on

14 of 40

Sort�Rev

Uniq

Cut (-c, -f, -d)

Tr

15 of 40

Hands on�Solution!

16 of 40

Paste

Options

  • -s -> single line
  • -d -> delimiter

17 of 40

Recap

18 of 40

Regex

19 of 40

Example

20 of 40

21 of 40

Regex symbols

1 or more of the previous char

+

0 or 1 of the previous (optional)

0 or more of the previous (‘+’ and ‘?’)

Anything

Characters, digits, spaces, word breaks

Beginning , end of line

?

*

.

\w, \d, \s, \b

^, $

22 of 40

Regex symbols

Character sets (^ -> except)

[ ]

Ranges {4} , {4, } , {4,5}

Grouping

Reference

{ }

( )

\1, \2

OR operator

|

23 of 40

Regex applications

  • Programming languages, IDEs
  • Finding patterns
  • User input validation
  • Cleaner code
  • Smart search (Solution)

24 of 40

BREAK

25 of 40

Grep

Options

  • -B, -E ({ }, (), ?, |, +), -P (\d) -> (Regex types)
  • -i -> Case insensitive
  • -n -> Line numbers
  • -c (Count)
  • -A, -B, -C -> (After, Before, Combination)
  • -r -> Recursive

26 of 40

Hands on

  • Grep email addresses

Ex: ahmed@gmail.com

  • Grep old discord usernames

Ex: Ahmed#1010

  • Grep credit cards that contain 2 similar consecutive digits

27 of 40

Sed

Syntax

sed “s/pattern/replace/flags” file.txt

28 of 40

Sed options

Detect ‘/’s

Detect pattern in a line first (/ /)

Delete (d)

Multiple sed commands

Insert (&)

29 of 40

Hands on

30 of 40

Hands on�Solution!

31 of 40

Awk

32 of 40

How it works

33 of 40

Syntax

awk ‘/pattern/ {action}’ file.txt

34 of 40

Example

35 of 40

Awk features

Arithmetic

If conditions + &&, ||

Relational expressions (~, !~, >)

Range patterns (,)

Begin, end

36 of 40

C = (F – 32) * 5/9

37 of 40

Hands on�Solution!

38 of 40

Summary

39 of 40

Outcomes

  • Increase productivity (By automating tasks)
  • Manipulate text (Search, Filter, Replace, Transform etc.)
  • Write advanced bash scripts
  • Validate user input in a smarter way
  • Understand new concepts

40 of 40

Thanks!