current position:Home>Sed, awk and lamp website architecture of Linux

Sed, awk and lamp website architecture of Linux

2022-01-26 21:51:47 Bo childe

sed Introduce

What is? sed?

Sed Is a stream processing editor , It can help us process files automatically 、 Analyzing log files 、 Modify configuration files, etc .

Stream Editor Text stream editing ,sed It's one by one “ Non interactive " Character stream oriented editor . Can handle multiple files and multiple lines at the same time

  • The original file can not be changed , Output the entire file to the screen
  • You can output the matched content to the screen
  • You can also change the original file , But no results will be returned on the screen

sed Processing flow

 sed Parameter description

sed [-hnV][-e<script>] [-f<script file >] [ text file ]

  • -e<script> or --expression= <script> As specified in the options script To process the input text file .
  • -f<script file > or --file= <script file > As specified in the options script File to process the input text file .
  • -h or --help Display help .
  • -n or --quiet or --silent Show only script The result of the treatment .
  • -V or --version Display version information .

sed Action can be understood as operation

  • a: newly added ,a You can use a string after , The new string will be on the next line of the current
  • c: replace ,c You can use a string after , These strings can replace n1,n2 Between the lines
  • d: Delete , Because it's deletion , therefore d Usually written at the end
  • i: Insert ,i You can use a string after , The new string will be on the current line
  • p: Print , Display a selected data . Usually p And parameters sed -n Run together
  • s: Replace , It can be replaced directly . Usually the s The action of can be combined with regular representation . for example 1,20s/old/new/g ( Is it right? vim The replacement is very similar to ? )

The sample file is shown in the figure

  Find the line , Find the file that contains 789 The line of

cat sed.test | sed -n '/789/p'

  Add a third line to the file , The content is hello world

sed -e '2a hello world' sed.test

  Delete row , Delete 2 To 3 That's ok

 cat -n sed.test | sed -e '2,3d'

  Delete include 789 The line of

cat sed.test | sed '/789/d'

  Replacement row , take 1-2 Replace the line with hahaha

cat -n sed.test | sed '1,2c hahaha'

  Find and replace strings , Place the... In the sample text Hello Switch to haha

 sed Modify file

  • The previous series of operations , Just output the content on the screen as required , The file itself has not changed .
  • sed It can also be used to modify the file itself . But it's very dangerous , It is possible that the file may be corrupted by misoperation , So we can only use it with sufficient certainty .
  • combination -i Find and replace to modify the file , Add... In the above example -i that will do .

  sed -i 's\Hello\haha\g' sed.test

awk Use

What is? awk?

awk Is a programming language , Used in linux/unix Processing of text and data . It supports user-defined functions and dynamic regular expressions , yes linux/unix A powerful programming tool under .

awk The name comes from the three founders Alfred Aho, Peter Weinberger, and Brian Kernighan Of Family Name First character .

awk Treatment mode

awk Process one line at a time

awk Yes for each line section Handle , Spaces and tabs are the default delimiters, slicing each row , The cut part is then analyzed

awk Format

Command format :

awk [ Option parameters ] 'script' var=value file(s)

        script form :pattern {awk Operation command }

            pattern: Regular expressions ; Logical judgment

   awk Operation command : Built in functions :print() printf() getline..;

         Control command :if(){...}else{...};while(){..};

Awk Embedded parameter application

Built-in variables 1:

  • $0: Represents the entire current line
  • $1: The first field in each row
  • ...

Built-in variables 2:

  • NR: Record number of each line , Line number
  • NF: Field number variable , Total number of fields
  • FILENAME: File name being processed

awk Example

With : Division , Show /etc/passwd The first column , That is, the user name column

 cat /etc/passwd | awk -F : '{print $1}'

Show /etc/passwd Of the 1 Column and the first 4 Column , Display... Separated by commas , Add column names before all rows begin start1, start4; Add... In the last line ,end1,end4

cat /etc/passwd | awk -F ':' 'BEGIN {print "start1,start4"} {print $1 "," $4} END {print "end1,end4"}'

  Match a certain character

 cat /etc/passwd | awk -F : '$1 ~ /oo/'

  Show /etc/passwd The first five elements of , Show the line number of each line 、 Number of columns , And the full content of the line

head -n5 /etc/passwd | awk -F : '{print NR " " NF " " $0}'

  Conditional operations match

cat /etc/passwd | awk -F : '$3 == "0"'

  Here the operation symbol can be used ==、>、>=、<、<=、!= Both are feasible

When compared with numbers , Use double quotation marks for numbers "" Lead up , Representation string , Without guidance, it means number

cat /etc/passwd | awk -F : '$3<="1000"'

Add up the first four lines of the user's uid

head -n4 /etc/passwd | awk -F : '{(sum=sum+$3)};END{print sum}'

  Process control

The first character of the output first field is greater than q The line of

cat /etc/passwd | awk -F ':' '{if($1 > "q"){print $1}else{print "_"}}'

 LAMP Definition

The basic definition

  • LAMP A set of free software acronyms commonly used together to run a dynamic web site or server .
  • Linux, operating system
  • Apache, Web server
  • MariaDB or MySQL, Database management system ( Or the database server )
  • PHP、PerI or Python, Scripting language


Although these open source programs themselves are not specifically designed to work with other programs , But because they're cheap and universal , This combination is coming into fashion ( majority Linux The distribution is bundled with these software ). When used together , They act like a dynamic “ Solution package ”( Solution Packages).


Apache( Transliterated as Apache ) It's the world's number one user Web Server software . It can run on almost all widely used computing platforms , Because of its cross platform and security is widely used , It's the most popular Web One of the server-side software . It's fast 、 Reliable and accessible through simple API expand , take Perl/Python Wait for the interpreter to compile into the server . There are dangerous functions that can execute code or system commands .

LAMP Installation method

install LAMP Related installation package (yum -y install httpd/mariadb/mariadb-server/php/php-mysql)

  • install httpd、mariadb、mariadb-server、php、php-mysql
  • Use yum install , Automatically resolve dependencies

If the installation process goes wrong, use :ps -ef | grep yum( Check the process )

Then find the corresponding process and close :kill -9 pid(-9 Indicates that the process is forcibly terminated )

Start the service

start-up httpd command :systemctl start httpd

start-up mariadb command :systemctl start mariadb

Set up httpd Power on and auto start command :systemctl enable httpd

Set up mariadb.service Power on and auto start command :systemctl enable mariadb.service

Visit the built-in test page , Turn off the firewall after startup :systemctl stop firewalld

Visit yourself IP Address , The following interface appears to indicate success

LAMP Modification of configuration document

stay lamp in , All website pages can only be accessed in this directory by default , The default port number is 80

cd /var/www/html

vim test1.php




Visit the test page

After modification, the default port number is 8080, And visit the test page

Get into /etc/httpd/conf/httpd.conf Under the configuration file

Use set nu According to the line Numbers

Find No 42 Line modify the port number to 8080

to open up 8080 Port number

firewall-cmd --add-port=8080/tcp --permanent

restart httpd service

systemctl restart httpd

Use 8080 Port access again

copyright notice
author[Bo childe],Please bring the original link to reprint, thank you.

Random recommended