Thursday, 16 June 2022

DevNet notes - Linux - Bash (2)

grep basics

grep is a command line tool for searching plain text to match a regular expression

grep use examples:


#grep 'import' test_requests.py - will display line in the file that contains word 'import'


output:


    from turtle import title

    import requests

    from bs4 import BeautifulSoup


#grep -R 'import' . - will look for all files within the directory and subdirectories that contain word 'import'


output:


    ./test_urllib:import urllib3

    ./xml2-to-dict.py:import xmltodict

    ./xml-to-dict.py:import xmltodict

    ./eveng-request.py:import requests

    ./parse_yaml.py:import yaml

    ./test_requests.py:from turtle import title

    ./test_requests.py:import requests

    ./test_requests.py:from bs4 import BeautifulSoup

    ./automate-l1.py:from __future__ import print_function, unicode_literals

    ./automate-l1.py:import logging

    ./automate-l1.py:from netmiko import ConnectHandler, redispatch

    ./automate-l1.py:from netmiko import Netmiko

    ./automate-l1.py:from getpass import getpass

    ./json-test.py:import json

    ./parse_json.py:import json


other options:


'-i' - will make above search case sensitive, example:


#grep -R -i 'Cisco123' .


output:


    ./curl_get_token.sh:curl -X POST -u 'devnetuser:Cisco123!' -H 'Content-Type: application/json' https://sandboxdnac.cisco.com/dna/system/api/v1/auth/token


'-G' -indicates a standard regular expression, supports following metachracters:

     ^ beggining of the line

     $ end of the line

     . single character

     * zero or more occurences of the preceeding character

     [xyz] to match either 'x', 'y' or 'z'

     [d-f] or [1-3] to match character in the range between 'd-f' or '1-3'

     \< or \b to match beggining of the word

     \> to match end of the word

     \ escape character

     

'-E' -indicates extended regular expression, supports all above metacharacters used in standard expression and additionally:

     ? zero or one occurance of the proceeding character

     + one or more occurancess of the proceeding character

     {X} or {X,Y} strings with X repetition or X repetition but lower that Y repetition

     | operator 'OR'

     () capture group


'-F' -indicates fixed regular expression

'-P' -indicates Perl regular expression


Bash - echo command


echo " Hello! " - outputs the text inside quatation marks, also supports following escape characters:

    \n -new line

    \t -horizontal tab

    \v -vertical tab

    \b -backspace

    \\ -prints the backslash


echo - how to show variable/run command within quotation marks? use '$' sign, example:


lets set a variable:

#MY_VAR='0123456789'


use case:

#echo "my test variable is: $MY_VAR" 


output:

    my test variable is: 0123456789


another use example:

#echo "list of my files: $(ls)" - will return list of files in the current location


No comments:

Post a Comment

Plumbing... QoS

Rule no 1. QoS does not help in situations where there is no enough bandwidth but helps optimize performance by prioritization of the traffi...