Linux commands that Java developers must master

Posted by Thundarfoot on Tue, 23 Jul 2019 04:03:57 +0200

Be a positive person
Code, fix bug s, improve yourself
I have a paradise for programming, spring blossoms!

Learning should be happy. In this paradise, I try my best to explain knowledge or technology with simple and easy-to-understand (funny and interesting) expressions, and make the learning journey full of fun. This is the beginning of blog writing.

I've written two introductions to Linux commands.
1,Linux commands that Java developers must master (1)
2,Linux commands that Java developers must master (2)
Some time ago, I saw some movies of Harry Potter. It suddenly occurred to me that the command of Linux is as magical as the magic in Harry Potter movies. So learn these commands of Linux, we can have magic like Harry Potter, the magic world is still very interesting, it is full of joy. This article talks about three commands of Linux. These three commands are grep, sed and awk. They are also three important commands in Linux. After learning these three commands, we also use three more powerful magic.

Beginning with a narrative:

There is a magic school with three magic. These three magics have always been considered as the three most important magic in the school of magic, also known as the Three Swordsmen. Let's start the study of these three magic.

Magic Map:

1. Text filter-grep

The grep command is used to find qualified strings in files. It is a powerful text search tool that supports filtering conditions written by regular expressions or characters and basic text characters.

The grep instruction is used to find a file whose content contains the specified template style. If the content of a file conforms to the specified template style, the preset grep instruction will display the column containing the template style. Simply put, the user can specify a "pattern" (option) to check the matching of the target text and print the matched lines.

Note: When entering string parameters in the grep command, it's better to enclose them in double quotes. Double quotes can also be used when calling variables. Single quotation marks should be used in invoking pattern matching.

  • Common command format
# grep matching condition target file / directory
 Optional parameters:
- c or -- count: Calculates the number of columns that match the style.
- i or -- ignore-case: Ignore the difference between case and case of characters
 - l or -- file-with-matches: List file names that match the specified style of file content
 - n or -- line-number: Prior to displaying the styled row, indicate the column number of that row
 - s: does not display error messages that do not exist or do not match text.
- v or -- revert-match: Displays all lines that do not contain matched text
 - r or -- recursive: The effect of this parameter is the same as that of the specified "- d recurse" parameter
 - E or -- extended-regexp: Use a generic representation of styles as extensions
....

Description: In regular expressions
 ^ Match string start
 $: Matches the end of the string
 * Characters appear [0-any time]
\?: Characters appear [0-1]
\+ Characters appear [1-any time]
  • Simple examples
# 1. Create a new busyday busyday 1 busyday 2 file 
[root@localhost learn_grep]# touch busyday
[root@localhost learn_grep]# vim busyday 
# Similar to other busyday1 busyday2, the contents of the file are brief
[root@localhost learn_grep]# cat busyday 
abc
123abdef
789happy
hjk567
hello123
greptest

# 2. Query the file where happy is located
[root@localhost learn_grep]# grep "happy" busyday*
busyday:789happy
busyday1:123happy
busyday2:456happy

# 3. Statistics of the occurrence of happy's file content
[root@localhost learn_grep]# grep -c "happy" busyday*
busyday:1
busyday1:1
busyday2:1

# 4. Display all lines matching happy on the third line
[root@localhost learn_grep]# grep -n "happy" busyday*
busyday:3:789happy
busyday1:3:123happy
busyday2:3:456happy

# 5. Display all lines that do not contain happy
[root@localhost learn_grep]# grep -v "happy" busyday*
busyday:abc
busyday:123abdef
busyday:hjk567
busyday:hello123
busyday:greptest
busyday:
busyday1:abc
busyday1:4567abdef
busyday1:hjk321
busyday1:hello987
busyday2:abc
busyday2:9876abdef
busyday2:hjk4321
busyday2:hello0980

# 6. Find 123 happy by precise matching 
[root@localhost learn_grep]# grep "123happy" busyday*
busyday1:123happy

# 7. Regular matching, find all 123 beginning
[root@localhost learn_grep]# grep -E "^123*" busyday*
busyday:123abdef
busyday1:123happy

There are many more, do magic exercises on your own!

2. Character Processor-sed

The sed command uses script to process text files. Sed can process and edit text files according to script instructions. It is easy to use regular expressions perfectly, and its functions are remarkable.

  • Basic Grammar
sed [- parameter]'command'text

Description of parameters:
- e < script > or - expression= < script > processes the input text file with the script specified in the option.
F < script file > or - file = < script file > processes the input text file with the script file specified in the option.
- h or - help shows help.
- n or - quiet or - silent only shows the results of script processing.
V or version displays version information.
Action description:
A: Added, a can be followed by strings, and these strings will appear in a new line (the next line at present)~
c: Instead, strings can be used at the back of c. These strings can replace lines between N1 and n2.
d: Delete, because it is deleted, so there is usually no drum after d.
i: Insert, i can be followed by strings, and these strings will appear on a new line (the current previous line);
p: Print, or print out some selected data. Usually p runs with parameter sed-n
 S: Substitution. Substitution can be done directly. Usually the action of this s can be matched with the normal representation! For example, 1,20s/old/new/g
  • Example
# 1. sed's/substituted strings/new strings/g'
# 1. Replace aflyun with "paradise of java programming technology"
[root@localhost learn_awk]# Echo "aflyun 0315" | sed's / aflyun / Java programming technology paradise /' 
java Programming Technology Paradise 0315
[root@localhost learn_awk]# Echo "aflyun0315" | sed's * aflyun * Java programming Paradise *'
java Programming Technology Paradise 0315
# 2. Specify which row of the input stream is edited. If omitted, the default is to edit all rows.
# 2. Replace aflyun with "paradise of java programming technology"
[root@localhost learn_awk]# cat aflyun 
flyun110
Hello aflyun
aflyun0315
#  Replace line 2
[root@localhost learn_awk]# Cat aflyun | sed'2s / aflyun / Java programming Paradise /'
aflyun110
Hello java Paradise of Programming Technology
aflyun0315
#Replace all rows (no address, default)
[root@localhost learn_awk]# Cat aflyun | sed's / aflyun / Java programming technology paradise /'
java Programming Technology Paradise 110
Hello java Paradise of Programming Technology
java Programming Technology Paradise 0315

# Basic Editorial Order
[root@localhost learn_awk]# cat aflyun 
aflyun110
Hello aflyun
aflyun0315
# 1. Insert command
[root@localhost learn_awk]# Sed'2i Java programming paradise'aflyun 
aflyun110
java Paradise of Programming Technology
Hello aflyun
aflyun0315

# 2. Delete commands
[root@localhost learn_awk]# sed '2d' aflyun 
aflyun110
aflyun0315

# Other similar

# IV. Replacing the Content of Documents
# Sed-i's/aflyun/java Programming Technology Paradise/g'filename    
# If there is no g tag, only the first matching aflyun in each line is replaced by a java programming Paradise
[root@localhost learn_awk]# cat aflyun 
aflyun110
Hello aflyun
aflyun0315
[root@localhost learn_awk]# Sed-i's/aflyun/java programming technology paradise/g'aflyun 
[root@localhost learn_awk]# cat aflyun 
java Programming Technology Paradise 110
Hello java Paradise of Programming Technology
java Programming Technology Paradise 0315

3. Report Generator-awk

AWK is a language for processing text files and a powerful text analysis tool.

AWK is called AWK because it takes the first characters of three founders, Alfred Aho, Peter Weinberger and Brian Kernighan's Family Name. It is also known as the eldest of the three swordsmen. If the sword goes out of sheath, it will be extraordinary. Mastering awk will make your job bigger.

Awk processing mechanism: awk will process text line by line, supporting some preparatory work before processing the first line, and some summary work after processing the last line, which are reflected in the command format as follows:

BEGIN {}: Executed before reading the first line of text, commonly used to initialize operations

{}: Line-by-line processing, line-by-line text reading and corresponding processing, is the most common editing instruction block.

END {}: Executed after processing the last line of text, usually used to output processing results

Common awk built-in variables

Variable name Explain
$0 Current record
$1~$n The nth field of the current record
FS Input field delimiter, default line break
NF The number of fields in the current record is the number of columns
NR The number of records that have been read is the line number, starting from 1.
OFS Input field separator, default space
ORS Output record delimiter, default newline character
  • Basic usage 1
awk '{[pattern] action}' {filenames}   # Line matching statement awk''can only be used with single quotation marks
  • Example
# Add a happy.log, content
2 this is a test
3 Are you like awk
This's a test
10 There are orange,apple,mongo
# Each line is divided into spaces or TAB s, with 2 or 4 items in the output text.
[root@localhost learn_awk]#  awk '{print $1,$4}' happy.log 
2 a
3 like
This's 
10 orange,apple,mongo
 # Format output
[root@localhost learn_awk]# awk '{printf "%-8s %-10s\n",$1,$4}' happy.log 
2        a         
3        like      
This's             
10       orange,apple,mongo
  • Basic usage 2
awk -F  #- F is equivalent to the built-in variable FS, specifying the split character
  • Example
[root@localhost learn_awk]# cat happy.log 
2 this is a test
3 Are you like awk
This's a test
10 There are orange,apple,mongo
# Use "," segmentation
[root@localhost learn_awk]# awk -F, '{print $1,$2}'   happy.log 
2 this is a test 
3 Are you like awk 
This's a test 
10 There are orange apple # Notice that there are no commas here.

# Or use built-in variables
[root@localhost learn_awk]# awk 'BEGIN{FS=","} {print $1,$2}'   happy.log 
2 this is a test 
3 Are you like awk 
This's a test 
10 There are orange apple # Notice that there are no commas here.

# Use multiple separators. First use space partitioning, then use "," partitioning for the result of partitioning.
[root@localhost learn_awk]# awk -F '[ ,]'  '{print $1,$2,$5}'   happy.log 
2 this test
3 Are awk
This's a 
10 There apple

# $1, $2, $5 outputs the first, second, and fifth, and no outputs. For example, This's a test, no $5.

awk is very powerful and has many functions. Here we can only briefly introduce it. This magic learning can not be achieved in two days.

Conclusion: Today's three orders are three magic. And we learn commands, just like those students who study magic in Harry Potter's School of Witchcraft and Magic, some of them have high comprehension ability. If he wants to learn a magic, he just needs to practice it. Some of them may have relatively low comprehension ability and need him to practice it constantly in school. Only then can we master these magic. But no matter the level of comprehension, as long as we keep practicing, even if we can not practice the most powerful magic, it will not be too bad in the end. Learning Linux commands is the same. Learn Linux commands as magic.

Recommended reading

Linux commands that Java developers must master (1)
Linux commands that Java developers must master (2)
Linux commands that Java developers must master (3)
Two Linux magic tools that Java developers must master (4)
Linux Commands Java Developers Must Master - Learn to Use (V)

Thank you for your reading. If you think this blog is helpful to you, please comment or like it, so that more people can see it! Wish you a happy day!

<center> <font color='red'> Java programming technology paradise </font>: a public number to share programming knowledge. Learn with the elder driver of the park, and make a little progress every day. </center>
<p/>
Font color='blue'> Scanning attention, background reply to [resources], get the collection of dry goods! 99.9% of the partners liked </font> </center>.
<p/>

Feiyun </center> is getting better every day

Topics: Linux Java Programming Spring