Sunday, February 12, 2012

sed and awk usage


# # # # # # # # # # # # # # # # # #
A description of the basic special characters
# # # # # # # # # # # # # # # # # #

  . (Dot) 
      Newline character (\ n, newline) would match any character except:
      However, awk on the line (\ n) does not specify only the newline character (\ n) is the match come.

 * (Asterisk)
      General characters that match the regular expression does not have a limit on the number of characters (0 to infinity).

 [...] (Brackets)
     All characters in parentheses find a character that matches. For example, [aE] if he, a look or E character.
     - (Hyphen) when using a specific character to a certain point between all the characters are.
     In parentheses, the function of all meta-characters lose their will. ^ Just as the character with the exception of the features there are also.
     ^ (Caret) inside the parentheses! (Not) have the capability. For example, [^] is the 'non-whitespace character is any character point.

 ^ (Caret)
     The beginning of each line indicates. This is not the first character is also important to know.
     Only the beginning of each line will be called. The following is not a newline character in awk.

 $ (Dollar)
     Marks the end of each line. ^ (Caret) as well as the transfer is not a newline character in awk.

 \ {N, m \} (middle brackets)
     Single characters, meta characters shall specify the area that matches the number.
     \ {N \}, saying that the dog is exactly n,
     \ {N, \} n-2, or
     \ {, M \} refers to the m or less,
     \ {N, m \} is at least n, m more or less says.

 \ (Back slash)
     Next character is a special character (Special Character) Indicate that you are.

# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #

Replace

:% S / boy / girl / / / edit mode the letters in the file, the entire row of the boy and girl with the task of replacing the letters.

: 5,10 s / boy / girl / g / / 5 through 10 letters to change the row, you could come out in a line more than once to change the option g.

:., 20s/boy/girl / / / current row. Up to 20 rows.

:., $ S / boy / girl / / / current row. In the last row.

:., / Man / s / boy / girl / / / current row. In the next row that contains the letters of the man.

# # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # #

  sed [-e script] [-f script-file] [file ...]

Basic functions have been adopted from the ed, sed all these features are applied to. However, the interactive editor ed is
sed is a stream editor. Streaming, interactive editor and editor of the difference between the input and output is an interactive editor
One is made, a stream editor resulted in the input is the output of one.
\ N for newline character is used as a streaming editor.  

 Find (search), output (print),
    sed-n '/ abd / p' list.txt: list.txt file while reading line by line (-n: Do not print that reads) abd character finds the line output (p) is.

 Replacement (substitute),
    sed 's / addrass / address /' list.txt: addrass a change in address. However, the original file without changing the output is reversed.
     sed 's / addrass / address /' list.txt> list2.txt
     sed 's / \ t / \ /' list.txt: Convert Enterprise taepmunjareul
    sed 's / □ □ * / □ /' list.txt: (* display: □ indicates a space character.) one or more white space the above syntax string and turns it into one space.

 Add (insert)
     scriptfile - s /
     sed-f scriptfile 1.html:

 Delete (delete)
    sed '/ TD / d' 1.html: TD Delete the line containing the character to the output.
     sed '/ Src /! d' 1.html: Src characters do not delete the line.
     sed '1, 2d '1.html: the first one line, two lines, delete.
    sed '/ ^ $ / d 1.html: is the command to delete the blank line
  Regular expressions to extract only the file name
     s / ^. * \ / \ ([a-zA-Z0-9.] * \) ". * $ / \ 1 /: ^ at the beginning of the line. * any string, \ (, \) the regular expressions, grouping, and the top end of the line $.
    (S; ^. * \ / \ ([A-zA-Z0-9.] * \) ". * $; \ 1 ;) \ 1 refers to the first element of the grouped.
                                                                        [A-zA-Z0-9.], And the letters and numbers. (Comma) character to represent (character) says.
       That is the same string as the first group GF02.jpg and I grouped all of the following line is to change the contents. 

  / G: global meaning when there are multiple characters on each line target is to handle.

 who | sed-e 's;. * $;;': the first space of each line deleted from the final make up.

 who | sed-e 's; ^. *;;': from the beginning of each line, delete the last to make space.

 who | sed-e 's; ^. *:;;': from the beginning of each line: where there is a character (:) character) to make deleted.  

 The-n option
 sed on the input received is always stdout found that each line represents. However, sometimes a few lines from a file to extract a sed there are times when you want to use. In this case, use the-n option. This option is exactly what you tell me if you need to print any line you do not want to tell sed. Therefore p is used as the command. Line number and line number by showing a range of lines of text using sed optionally be able to print. As you can see in the next, the first two lines from a file was printed.

 $ Sed-n '1, 2p 'intro Just print the first 2 lines from intro file.

Surrounded by a forward slash instead of the line number sign p command if used with a string of characters that includes sed them through the lines of standard input will be printed. Thus, the first two lines from one file to print can be used as follows.

 $ Sed-n '/ UNIX / p' intro Just print lines containing UNIX
   sed '5 d ': line 5, delete
   sed '/ [Tt] est / d': Test or delete any line that contains the test
   sed-n '20, 25p 'text: text only print from the 20-25 to the line
   sed '1, 10s/unix/UNIX/g 'intro: intro of the first 10 lines of changes to the UNIX unix
   sed '/ jan/s/-1/-5': jan all the lines that contain the first change of -1 to -5
   sed 's / ... / /' data: each data line, delete them from the first three characters
   sed  'S / ... $ / /' data: each data line is deleted from the final 3 characters
   sed-n '1 'text: a non-printing characters \ nn as (where nn is the character of the octal value),
                                                    And tab characters> to print all lines from each text that represents

# # # # # # # # # # # # # # # # # #
awk command
# # # # # # # # # # # # # # # # # #

 awk '/ west /' datafile: west line output in the article

 awk '/ ^ north /' datafile: north of the line that begins with the output

 awk '/ ^ (no | so) /' datafile: no, or so the line that starts with the output

 awk '{print $ 3, $ 2}' datafile: datafile list, and the second field of the third to the clearance space output

 awk '{print $ 3 $ 2}' datafile: datafile third and second fields of the list, just attach the output 

 awk '{print "Number of fields:" NF}' datafile: datafile number of fields each of which returns julmadaui.

 awk '$ 5 ~ / \. [7-9] + /' datafile: fifth field is a number between 7 and 9 periods daeumet the record that at least one output

 awk '$ 2! ~ / E / {print $ 1, $ 2}' datafile: E pattern in the second field of the record is not the first and second field output

 awk '$ 3 ~ / ^ Joel / {print $ 3 "is a nice guy."}' datafile: Joel Starting with the third field "is a nice guy" with the output

 awk '$ 8 ~ / [0-9] [0-9] $ / {print $ 8}' datafile: If two numbers, the eighth field is the field output

 awk '$ 4 ~ / Chin $ / {print "The price is $" $ 8 "." } 'Datafile: After the fourth field in Chine "The price is $" 8 times the output field, and the period

 awk-F: '{print $ 1}' datafile:-F option to enter the field ':' as a distinction.

 awk-F "[:]" '{print $ 1, $ 2}' datafile: input field space and ':' as the field delimiter

 awk-f awk_script.file datafile:-f option when using the awk script file this content.

 awk '$ 7 == 5' datafile: 7 times the field is equal to 5, the output

 awk '$ 2 == "CT" {print $ 1, $ 2}' datafile: 2 times the field "CT" character is equal to 1 and 2 field output

 awk '$ 7 <5 {print $ 4, $ 7}' datafile: 7 times a field is less than 5, 4, 7 field output

 awk '$ 6> .9 {print $ 1, $ 6}' datafile: 6 times a field is greater than 0.9 times, six times the output

 awk '$ 8> 10 && $ 8 <17' datafile    

 awk '$ 2 == "NW" | | $ 1 ~ / south / {print $ 1, $ 2}' datafile

 Redirecting the output / input

     File descriptor 0: standard input
     File descriptor 1: The standard output
     The file descriptor 2: The standard error output
     $ Kill -1 1234> killout.txt 2> & 1: killout.txt as standard output and error output means saying.
     $ Kill -1 1234 1> & 2: the standard error output to the output means saying.