Linux learning notes: text processing and shell programming

Posted by robwilson on Sat, 08 Jan 2022 22:25:53 +0100

Welcome to the official account, develop the big data from the sinkhole Specialty:

 

Three swordsmen of text processing: awk, grep, sed

​awk:

AWK is a language for processing text files and a powerful text analysis tool.

Parameters:

       

grep:

The Linux grep command is used to find qualified strings in a file.

The grep instruction is used to find a file whose content contains the specified template style. If it is found that the content of a file conforms to the specified template style, the default grep instruction will display the column containing the template style. If no file name is specified, or if the given file name is -, the grep instruction reads data from the standard input device.

grep parameter:

       

sed:

The Linux sed command uses scripts to process text files.

sed can process and edit text files according to the instructions of the script.

Sed is mainly used to automatically edit one or more files, simplify repeated operation of files, write conversion programs, etc.

       

     

       

Judge according to file permissions

       

  1. Compare between two files

  1. Comparison between two integers

       

         

[ 23 -ge 22 ] && echo "yes" || echo "no" yes

#Judge whether 23 is greater than or equal to 22, of course

[ 23 -le 22 ] && echo "yes" || echo "no"  no

#Judge whether 23 is less than or equal to 22, of course not

5. String judgment

       

         

name=sc

#Assign a value to the name variable

[ -z "$name" ] && echo "yes" || echo "no"  

no

#Judge whether the name variable is empty. Because it is not empty, no is returned

aa=11

bb=22

#Assign values to variables aa and bb

[ "$aa" == "bb" ] && echo "yes" || echo "no"            

no

#Judge whether the values of two variables are equal and obviously not equal, so no is returned

6. Multiple conditional judgment

       

         

aa=11

[ -n "$aa"  -a "$aa" -gt 23 ] && echo "yes" || echo "no"

no

#Judge whether the variable aa has a value and whether the value of the variable aa is greater than 23

#Because the value of variable aa is not greater than 23, although the first judgment value is true, the returned result is also false

aa=24

[ -n "$aa"  -a "$aa" -gt 23 ] && echo "yes" || echo "no" yes

Several points needing attention in single branch conditional statements

  • if statements end with fi, which is different from curly braces in general languages

  • [conditional judgment formula] is to use the test command to judge, so there must be a space between brackets and conditional judgment formula

  • then is followed by the program executed after meeting the conditions. You can put it after [], and use ";" division. It can also be written on a new line, so ";" is not required Yes

Example: determine partition utilization

#!/bin/bash#backups mysql database# Author:shenchao(E-mail:shenchao@lampbrother.net)ntpdateasia.pool.ntp.org&>/dev/null#Synchronize system time date=$(date+%y%m%d)#Assign the current system time to the variable in the format of "mm / DD / yy" datesize=$(du-sh/var/lib/mysql)#Count the size of the MySQL database and assign the size to the size variable if [- D / TMP / dbbak] then echo "date: $date!" >/ tmp/dbbak/dbinfo. txt        echo"Datasize$size">>/tmp/dbbak/dbinfo. txt        cd/tmp/dbbak        tar -zcf mysql-lib-$date. tar. gz /var/lib/mysql dbinfo. txt &>/dev/null        rm -rf /tmp/dbbak/dbinfo. txt    else        mkdir /tmp/dbbak        echo"Date:date!">/ tmp/dbbak/dbinfo. txt        echo"Datasize:size">>/tmp/dbbak/dbinfo. txt        cd/tmp/dbbak        tar -zcf mysql-lib-$date. tar. gz /var/lib/mysql           dbinfo. txt &> /dev/null         rm -rf /tmp/dbbak/dbinfo. txt fi

#!/bin/bash#From 1 to 100#Author:shenchao(E-mail:shenchao@lampbrother.net)i=1s=0while [ $i -le 100 ] #If the value of variable i is less than or equal to 100, execute the loop do s = $(($s + $i)) i = $(($i + 1)) doneecho "thesumis: $s"

#!/bin/bash#From 1 to 100#Author:shenchao(E-mail:shenchao@lampbrother.net)i=1s=0until [ $i -gt 100 ]#The loop stops until the value of variable i is greater than 100. Do s = $(($s + $i)) i = $(($i + 1)) doneecho "thesumis: $s"

#!/ Bin / bashfunwithreturn() {echo "this function will add the two input numbers..." Echo "enter the first number:" read aNum echo "enter the second number:" read othernum echo "the two numbers are $aNum and $othernum!" Return $(($aNum + $othernum))}funwithreturnecho "the sum of the two numbers entered is $?!" ​

Topics: Linux Operation & Maintenance server