Linux Classroom Series – 19/Sept/2020

Extended grep (grep -E or EGREP)

  • Write a extended regex with alternations
  • Lets look for examples
  • Grep Options
    1. -b
    2. -c
    3. -h
    4. -i
    5. -l
    6. -n
    7. -s
    8. -v
    9. -w

Sed

wget https://gist.githubusercontent.com/shaikkhajaibrahim/8eb405804c97dba868ee9e359109871d/raw/901edf320164af623eba04f5affbbc4ab1e93038/sales.csv
  • Sed cheatsheet Refer Here or use any cheatsheet of your choice

  • Sed Commands:

    • a\
    • b label
    • c\
    • d
    • D
    • i\
    • h
    • H
    • g
    • G
    • l
    • n
    • N
    • P
    • q
    • w
    • W
    • s/re/string/
  • Error messages:

    1. Unknown command
    sed '1,3v ' sales.csv
    
  • Examples

  • Write a sed command to print

    • all the records having Libya
    • Replace Libya with UAE
    • Insert a line for all the records which have Canada with any text
    • Insert a line at the end of the file

AWK

  • AWK is programming language for manipulating data and generating reports.
  • AWK format
awk 'pattern' filename
awk '{action}' filename
awk 'pattern' '{action}' filename

Shell scripts

  • How to deal with named arguments
./installjava.sh --package-name openjdk8-jdk
  • Lets understand an important operator called as shift

    • When the run shift the current positional parameters are shifted left n times default value of n is 1
    • Look at the below example
    #!/bin/bash
    
    echo "arguments passed are: $@"
    echo "total number of arguments $#"
    echo "first argument is $1"
    shift
    echo "after shift"
    
    echo "arguments passed are: $@"
    echo "total number of arguments $#"
    echo "first argument is $1"
    
    shift 2
    
    echo "after shift 2"
    echo "arguments passed are: $@"
    echo "total number of arguments $#"
    echo "first argument is $1"
    
    
    • The output of execution with various examples is
  • We can use shift operator and a while loop to parse arguments

#!/bin/bash
#./calc.sh --print 1 --square 2
#./calc.sh -p 1 -s 2

while [[ $# -gt 0]]
do
    key="$1"
    case $key in 
        -p|--print)
        echo "$2"
        shift 2
        ;;
        -s| --square)
            number="$2"
            echo $((number * number))
        shift 2
        ;;
    esac
done

  • Simple square and cube calculator
#!/bin/bash

while getopts "s:c:" option;
do 
    case "$option" in
        s)
            value=$OPTARG
            echo "$((value * value))"
            ;;
        c)
            value=$OPTARG
            echo "$((value * value * value))"
            ;;

    esac
done
  • Exercise: Extend this script to display help when user passes -h and also support long options

Leave a ReplyCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Social Network Integration by Acurax Social Media Branding Company

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version
%%footer%%