Lao Li share: "Linux shell Script Raiders" Essentials (ii)

Source: Internet
Author: User

Poptest is the only training institute for developing Test and development engineers in China, aiming at the ability of the trainees to be competent in automated testing, performance testing and testing tools development. If you are interested in the course, please consult qq:908821478. Linuxshell is one of the basic skills of a test development engineer, so a large number of Linuxshell courses have been added to the Poptest test development class, laying the groundwork for students to develop cross-platform test platforms.

1. Cat

Cat-s//Multiple blank lines are compressed into one

Cat *.txt | Tr-s ' \ n '//Remove blank lines

Cat-n//Gach No.

2. Find

Traverse down the file hierarchy, match the qualifying files, and perform the appropriate actions.

eg

Find./! -name "*.txt"-print

[[email protected] program_test]# Find/-type f-name "*.SWP"-delete

3. Xargs converts standard input data into command line parameters.

[email protected] program_test]# Cat OUT.txt | Xargs//Convert a single line to multiple lines

[email protected] program_test]# Cat OUT.txt | Xargs-n 3//Convert a single line to multiple lines, with a number of 3 per row.

Count the number of lines of code for. C and. cpp files. Xargs-0 the delimiter.

[[email protected] program_test]# find. -type f-name "*.c*"-print0 | xargs-0 wc-l
Ten./main/cos_value.c
Ten./main/sin_value.c
5./MAIN/HAHA.C
/main/main.c.
8./hello.cpp
8./sin.c
/review2.cpp.
/review5.cpp.
7./hello.c
119 Total

The 4.tr command (abbreviated by translate) can replace, delete, and compress characters from standard input.

That is, change a set of characters to another set of characters.

1) Replace

echo "HELLO" | tr [A-z] [a-z]

2) Delete

[Email protected] program_test]# echo "Hello 123 World 456" | Tr-d ' 0-9 '

Hello World

3) Compressed characters

[Email protected] program_test]# echo "GNU is not UNIX" | Tr-s "
GNU is not UNIX

General examples

[email protected] program_test]# cat Sum.txt
1
2
3
4
5
[email protected] program_test]# Cat Sum.txt | Echo $[$ (tr ' \ n ' + ') 0]
15

5,MD5 Check

[Email protected] program_test]# md5sum out.txt > Out.txt.md5
[email protected] program_test]# cat OUT.TXT.MD5
Fd46d559bf0c90170fef3da3c3de4c67 OUT.txt

eg

[[email protected] program_test]# Find/-type f-name "*.txt"-print0 | xargs-0 md5sum >> CURR_DIR.MD5

46e17910faf509df48dbfba9729ef018./banana.txt
C1DBBF63209A5580C052DC557510E7FB./11.txt
A80ddf02fa3a86c14066204e4bf2dbb9./multiline.txt

[Email protected] program_test]# md5sum-c CURR_DIR.MD5
./banana.txt:ok
./11.txt:ok
./multiline.txt:ok

6. Sort order

Sort sorted by 2nd column

[Email protected] program_test]# sort-k 2 sort.txt
4 Bad 5000
3 Linux 50
1 Mac 2000
2 WinXP 100

Sort reverse order
[Email protected] program_test]# sort-r sort.txt
4 Bad 5000
3 Linux 50
2 WinXP 100
1 Mac 2000

General Example: Count the occurrences of each character in a string

[Email protected] program_test]#./total_cnts.sh
Ahebhaaa
4a1b1e2h
[email protected] program_test]# cat total_cnts.sh
input= "AHEBHAAA"
output=$ (echo $INPUT | sed ' s/[^. /&\n/g ' | Sed '/^$/d ' | Sort | uniq-c | Tr-d ' \ n ')
Echo $INPUT
Echo $output

[Interpretation]: sed ' s/[^. /&\n/g '//any one of the characters is appended with \ n

Split as follows:

[Email protected] program_test]# input= "AHEBHAAA"
[Email protected] program_test]# echo $input | Sed ' s/[^. /&\n/g '
A
H
E
B
H
A
A
A

Sed '/^$/d '//delete empty lines

UNIQ-C//counts the number of occurrences of each line of text.

Tr-d ' \ n '//Remove line breaks and space characters

7. Temporary file naming

[Email protected] program_test]# temp_file= "/tmp/var.$$"
[Email protected] program_test]# echo $temp _file
/tmp/var.16565

Lao Li share: "Linux shell Script Raiders" Essentials (ii)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.