Several common text processing tools under Linux

Source: Internet
Author: User
Tags egrep

1. Several common text-processing commands

1.cat
Cat we were one of the most common commands in early learning, but we used only the cat command itself, such as cat/etc/passwd, to view the contents of/etc/passwd, no cat option, in fact, the cat command is an option, but we do not use much, The specific parameters are as follows:
First use Vim to edit a simple text, HelloWorld, the content is HelloWorld, here just to show, the specific use of VIM will be described below.

cat /app/block/helloworld                                                                                                //查看helloworld文本内容

- e view text when displaying Liux newline characters in text $

cat /app/block/helloworld                                                                                                //显示helloworld中的换行符$

- v displays line breaks in Windows when viewing Windows text ^m
Use the RZ command to upload helloworld.txt to/app/block in the Windows system.

cat /app/block/helloworld.txt                                                                                           //显示helloworld.txt中的换行符^M

- t display tab

cat -T /app/block/helloworld                                                                                            //显示helloworld中的Tab

- N Displays the result before adding the line number

cat -n /app/block/helloworld                                                                                            //显示的helloworld中增加行号

- b Displays the result before the line number is incremented, but the blank line does not participate in the number

cat -b /app/block/helloworld                                                                                            //helloworld中的空行不编号

-S compression continuous blank line into a row
Need to add a few blank lines after the text

cat -s /app/block/helloworld                                                                                            //压缩helloworld中连续空行为一行


2.tac
The tar command has only one function, which is to display the contents of the text in reverse order

tac /app/block/helloworld                                                                                                //倒序显示helloworld

3.rev
Rev also features only one, that is, the content of the standard input is displayed backwards in each line, supporting the pipeline.

cat /app/block/helloworld|rev                                                                                          //helloworld每行倒着显示


4.head
Head has two commonly used parameters, head is the meaning of the header, is the text of the head part of the operation.
-N Prints the nth line at the beginning of the text

cat /app/block/helloworld|head -1                                                                                   //打印helloworld的第一行

- c N takes the first n bytes of a character

echo helloworld|head -c 5                                                                                              //取helloworld的前5个字节


5.tail
Tail is similar to head, there are also two commonly used parameters, but tail is the tail meaning, is to manipulate the text tail.
-N Prints the line at the end of the text

cat /app/block/helloworld|tail -1                                                                                      //打印helloworld的最后一行


- c N takes the latter n bytes of a character

echo helloworld|tail -c 5                                                                                                 //     取helloworld的后5个字节


Tail takes the last n characters containing the newline character $.
6.cut
Cut is the meaning of cut, cut the text content, the general commonly used options are four, and,-d,-f, general use

- D+ parameter 1 (d + no space in middle)-f+ parameter 2 with parameter 1 as the delimiter, cut the parameter 2 column
Hellow text

cat /app/block/helloworld|cut -d‘ ‘ -f1                                                                               //取出helloworld中以空格为间隔的第一列


The- f option allows you to specify a range and is not necessarily a parameter, such as:

cat /app/block/helloworld|cut -d‘ ‘ -f1-3, 5                                                                      //取出helloworld中以空格为间隔的第一列到第三列和第5列

- C m/m-n/m-n,k Remove the m/m-n/m-n,k character from the string

echo hi hello world i am fun|cut -c 1-3,5                                                                        //显示字符串中第1-3,和第五个字符

cut--output-delimiter= "#" output delimiter
7.past
The functions of the Paste command are used for merging, can be used alone, or can be used with options, there are two options

paste /app/block/helloworld /app/block/helloworld.txt                                                    //合并helloworld和helloworld.txt


- d ' Q ' F1 F2 merged with Q as the delimiter, Q can be any character

paste -d‘|‘ /app/block/helloworld /app/block/helloworld.txt                                              //合并helloworld和helloworld.txt,并用|作为间隔符

- s changes multiline content lines to rows

paste -s /app/block/helloworld                                                                                        //将helloworld中的多行内容变为一行


8.wc
-L Displays the number of lines of text

cat /app/block/helloworld|wc -l                                                                                         //显示helloworld的行数


-M displays the number of characters in the text

cat /app/block/helloworld |wc -m                                                                                      //显示helloworld中的字符数


-C Displays the number of bytes in the text

cat /app/block/helloworld |wc -c                                                                                       //显示helloworld中的字节数


-W Displays the number of words in the text

cat /app/block/helloworld |wc -w                                                                                      //显示helloworld中的单词数


-L Displays the length of the longest line in the text

cat /app/block/helloworld |wc -L                                                                                       //显示helloworld中最长行的长度

2. Text of the Three Musketeers grep

grep is a text-processing tool that filters text and only filters out the content, and he has these options:
The-V keyword displays rows that do not contain a key word,

cat /app/block/helloworld|grep -v hello                                                                            //过滤出不包含hello的行


-I keyword ignores the capitalization of keywords

cat /app/block/helloworld|grep -i hello                                                                            //过滤出hello,并且不区分其中字母的大小写

-N Displays the result of adding line numbers before each line

cat /app/block/helloworld |grep -n hello                                                                         //过滤出含有hello的行,并且在结果上加上行号


-C displays only the number of rows found for the result

cat /app/block/helloworld |grep -c hello                                                                          //过滤hello但不现实结果,只显示过滤出hello的行数


-O displays only keywords that match to, and does not display other content from peers

cat /app/block/helloworld |grep -o hello                                                                          //只显示过滤出的关键字hello,不现实其他的内容


-A # Displays the key line and the down n rows

cat /app/block/helloworld |grep -A2 hello                                                                       //过滤出包含hello的行以及其下一行


-B # Displays the line of keywords and the n rows up

cat /app/block/helloworld |grep -B hello                                                                          //过滤出包含hello的行以及上一行


-C # Displays key lines and n rows up and down n rows

cat /app/block/helloworld |grep -C1 hello                                                                       //过滤出包含hello的行以及其下一行和上一行


-e keyword 1-e keyword 2 ... Relationships between multiple keywords Yes or

cat /app/block/helloworld |grep -e hello -e Hello                                                             


-W keyword matches entire word

cat /app/block/helloworld |grep -w hello                                                                          //过滤出只含有hello单词的行


-e equals egrep using extended regular expressions
Plus-e or using EGREP filtering means using extended regular expressions
-F equals fgrep does not use regular expressions
Regular expressions are described in the next blog post.

Several common text processing tools under Linux

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.