Use of sed and awk under linux

Posted by rantsh on Mon, 29 Jul 2019 03:07:32 +0200

Article Directory

sed:

Common options:

  • -n: Use silent mode.In general sed usage, all data from STDIN is typically listed on the screen.However, if the -n parameter is added, only the line (or action) that has been specifically treated by sed will be listed.
  • -e: edit sed actions directly in instruction line mode;
  • -f: directly write sed's actions in a file, -f filename can execute sed's actions in filename;
  • -r:sed's actions support the grammar of extended normal representations.(preset is basic formal representation)
  • -i: Modify the contents of the file read directly instead of being output by the screen.

Common commands:

  • A: new, a can be followed by strings that will appear on a new line (next line now)~
  • c: instead, c can be followed by strings, which can replace lines between N1 and n2!
  • D: Delete, because it is deleted, so there is usually no click after d;
  • i: Insert, i can be followed by strings that will appear on a new line (the previous line currently);
  • p: Printing, also printing the selected data.Normally p works with the parameter sed-n ~
  • S: Substitution. Substitution can be done directly!Usually the actions of this s can be matched with regular representations!For example, 1,20s/old/new/g is it!
Delete a row
[root@localhost ruby] # sed '1d' ab              #Delete first line 
[root@localhost ruby] # sed '$d' ab              #Delete last line
[root@localhost ruby] # sed '1,2d' ab           #Delete first to second lines
[root@localhost ruby] # sed '2,$d' ab           #Delete the second to last line

//Show a row
[root@localhost ruby] # sed -n '1p' ab           #Show the first line 
[root@localhost ruby] # sed -n '$p' ab           #Show last line
[root@localhost ruby] # sed -n '1,2p' ab        #Show first to second lines
[root@localhost ruby] # sed -n '2,$p' ab        #Show second to last lines

//Query using patterns
[root@localhost ruby] # sed -n '/ruby/p' ab    #Query includes all rows where the keyword ruby is located
[root@localhost ruby] # sed -n '/\$/p' ab        #Queries include all rows where the keyword $is located, masking special meaning with backslashes\

//Add one or more lines of string
[root@localhost ruby]# cat ab
Hello!
ruby is me,welcome to my blog.
end
[root@localhost ruby] # sed '1a drink tea' ab  #Add string after first line"drink tea"
Hello!
drink tea
ruby is me,welcome to my blog. 
end
[root@localhost ruby] # sed '1,3a drink tea' ab #Add string after first to third line"drink tea"
Hello!
drink tea
ruby is me,welcome to my blog.
drink tea
end
drink tea
[root@localhost ruby] # sed '1a drink tea\nor coffee' ab   #Add multiple lines after the first line, using the newline character\n
Hello!
drink tea
or coffee
ruby is me,welcome to my blog.
end

//Replace one or more lines
[root@localhost ruby] # sed '1c Hi' ab                #Replace the first line with Hi
Hi
ruby is me,welcome to my blog.
end
[root@localhost ruby] # sed '1,2c Hi' ab             #Replace lines 1 to 2 with Hi
Hi
end

//Replace a part of a line
//Format:sed 's/String to replace/New string/g'   (Strings to be replaced can be used with regular expressions)
[root@localhost ruby] # sed -n '/ruby/p' ab | sed 's/ruby/bird/g'    #Replace ruby with bird
[root@localhost ruby] # sed -n '/ruby/p' ab | sed 's/ruby//g'        #Remove ruby

//insert
[root@localhost ruby] # sed -i '$a bye' ab         #Direct input on last line in file ab"bye"
[root@localhost ruby]# cat ab
Hello!
ruby is me,welcome to my blog.
end
bye

//Delete matching rows

sed -i '/Match string/d'  filename  (Note: If the matching string is a variable,''is required instead of''. Remember as if)

//Replace a string in a matching line

sed -i '/Match string/s/Replace Source String/Replace Target String/g' filename

awk

awk '{pattern + action}' {filenames}

Although the operation may be complex, the syntax is always the same, where the pattern represents what AWK looks for in the data, and the action is a series of commands that are executed when a match is found.Curly brackets ({}) do not always appear in the program, but they are used to group a series of instructions according to a specific pattern.Patterns are regular expressions to be represented, enclosed in slashes.
The basic function of awk language is to browse and extract information in files or strings based on specified rules, after which awk can extract information for other text operations.Complete awk scripts are often used to format information in text files.
Usually, awk is processed as a unit of file behavior.Awk receives each line of the file and executes the command to process the text.

ubuntu@ubuntu:~/chat-project/test$ last -n 5
ubuntu   pts/9        192.168.43.254   Sun Jul 28 17:03   still logged in
ubuntu   pts/9        192.168.43.254   Sun Jul 28 04:37 - 04:37  (00:00)
ubuntu   pts/9        192.168.43.254   Sun Jul 28 04:37 - 04:37  (00:00)
ubuntu   pts/9        192.168.43.254   Sun Jul 28 04:37 - 04:37  (00:00)
ubuntu   pts/9        192.168.43.254   Sun Jul 28 04:37 - 04:37  (00:00)

ubuntu@ubuntu:~/chat-project/test$ last -n 5 | awk '{print $1}'
ubuntu
ubuntu
ubuntu
ubuntu
ubuntu

ubuntu@ubuntu:~/chat-project/test$ head /etc/passwd | awk -F ':' '{print $1}'
root
daemon
bin
sys
sync
games
man
lp
mail
news

ubuntu@ubuntu:~/chat-project/test$ head /etc/passwd | awk -F ':' 'BEGIN {print "hello world"} {print $1, $7} END { print "end"}'
hello world
root /bin/bash
daemon /usr/sbin/nologin
bin /usr/sbin/nologin
sys /usr/sbin/nologin
sync /bin/sync
games /usr/sbin/nologin
man /usr/sbin/nologin
lp /usr/sbin/nologin
mail /usr/sbin/nologin
news /usr/sbin/nologin
end

The awk workflow is as follows: BEGING is executed first, then the file is read, a record is read with a/n line break, the record is divided into fields by the specified field separator, the field is filled, $0 represents all fields, 1 represents the first field, 1 represents the first field, n represents the nth field, and the execution mode beginsThe corresponding action.Then start reading the second record.... until all the records are read, and finally perform the END operation.

search root Rows in
ubuntu@ubuntu:~/chat-project/test$ awk -F: '/root/' /etc/passwd
root:x:0:0:root:/root:/bin/bash

//Show bash information for root row
ubuntu@ubuntu:~/chat-project/test$ awk -F: '/root/{print $7}' /etc/passwd
/bin/bash

Topics: Ruby Ubuntu