"Update complete" Linux command line and Shell script Programming Daquan (3rd edition) reading notes 21-26 chapters

Source: Internet
Author: User

21st Chapter SED Advanced

N: Add the next line in the data flow to create a multiline group (multiline) to process.

D: Delete the first row in a multiline group.

P: Prints the first row in the multiline group.

Sed '/header/{n; d} ' Data.txt

First match the row with the header, then N to move to the next line of text, and then delete. Then continue searching for the line containing the header in Data.txt and continue with the previous process.

Add a text that reads as follows:

[email protected] ~]# cat Data.txt

On Tuesday, the Linux System

Administrator ' s group meeting would be held.

All System Administrators should attend.

Thank for your attendance.

Now you want to replace the first and second lines of system administrator with the desktop User, you can do this:

[[Email protected] ~]# sed ' n;s/system.administrator/desktop user/' data.txt

On Tuesday, the Linux Desktop User's group meeting would be held.

All Desktop Users should attend.

Thank for your attendance.

Used here (.) To match spaces and newline characters, but when it matches a newline character, it removes the line break from the string, causing the two lines to be merged into one line. n here is the addition of the next line of text to the existing text in the pattern space. If you do not want to create a newline, you can do this:

Sed ' n;s/system\nadministrator/desktop\nuser/;s/system administrator/desktop user/' data.txt

Again as text:

[email protected] ~]# cat Data.txt

On Tuesday, the Linux System

Administrator ' s group meeting would be held.

All System Administrators should attend.

At this point the system administrator in the last row, if using n would miss it, because no other rows could be read into the schema space and the row was merged, and the N command would be halted, so to change:

Sed ' s/system administrator/desktop user/; n;s/system\nadministrator/desktop\nuser/' Data.txt

There is such a text:

[email protected] ~]# cat Data.txt

The header line.

This was a data line.

The last line.

Now you want to delete the first line of blank lines:

Sed '/^$/{n;/header/d} ' Data.txt

First SED looks for a blank line (^$) and then uses the n command to add the next line of text to the pattern space. If the new schema space content contains the header of the word, the D command deletes the first row in the pattern space.

There is a text:

[email protected] ~]# cat Data.txt

This is the header

Line.

This was a data line.

The last line.

[Email protected] ~]# sed-n ' n;/header\nline1/p ' data.txt

This is the header

Pattern space is an active buffer that saves the text to be checked when the SED editor executes the command, but it is not the only space for the SED editor to hold the text. The SED editor has another buffer area to hold space.

h, copy the pattern space to the hold space.

H, attach the pattern space to the hold space.

G, which will keep the space copied to the pattern space.

G, which will keep space attached to the pattern space.

x, swap mode space and save the contents of the space.

For example, there is a text:

[email protected] ~]# cat Data.txt

This is the header

Line1.

This was a data line.

The last line.

[[email protected] ~]# sed-n '/header/{h;p;n;p;g;p} ' Data.txt

This is the header

Line1.

This is the header

Sed first takes out the matching header line, then copies the line to hold space, then puts the next line in the pattern space and prints it, then copies it from the hold space to the pattern space and prints it out.

[Email protected] ~]# sed-n '/header/!p ' data.txt

Line1.

This was a data line.

The last line.

!p is excluded from printing.

Sed ' $! N;s/system\nadministrator/desktop\nuser/;s/system administrator/desktop user/' data.txt

$! n means that the last line is not executed, and the next line is added to the pattern space after matching, but the remaining rows are executed.

Sed ' {2,3b; s/this is/is this/;s/line./test?/} ' data.txt

Skip 2-3 lines without filtering.

Sed ' {/first/b jump1;s/this is the/no jump on/;:jump1;s/this be the/jump here on/} ' data.txt

Sets the behavior of the match first JUMP1 tag, where the tag matches with a specific substitution, and the remaining lines using the default substitution.

Sed ' {s/first/matched;t;/s/this is the/no match on/} ' data.txt

This is a If-then statement that first matches the primary, if the match is replaced with matched, then the T is not executed, and if the first substitution fails, the second substitution executes.

The,& symbol in SED can be used to represent a matching pattern in a replacement command:

[Email protected] ~]# echo "The cat sleeps in his hat." | Sed ' s/.at/' & '/g '

The "Cat" sleeps in his "hat".

. At matches cat and hat, and is then replaced by "Cat" and "hat."

[Email protected] ~]# echo "The System Administrator manual" | Sed ' s/\ (system\) administrator/\1 user/'

The System User Manual

Sed doubles line spacing (adds a blank line after each row has rows)

Sed ' G ' data.txt

If the last line does not require a blank line:

Sed ' $! G ' Data.txt

Double the line spacing for text that may contain blank lines:

Sed '/^$/d;$! G ' Data.txt

To add a line number to the file:

Sed ' = ' data.txt

Print the end line:

Sed-n ' $p ' data.txt

22nd Chapter Gawk Advanced

Gawk supports two types of variables:

Built-in variables

Custom variables

$1,$2 This is a built-in variable type---data field variable.

Gawk data fields and record variables:

FieldWidths, a column of numbers separated by spaces that defines the exact width of each data field

FS, input field delimiter

RS, enter record delimiter

OFS, output field delimiter

ORS, output record delimiter

Like what:

Gawk ' begin{fs= ', '; ofs= '-'} {print $1,$2,$3} ' data.txt

Once fieldwidths is set, FS is ignored and fields are calculated based on the width of the field provided.

Gawk ' begin{fieldwidths= ' 3 5 2 5 "} {print $1,$2,$3,$4} ' data.txt

Output 3-bit, 5-bit, 2-bit, 5-bit.

The default RS and Ors are set to line breaks. The record delimiter is where the delimiter counts as a record.

Gawk built-in variables:

ARGC, number of current command line arguments

Argind, the location of the current file in argv

ARGV, an array containing command-line arguments

CONVFMT, number conversion format (see printf statement), default value%.6 g

ENVIRON, an associative array that consists of the current shell environment variable and the period value

ERRNO, the system error number when the input file error is read or closed

filename, the file name of the data file used as the Gawk input data

FNR, the number of data rows in the current data file

IGNORECASE, when set to a value other than 0, ignores the character case of the character teller that appears in the Gawk command

NF, total number of fields in the data file

NR, number of input records processed

OPMT, the output format of the number, the default value is%.6 g

Rlength, the length of the substring that is matched by the match function

Rstart, the starting position of the substring that is matched by the match function

The NF variable can specify the last data field in the record without knowing where it is located:

Gawk ' begin{fs= ': '; Ofs= ":"} {print $, $NF} '/etc/passwd

Gawk Advanced Usage:

Gawk ' begin{var["a"] = 1;var["g"] = 2; for (test in Var) {print "Index:" Test, "-Value:", Var[test]}} '

Gawk ' begin{fs= ', '/11/{print ' data.txt

Match 11 for output.

Gawk ' begin{fs= ', '} $ ~/^data2/{print} ' Data.txt

The second field matches the beginning of data2, and the entire line is printed.

Exclude:

$!~/expression/

Gawk-f: ' $4 = = 0 {print '} '/etc/passwd

The fourth data field at the end of 0 prints the first data field.

= =, equal to

<=, less than or equal to

<, less than

>=, greater than or equal to

, Greater than

Gawk ' {if (>) print $ ' data.txt

Judging way.

Gawk ' {if (>20) {x= $ * 2; print X} else {x = $1/2;print x}} ' Data.txt

If-else Way.

Gawk ' {total = 0;i = 1;while (i<4) {total+= $i; i++} avg = total/3;print avg} ' Data.txt

Gawk ' {total = 0;i = 1; does {total + = $i; i++} while (total < all) print Total} ' Data.txt

Gawk ' {total =0;-(i = 1;i < 4; i++) {total + = $i} avg = total/3;print avg} ' Data.txt

Chapter 23rd using other shells

You can try to learn to use zsh and support modularity.

24th. Writing a simple script utility

Slightly

25th. Create DATABASE, Web, and e-mail related scripts

The side that writes the script to MySQL is available:

MySQL mytest-u test <<eof

Show tables;

SELECT * FROM Employees where salary > 40000;

Eof

echo "This is a text message." | mail-s "Subject" [email protected]-b [email protected]-C [email protected]

-s Specifies the body,-B is bcc,-c is CC.

The 26th chapter some interesting little scripts

Wget-o Log.log-o x.html $url

The page of the address of the x.html is saved into a log, and the output is stored as log.log.

WGET-NV--spider $url

Tests if the-NV is valid and streamlines the output.

Curl can be used to send messages:

[Email protected] ~]# Curl http://textbelt.com/intl-d number= "15601857483"-D "Message=text"

{

"Success": True

}


"Update complete" Linux command line and Shell script Programming Daquan (3rd edition) reading notes 21-26 chapters

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.