Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What Linux commands should every programmer know?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces what Linux commands every programmer should know, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

Let's start by dealing with some data. Suppose we have two files that record the order list and the order processing result, respectively.

Order.out.log 8:22:19 111,1, Patterns of Enterprise Architecture, Kindle edition, 39.99 8:23:45 112,1, 喜悦 of Clojure, Hardcover, 29.99 8:24:19 11313,-1, Patterns of Enterprise Architecture, Kindle edition, 39.99 order.in.log 8:22:20 11111112Order Complete 8:23:50, Order sent to fulfillment 8:24:20 113, Refund sent to processingcat

Cat-Connect the file and output the result

The cat command is very simple, as you can see from the following example.

Jfields$ cat order.out.log 8:22:19 111,1, Patterns of Enterprise Architecture, Kindle edition, 39.998 jfields$ cat order.out.log 231121,1, 喜悦 of Clojure, Hardcover, 29.9982419113,-1, Patterns of Enterprise Architecture, Kindle edition, 39.99

As described in its description, you can use it to connect multiple files.

Jfields$ cat order.* 8:22:20 111,1121,113,111,1, Patterns of Enterprise Architecture, Kindle edition, 39.998 Order Complete8:23:50 23 jfields$ cat order.* 11212, 1, 喜悦 of Clojure, Hardcover, 29.99824 Refund sent to processing8:22:19 19113,-1, Patterns of Enterprise Architecture, Kindle edition, 39.99

If you want to see the contents of these log files, you can connect them and output them to standard output, as shown in the above example. This is useful, but the output can be more logical.

Sort

Sort-text in the file is sorted by line

At this point, the sort command is obviously your choice.

Jfields$ cat order.* | sort8:22:19 111,1, Patterns of Enterprise Architecture, Kindle edition, 39.998 Patterns of Enterprise Architecture 20111, Order Complete8:23:45 1121,1, 喜悦 of Clojure, Hardcover, 29.998 Patterns of Enterprise Architecture 2512, Order sent to fulfillment8:24:19 113,-1, Patterns of Enterprise Architecture, Kindle edition, 39.998 24 Patterns of Enterprise Architecture 20113, Refund sent to processing

As the example above shows, the data in the file has been sorted. For some small files, you can read the whole file to deal with them, however, real log files usually have a lot of content, you can not ignore this situation. At this point, you should consider filtering out some content and passing the content after cat and sort to the filtering tool through the pipeline.

Grep

Grep, egrep, fgrep-print out lines of text that match the criteria

Suppose we are only interested in the order for the book Patterns of Enterprise Architecture. With grep, we can restrict the output of orders that contain Patterns characters.

Jfields$ cat order.* | sort | grep Patterns8:22:19 11111,1, Patterns of Enterprise Architecture, Kindle edition, 39.99824 Patterns of Enterprise Architecture 19113,-1, Patterns of Enterprise Architecture, Kindle edition, 39.99

Suppose there is something wrong with the refund order 113 and you want to see all the relevant orders-you need to use grep again.

Jfields$ cat order.* | sort | grep ":\ d\ d113," 8:24:19 113,- 1, Patterns of Enterprise Architecture, Kindle edition, 39.99824 grep 20113, Refund sent to processing

You will find that there are some other matching patterns on grep besides "113th". This is because 113 can also match the bibliography or price, and with extra characters, we can find exactly what we want.

Now that we know the details of the returns, we would also like to know the daily sales and the total amount of refunds. But we only care about the information of Patterns of Enterprise Architecture, and we only care about quantity and price. What I'm going to do now is to remove any information that we don't care about.

Cut

Cut-deletes some areas on the character line in the file

To use grep again, we use grep to filter out the rows we want. With the row information we want, we can cut them into small segments and delete unwanted parts of the data.

Jfields$ cat order.* | sort | grep Patterns8:22:19 111,1, Patterns of Enterprise Architecture, Kindle edition, 39.998Para24Gan19113,-1, Patterns of Enterprise Architecture, Kindle edition, 39.99jfields$ cat order.* | sort | grep Patterns | cut-d ","-f2prit 5 1,39.99-1,39.99

Now, we reduce the data to the form we want to calculate, and paste the data into Excel to get the results immediately.

Cut is used to subtract information and simplify tasks, but we usually have a more complex form for output. Suppose we also need to know the ID of the order, which can be used to correlate other relevant information. We can get ID information with cut, but we want to put ID on the line * * and wrap it in single quotation marks.

Sed

Sed-A stream editor. It is used to perform basic text transformations on the input stream.

The following example shows how to transform our file lines with the sed command, and then we use cut to remove useless information.

| jfields$ cat order.* | sort | grep Patterns\ > | sed s / "[0-9\:] *\ ([0-9] *\)\,\ (.*\)" / "\ 2,'\ 1'" / 1, Patterns of Enterprise Architecture, Kindle edition, 39.99,' 111mm color 1, Patterns of Enterprise Architecture, Kindle edition, 39.99 '113mm lmpCollejfields01grep Patterns jfields$ cat order.* | sort | grep Patterns\ > | sed s / "[0-9\:] *\ ([0-9] *\)\,\ (. *\)" / "\ 2,'\ 1'" / | cut-d ","-f1Magne4, 51,39.99, '111humor: 1,39.99,' 113'

Let's say a few more words about the regular expressions used in the example, but it's not complicated. Regular expressions do the following things

Delete timestamp

Capture order number

Delete commas and spaces after the order number

Capture the remaining line information

The quotation marks and backslashes are a bit messy, but they must be used when using the command line.

Once we have captured the data we want, we can use\ 1 &\ 2 to store them and export them to the format we want. We also added the required single quotation marks and a comma to keep the format consistent. Use the cut command to delete unnecessary data.

Now we have a problem. We have shown above how to reduce the log file to a more concise order form, but our finance department needs to know what books are in the order.

Uniq

Uniq-Delete duplicate lines

The following example shows how to filter out book-related transactions, delete unwanted information, and get a non-repetitive message.

Jfields$ cat order.out.log | grep "\ (Kindle\ | Hardcover\)" | cut-d ","-f3 | sort | uniq-c 1 喜悦 of Clojure 2 Patterns of Enterprise Architecture

It seems to be a very simple task.

These are good commands, but only if you can find the file you want. Sometimes you will find some files hidden in a deep folder and you have no idea where they are. But if you know the name of the file you're looking for, it won't be a problem for you.

Find

Find-search for files in the file directory

In the above example, we processed the order.in.log and order.out.log files. These two files are in my home directory. The following example will show you how to find such files in a deep directory structure.

Jfields$ find / Users-name "order*" Users/jfields/order.in.logUsers/jfields/order.out.log

The find command has a lot of other parameters, but this is all I need 99% of the time.

With a simple line, you can find the file you want, and then you can view it with cat and trim it with cut. But when the files are very small, you can pipe them to the screen, but when the files are large enough to exceed the screen, you should probably pipe them to the less command.

Less

Less-move forward or backward in the file

Let's go back to the simple cat | sort example. The following command outputs the merged and sorted content to the less command. In the less command, use "/" to perform a forward search, and use "?" Command to perform a backward search. The search condition is a regular expression.

Jfields$ cat order* | sort | less

If you use / 113.orders in the less command, the information for all 113orders will be highlighted. You can also try?. * 112, all timestamps related to order 112 will be highlighted. * you can use'q' to exit the less command.

Thank you for reading this article carefully. I hope the article "what Linux commands every programmer should know" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report