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

Source: Internet
Author: User

1, the IP address of the regular expression: [0-9]{1,3}\. [0-9] {1,3}\. [0-9] {1,3}\. [0-9] {1,3}

2. grep usage

Recursive retrieval of text in a multilevel directory

[Email protected] program_test]# grep "yang"./-rn

./test.txt:6:laoyang
./right.txt:1:1 Yang Man

Ignore case matching

[Email protected] program_test]# echo Hello World | Grep-i "HELLO"
Hello World

recursively searches all. C and. cpp files

[[email protected] program_test]# grep "main ()". -R--include *. {C,cpp}
./hello.c:int Main ()
Sin.c:int Main ()
Hello.cpp:int Main ()

Match a few lines after a result

[Email protected] program_test]# echo-e "A\NB\NC\NA\NB\NC" | grep a-a 1
A
B
--
A
B

3. Cut command

Cut, a gadget that cuts text by column.

-D delimiter; -F Columns to extract

[Email protected] program_test]# cut-d ":"-f5--complement Passwd_yang
Root:x:0:0:/root:/bin/bash
Bin:x:1:1:/bin:/sbin/nologin

[Email protected] program_test]# cut-c1-5 Passwd_yang
Root
Bin:x
Daemo
Adm:x

Count the word frequency in a specific file

[email protected] program_test]# cat word_freq.sh

#!/bin/bash

If [$#-ne 1];

Then

echo "Usage: $ filename"

Exit-1

Fi

Filename=$1

Egrep-o "\b[[:alpha:]]+\b" $filename | \

awk ' {count[$0]++} \

END {printf ("%-14s%s\n", "word", "Count"); \

for (Ind in count) \

{printf ("%-14s%d\n", Ind,count[ind]);} }‘

4. Sed command (stream editor)

Apply text processing.

1. Replace, start with the 3rd one

[Email protected] program_test]# echo this thisthisthis | Sed ' s/this/this/3g '
This thisthisthis

2. Remove the blank line

[[Email protected] program_test]# sed '/^$/d ' choice.sh

3. Matched string Markers &

[Email protected] program_test]# echo This was an example | Sed ' s/\w\+/[&]/g '
[This] [IS] [An] [Example]

4. Replacement example.

[email protected] program_test]# cat Sed_data.txt
ABC 111 this 9 file contains 111 numbers 0000
[email protected] program_test]# Cat Sed_data.txt | sed ' s/\b[0-9]\{3\}\b/number3/g '
ABC NUMBER3 This 9 file contains NUMBER3 numbers 0000

5. Practical examples

Get an error message for NTP synchronization (assuming the current device is not networked)

Example one: Ntpdate 8.8.8.8

Jan 07:28:26 ntpdate[7137]: Bind () Fails:permission denied

Example two: Ntpdate google.com

[Email protected] cutdemo]# ntpdate msf22.com
Error resolving msf22.com:Name or service not known (-2)
Jan 07:30:54 ntpdate[7169]: Can ' t find host msf22.com:Name or service not known (-2)
Jan 07:30:54 ntpdate[7169]: No servers can be used, exiting

Want to get [71**]: After the error message, before the deletion. The script is as follows:

[Email protected] cutdemo]# ntpdate msft22.com 2>&1 | Sed ' s/.*\]\://g '
Error resolving msft22.com:Name or service not known (-2)
Can ' t find host msft22.com:Name or service not known (-2)
No servers can be used, exiting

Explanation: Ntpdate msft22.com 2>&1//2>&1 standard error redirect to standard output.

Sed ' s/.*\]\://g '//delete file "]:" in front of the string.

5. awk tool for data flow, operation on columns and rows.

1), how awk is implemented

[Email protected] program_test]# echo-e "Line1\nline2" | awk ' BEGIN {print ' begin...\n '} {print} END {print ' end...\n '} '
Begin ...

Line1
Line2
End ...

2), awk to achieve cumulative sum

[[email protected] program_test]# SEQ 5 | awk ' BEGIN {sum=0; print ' summary: '} {print ' + '; sum+=$1;} END {print "= =" Sum} '

Summary
1 +
2 +
3 +
4 +
5+
==15

3), awk sets the delimiter.

-F delimiter $NF the last field in a row

[Email protected] program_test]# awk-f: ' {print $ \ t ' $NF} '/etc/passwd
Root/bin/bash
Bin/sbin/nologin
Daemon/sbin/nologin

4), print each letter in the file

[email protected] program_test]# cat read_each_word.sh

Cat hello.c | \

(While read line;

Do

#echo $line;

for word in $line;

Do

#echo $word;

For ((i=0;i<${#word};i++))

Do

Echo ${word:i:1};

Done

Done

Done)

5), Print 4–6 line content

[[email protected] program_test]# SEQ 100 | awk ' nr==4, nr==6 '
4
5
6

6), awk to implement the function similar to TAC reverse order.

[[email protected] program_test]# SEQ 9 | awk ' {lifo[nr]=$0; Lno=nr} END {print "nr =" NR; for (; lno>-1;lno--) {print Lifo[lno];}} '

NR = 9

9

8

7

6

5

4

3

2

1

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

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.