Linux command head, tail, tr, sort, uniq, grep

Source: Internet
Author: User

head [filename]head-n [filename] first one  lines  head-c [filename]  

Head defaults to show the first 10 rows.

tail [filename]tail-n [filename], last  lines  tail-c [filename], last  characters  

If the 第10-22 line to display text can be used:

1 head-n22 | Tail-n12

The use of TR is:

$ tr [OPTION] SET1 [SET2]

If both SET1 and SET2 are given, and option is not-D, then the elements in the first collection are replaced with the elements in the second collection.

$ TR A-Z a-zthegeekstuffthegeekstuff

If you need to replace the space with a tab, if there are contiguous spaces, you need to add the-s option, otherwise the default is to replace each space with a tab:

$ echo "This was for   testing" | tr-s [: space:] ' \ t ' thisisfortesting

If you want to delete an element, you can use (such as deleting all numbers):

$ echo "My username is 432234" | TR-D [:d igit:]my username is

If you need to remove characters other than numbers, you can use the-C option:

$ echo "My username is 432234" | TR-CD [:d igit:]432234

If you want to merge all the rows of a file into one line, you can use:

$ Tr-s ' \ n ' < file.txt

Sort has several common options:

-N Sort by number

-R Reverse Order

-K m,n sorted by M-n Field

-t change the file delimiter

To sort a tab-delimited text by a second field, you can use:

$ sort-k2,2-t $ ' \ t ' phonebook  Doe,    John555-1234 Fogarty,    Suzie555-2314 Doe, jane< c6/>555-3214 Avery, Cory    555-4132 Smith, Brett    555-4321

The tab is represented here with $ ' \ t '

Several common options for command Uniq are:

-C output Number of consecutive occurrences per line

-D outputs contiguous rows only once for rows that occur more than once

The difference between-D and-D is that the full output of the qualifying row

-U only outputs rows that do not appear consecutively

-I ignores case when comparing

-W N compares only the first n characters

-F N ignores comparison of top N filed

-S n ignores comparison of the first n characters

grep Common methods:

If you are looking for a string in a file, you can use:

grep "literal_string" filename

If the specified string is found in multiple files:

grep "string" File_pattern

-I option ignores case

To find multiple strings in a file:

grep "REGEX" filename

The-w option is used to find only the specified word, ignoring word as a string

-c option to see the number of occurrences

The-v option is used to output rows that do not appear with the specified string

The-e option can be used to specify multiple strings:

$ grep-v-E "a"-E "B"-E "C" test-file.txt

The-l (lowercase l) option is used to output the file name that contains the specified string:

 This demo_*demo_filedemo_file1

In addition, there are options such as-a,-b,-c.

Linux command head, tail, tr, sort, uniq, grep

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.