linux text processing tools -1

File contents: cat, more, less

File interception: head, tail

By column extraction: cut

Sort and statistics: sort, wc

-----------------------------------------------------

cat [OPTION] ... [FILE] ... // character files for viewing

-E: display linefeed $ 

-n: to show each line are numbered           

-A: show all control characters     

-b: non-blank line number 

-s: the compressed air line into a continuous row

------------------------------------------------------

Page View

more: Paging File View

     more [OPTIONS...] FILE...   

    -d: display the page and quit tips

less: page after page to view the output file or STDIN

  When you view useful commands include:

      / Text search text

      n / N skip to the next or previous match

  less command is a command to use pager man

less
space to scroll an
Enter key scrolled one
[pagedown]: Scroll down a
[pageup]: a turning direction
may also be entered directly after the colon / highlighted keyword to search for and to be used to the n Find or search backwards before N.

------------------------------------------------------------------------

The first few lines head view files

   head failename default display the first ten lines head -n11 passwd file == head -11 passwd // specifies the first few lines of the file view

After a few lines tail view files   

        tail failename default display after ten lines of a file tail -n11 passwd == tail -11 passwd // file specifies the view after a few lines          

  tail -f catalina.out trace file new additions, commonly used in the log monitoring

---------------------------------------------------------------------------------------------

cut   in columns to extract text

  cut [OPTION]... [FILE]... 

      -d DELIMITER: delimiter specified, the default tab 

      -f FILEDS:       

#: # The first field       

# # [#]: A plurality of discrete fields, 1,3,6 e.g.     

# - #: a plurality of consecutive fields, e.g. 1-6                 

Mixed use: 1-3, 7 

       -c cutting by character 

Example:
Cut -d: -f1,3 / etc / to the passwd //: as the separator, taking the first and third column
cat / etc / passwd | cut -d : -f1,3 displays a result of this root: 0

ens33 the ifconfig | head -2 | tail -1 | TR -s "" | Cut -d "" -f1-3
the ifconfig ens33 | head -2 | tail -1 | TR -s "" | Cut -d "" -f1 , 2,3
Cut -c34-36 // taken of 34-36 characters

------------------------------------------------------------------------------------------------------

wc  Statistics

  Total word count of the total number of rows, the total number of bytes, and total number of characters 

  wc story.txt 

  39     237    1901 story.txt

  The number of bytes the number of rows of words 

       Common options

    • -l count only the number of rows
    • -w only count the total number of words
    • -c only count the total number of bytes
    • -m only count the total number of characters
    • -L length of the longest line in the display file 

--------------------------------------------------------------------------------------------------------------------

tr 

      tr '' + // the 'replaced +

      tr -s '' // compression space

       tr -d '0-9' // delete characters

      tr -dc '[0-9 \ n]' in addition to other characters // Delete

      tr -s ""% compression and replace

-----------------------------------------------------------------------

Sort text sort    according to sort column (a character, a character comparison)

Common options

  • -r perform the reverse direction (top to bottom) finishing
  • -R random ordering
  • sort -n perform numerical size
  • -f option ignores character case (fold) string
  • Repeat line option -u (unique, unique) to delete outputs
  • c -t c option is used as the field delimiter
  • -k X X column using options as a character-delimited c can be used multiple times to organize 

     echo {1..10} | tr '' '\ n' | sort -rn // the line to a digital-to-digital, and then sorted according to the number

----------------------------------------------------------------------------------------------

the uniq
the uniq commands: delete duplicate lines before and after the input contact from

    uniq [OPTION] ... [FILE] ...

      • -c: Displays the number of each row recurring
      • -d: show only duplicate rows over
      • -u: Display only never duplicate rows
      • Note: Continuous repetitive and identical side

    And the sort often used together with the command:

      sort userlist.txt | uniq -c

--------------------------------------------------------------------------------

 grep filter rows

grep [OPTIONS] PATTERN [FILE...] 

      • --color = auto: matching the color display of the text 
      • -m # # second stop after the match 
      • -v display is not matched to the line pattern 
      • -i Ignore character case 
      • -n Display line numbers match 
      • -C count the number of rows that match 
      • -o display only the matched character string 
      • -q silent mode does not output any information 
      • -A # after, the line # 
      • -B # before, before the line # 
      • -C # context, each longitudinal row # 
      • -e implement the logic or the relationship between the number of options grep -e 'cat' -e 'dog' file 
      • -w matches whole words 
      • -E use ERE 
      • -F equivalent fgrep, does not support regular expressions 
      • -f file according to the schema file processing

nmap -v -sP 172.16.128.0/24 | grep -B1 up // match the previous row containing up line
| grep -e root -e bin or comprises a plurality of condition // comprising or containing root bin
LS | grep -v [ abc] // the file name does not contain three letters abc
cat / etc / centos-release | grep -o "\ <[0-9] \ + \>" | head -1 // match a single number
[root @ centos7 ~] #grep -E "^ ([^:] +):. * \ <\ 1 $" / etc / passwd packet / / extended regular expressions

 

---------------------------------------------------------------------------------------------

Regular Expressions

Character matches :

  •   . Matches any single character
  •   [] Matches any single character within a specified range, an example: [wang] [0-9] [az] [a-zA-Z]
  •   Any single character [^] matches outside the specified range 

Match times : after the character to be used in a specified number of times specified for the preceding character to appear

  •  * Matches any number of times in front of the characters, including zero greedy mode: as long as possible match
  • * Any character of any length
  • \? Match its preceding character 0 or 1 times 
  • \ + Matches its preceding character at least once 
  • \ {N \} matches the preceding character n times 
  • \ {M, n \} foregoing character matches at least m times, n times at most 
  • \ {, N \} foregoing character match up n times 
  • \ {N, \} foregoing character match at least n times

Location anchoring : positioning to appear

  •   ^ Beginning of a line anchored mode for most of the left
  •   $ Anchored end of the line, for the rightmost pattern
  •   ^ $ For pattern matching the PATTERN entire line blank line ^ $ ^ [[: space:]] * $ blank line
  •   \ <Or \ b word-initial anchor for the left word mode ( word mode : part of a word alphanumeric underscore count, word count other line of demarcation)
  •   \> Or \ b anchor ending, the right mode for the word 
  •   \ <PATTERN \> matches the entire word 

  Packet : \ (\) of one or more characters tied together, as a whole process, such as: \ (root \) \ + 

  Or : \ |     

  Example: a \ | ba or b C \ | cat C or cat \ (C \ | c \) at Cat or cat 

             

 

 Extended regular expressions

  egrep = grep -E 

   And the difference between the basic regular expression is, there are some places do not slash (\),

  Basic regular expressions need to use local slash: \ (\) \ <\> \ {\} \ + \?

  Extended regular expressions: \ <\>

 

ifconfig | grep -oE "(2 [ 0-4] [0-9] | 25 [0-5] | 1 [0-9] [0-9] | [1-9] [0-9]?) (\ (2 [0-4] [ 0-9] |. 25 [0-5] | 1 [0-9] [0-9] |? [1-9] [0-9])) {3 } "// extended regular expression matching ip

ifconfig |grep -Po "((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)"     //perl正则表达式匹配ip

  vim %s#^\(/download.*\)/media/\(.*\)\..*$#\1/mp3/\2.mp3    // vim 搜索替代 分组

  

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/hxfcodelife/p/11951610.html