Linux learning--review of shell tools (cut/sed/awk/sort)

Exercise 1: Output the data in the third row and the second column in a document.

Use cut and sed combined.

1. Use cut to find the data of the column

2. Use sed to find the row's data.

The command is as follows:

        cut -d " " -f 2 file.txt | sed -n '3p'

         Parsing: The -d of the cut command is used to specify the separator, and -f is used to specify the column

                     The -n of the sed command is used to print out the result, where '3p' specifies the first few lines.

cut -- used to display the specified part (column) in the row, and delete the specified field in the file

Commonly used forms: cut -d ":" -f 2 file.txt // Note -f -c can only use one of them

Commonly used parameters are:  

                -d : specify the delimiter

                -f : Display the content of the first column // For example, -f 2 is the second column, 2-4 is the second to fourth,

                            2- refers to all columns after column 2, and -2 means to include those before column 2.

               --complement extract columns other than specified fields

                -c : Indicates a character such as -c 2- is all characters including the second character.

sed -- the command used to select, replace, delete, and add data (the first few lines)

Common form: sed [option] '[action]' file name // pay attention to the single quotation marks

Common parameters:

                -n : Only used to view the data operated by the sed command, without changing the content of the source file

                -i : Use the modification result of sed to directly modify the file that reads the data, instead of the screen output action;

                -e : allow multiple sed command edits to be applied to the input data

                a : The a action will append data after the specified line, if you want to insert data before the specified line, you need to use the "i" action.

                d: delete, delete the specified row;

                p: print, output the specified line;

Exercise 2: Replace all characters a in the second row and third column of the data with c.

Idea: Get the data in the second row and the third column first, and then replace the characters in it

命令:cut -d " " -f3 a.txt | sed -n '2p' | sed -e 's/a/c/g'

 Exercise 3: After sorting the data from small to large, if the value in the second column of the third row is greater than the value in the third row and third column, output the former, otherwise output the latter.

Idea: first use sort to sort, and then use awk to judge and output while obtaining the value.

命令:sort -t " " -nk 3 b.txt | awk -F " " '{if (NR == 3) {print $2,$3}}' b.txt | awk -F " " '{if ($1 > $2) {print $1} if ($1 < $2) {print $2}}'

awk -- generally used for conditional judgment

Common form: awk 'condition 1 {action 1} condition 2 {action 2} ...' file name

Common parameters:           

                -F : specify the delimiter

                -v: var=value assigns a value value to the variable var before the start of the awk program, and these variable values ​​are used for the BEGIN fast of the awk program

                -f: Followed by a file that saves the awk program, instead of specifying the awk program on the command line

Conditions are: Basic arithmetic operations

sort -- sort the file and output

Common form: sort(option)(parameter)

 Common parameters:

                -n: Sort according to the size of the value;

                -r: sort in reverse order;

                -t sets the delimiter character used for sorting

                -k specifies the column to be sorted

Exercise 4. Write shell scripts to complete the requirements. Calculate the total price and output as required.

name        price        num

a        2        10

b        3        12

c        9        20

NOTE: The separator between characters is a tab.

The output is:

name        price        num        allmoney

a        2        10        20¥

b        3        12        36¥

c        9        20        180¥

Ideas for solving the problem: first obtain and store the value of the product of unit price and quantity, and then insert the corresponding value at the end of the line as required.

#!/bin/bash
a=`awk -F"\t" '{if(NR == 2){print $2*$3}}' a.txt`
b=`awk -F"\t" '{if(NR == 3){print $2*$3}}' a.txt`
cat a.txt | sed '1s/$/\tallmoney/;2s/$/\t`$a`/;3s/$/\t$b/'

Among them, use awk to judge and calculate the value of the product and store it, and then in the case of the original data, just insert the data in each row. Note: The backticks are used to retain the value of the current result for the next execution.

Exercise 5: In each line of the text, at the beginning of the line. Add characters to the end of the line

Use sed to manipulate lines, and use regular expressions to match the beginning and end of a line.

#!/bin/bash 
cat a.txt | sed "/./{s/^/xxx\t/;s/$/\tbbb/}"

In the command /./ is used to represent all, ^ and $ are used to match line guards and line endings.

Exercise 6: Input three numbers arbitrarily and determine the largest number.

Idea: First use read to input three numbers and store them. Then use if to judge

#!/bin/bash
read -p "one num: " a
read -p "two num: " b
read -p "last num: " c

let max=$a
if [ $b -ge $a ]
then
	max=$b
fi
if [ $c -ge $max ]
then
	max=$c
fi
echo "most big num :  $max"

Use read -p to save the input value, and then use if for conditional judgment to output the maximum value.

Guess you like

Origin blog.csdn.net/qq_50929489/article/details/127426582