Each developer comes to a certain stage of their professional life and will find himself looking for information about Linux. I am not an expert in this field. But with the following 8 commands, I can almost get anything I need.
Note: The following commands have many extensions to the document, the knowledge presented in the blog my most commonly used commands, usage. If you don't understand Linux commands, this post will give you a little guidance.
Let's take some text for example. Suppose we have 2 files, which have orders about where to place the third party and send a response.
Order.out.log
8:22:19, 1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
8:23:45 112, 1, Joy of Clojure, hardcover, 29.99
8:24:19 113,-1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
Order.in.log
8:22:20, Order Complete
8:23:50 112, order sent to fulfillment
8:24:20 113, Refund sent to processing
Cat
– Append files and print on standard output
jfields$ Cat Order.out.log
8:22:19, 1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
8:23:45 112, 1, Joy of Clojure, hardcover, 29.99
8:24:19 113,-1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
As his name says, you can concatenate multiple files
jfields$ Cat order.*
8:22:20, Order Complete
8:23:50 112, order sent to fulfillment
8:24:20 113, Refund sent to processing
8:22:19, 1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
8:23:45 112, 1, Joy of Clojure, hardcover, 29.99
8:24:19 113,-1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
See the effect, but we can improve its readability.
Sort
– Sort the text files by row, where sorting is a good choice
jfields$ Cat order.* | Sort
8:22:19, 1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
8:22:20, Order Complete
8:23:45 112, 1, Joy of Clojure, hardcover, 29.99
8:23:50 112, order sent to fulfillment
8:24:19 113,-1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
8:24:20 113, Refund sent to processing
It shows the effect we want to see, but it's just a small file. And the real data is very large, and some of the data you do not want to do?
Grep
grep, Egrep, fgrep– for matching output
Suppose I'm only interested in giving orders to POFEAA, and using grep can do it.
jfields$ Cat order.* | Sort | grep Patterns
8:22:19, 1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
8:24:19 113,-1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
Suppose there are some problems with order 113 and you want to see all the order information about 113. Yes, grep can help you.
jfields$ Cat order.* | Sort | grep ":d D 113,"
8:24:19 113,-1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
8:24:20 113, Refund sent to processing
You will find that there are more than 113 of them in the expression, because 113 may also appear in the price, or in the product, and this is strictly limited to the search results.
Now that we have sent the information for the return order, we will send the sales statistics to the accountant daily. They ask for every POFEAA item, but they only care about the quantity and the price we're going to put
Cut off the unwanted parts.
Cut-and
– Remove part from each line of the file
You should still use grep first.
jfields$ Cat order.* | Sort | grep Patterns
8:22:19, 1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
8:24:19 113,-1, Patterns of Enterprise Architecture, Kindle Edition, 39.99
jfields$ Cat order.* | Sort | grep Patterns | Cut-d ","-f2,5
1, 39.99
-1, 39.99
We have reduced the data to make the accounting at a glance.
Suppose the accountant wants to take the order ID as a reference, put it at the end of each line, and use single quotes.
Sed
– The Flow editor. Used to process text conversions.
The following example shows how to use it to do the data we want.
jfields$ Cat order.* | Sort | grep Patterns
>| Sed s/"[0-9:]* ([0-9]*), (. *)"/"2, ' 1 '"/
1, Patterns of Enterprise Architecture, Kindle Edition, 39.99, ' 111 '
-1, Patterns of Enterprise Architecture, Kindle Edition, 39.99, ' 113 '
lmp-jfields01:~ jfields$ Cat order.* | Sort | grep Patterns
>| Sed s/"[0-9:]* ([0-9]*), (. *)"/"2, ' 1 '"/| Cut-d ","-f1,4,5
1, 39.99, ' 111 '
-1, 39.99, ' 113 '
This is a regular expression, but nothing complicated. Do the following things
1. Delete Time
2. Capture Order Number
3. Delete the comma and the space after the order number
4. Capture the remainder of this row
Once we see the data we need, we can use 1&2 to let the output data conform to our format requirements.
Uniq
– Remove Duplicate rows
The following example shows how to grep the only related transaction, cut unnecessary information, and get the count.
jfields$ Cat Order.out.log | grep "(kindle| Hardcover) "| Cut-d ","-f3 | Sort | Uniq-c
1 Joy of Clojure
2 Patterns of Enterprise architecture
jfields$ Cat Order.out.log | grep "(kindle| Hardcover) "| Cut-d ","-f3 | Sort | Uniq
Joy of Clojure
Patterns of Enterprise Architecture
Find
-Find a file in the directory
Assuming that these 2 text files exist in our home directory, we don't have to know their full names.
jfields$ find/users-name "order*"
Users/jfields/order.in.log
Users/jfields/order.out.log
Of course there are many options, but 99% of the cases I do.
Less
– Move backwards through a file
Let's go back to the simplest example of cat|sort. You can search forward using "/", Backward use "?", 2 can use regular expression.
jfields$ Cat order* | Sort | Less
You can try/113.*, which will highlight order 113. You can use? *112 will also highlight order 112 and you can exit with ' Q '.
Linux commands are rich, and some people have a headache. These commands should help you do most of the text work without having to hand in your scripting language.
Original: Jayfields compilation: Big Love Data