Let's take some text as an example. Suppose we have two files with orders regarding third-party placement and sending responses.
Cat order. Out. Log
8:22:19 111, 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
Cat order. In. Log
8:22:20 111, order complete
8:23:50 112, order sent to fulfillment
8:24:20 113, refund sent to processing
Cat
-Append the file and print it on the standard output.
Jfields $ cat order. Out. Log
8:22:19 111, 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 111, order complete
8:23:50 112, order sent to fulfillment
8:24:20 113, refund sent to processing
8:22:19 111, 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
We can see the effect, but we can improve its readability.
Sort
-Sort text files in rows. Sorting is a good option here.
Jfields $ cat order. * | sort
8:22:19 111, 1, patterns of Enterprise Architecture, kindle edition, 39.99
8:22:20 111, 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
The above shows what we want to see, but this is just a small file. The real data is very big, and some are the data you don't want. What should I do?
Grep
Grep, egrep, fgrep-for matching output
Assume that I only care about the order for pofeaa and use grep.
Jfields $ cat order. * | sort | grep patterns
8:22:19 111, 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 in order 113, and you want to see all the order information about 113. That's right. grep can help you.
Jfields $ cat order. * | sort | grep ": \ 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 expressions, because 113 may also appear in the price or product. In this way, the search results are strictly restricted.
Now we have sent out the information of the return order, and we will also send sales statistics to the accountant every day. They require each pofeaa project, but they only care about the quantity and price.
Delete unnecessary parts.
Cut
-Delete a part from each row of the file
Use grep first.
Jfields $ cat order. * | sort | grep patterns
8:22:19 111, 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 clear at a glance.
Assume that the accountant wants to use the order ID as a reference, put it at the end of each row, and use single quotes.
Sed
-Stream editor. Used to process text conversion.
The following example shows how to use it to implement the desired data.
Jfields $ cat order. * | sort | grep patterns \
> | Sed S/"[0-9 \:] * \ ([0-9] * \) \, \ (. * \) "/" \ 2, '\ 1 ′"/
1, patterns of Enterprise Architecture, kindle edition, 39.99, '000000 ′
-1, patterns of Enterprise Architecture, kindle edition, 39.99, '000000 ′
Lmp-jfields01 :~ Jfields $ cat order. * | sort | grep patterns \
> | Sed S/"[0-9 \:] * \ ([0-9] * \) \, \ (. * \) "/" \ 2, '\ 1' "/| cut-D", "-F1, 4,5
1, 39.99, '000000 ′
-1, 39.99, '000000 ′
This is a regular expression, but it is not complex. Do the following:
1. Deletion time
2. Capture the order number
3. Delete the Space following the comma and order number.
4. Capture the rest of this row
Once we see the data we need, we can use \ 1 & \ 2 to make the output data meet our format requirements.
Uniq
-Remove duplicate rows
The following example shows how to grep the unique 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
Suppose these two text files exist in our home directory, so 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 I do this in 99% cases.
Less
-Move forward and backward in a file
Let's go back to the simplest cat | sort example. You can search forward using "/" and backward using "?", Both can use regular expressions.
Jfields $ cat Order * | sort | less
You can try/113. *, which will highlight order 113. Can you use ?. * 112, the order 112 is also highlighted. you can exit with 'q.
Linux commands are rich and some people have a headache. These commands can help you complete most of the text work without being handed over to your scripting language.
Original article: http://blog.jayfields.com/2012/08/8-linux-commands-every-developer-should.html