Awk & sed scripting language

Source: Internet
Author: User

SedOperator example

SedOperator example

Representation

Effect

8d

Delete the input 8th rows.

/^ $/D

Delete all empty rows.

1,/^ $/d

The number of empty rows from the beginning of the input is deleted to 1st (the first empty row is also deleted ).

/Jones/P

Print only the rows that contain "Jones" (use the-n option ).

S/Windows/Linux/

In each input line, replace the first "Windows" instance with "Linux ".

S/bsod/stability/g

In each input line, replace all "bsod" with "stability ".

S/* $ //

Delete all spaces at the end of each line.

S/00 */0/G

Compress all consecutive 0 values to a single 0.

/GUI/d

Delete all rows containing "Gui.

S/GUI // G

Delete all "guis" and maintain the integrity of the remaining parts.

 

 

1. Delete all empty lines in the file
#! /Bin/bash
Sed-E/^ $/D "$1" file.txt
= Sed-e '/^ $/d' file.txt
Exit 0

-E: indicates that the "edit" command is followed (optional) Edit
^: Indicates the beginning of a row.
$: Indicates the end of a row.
D: delete command
"$1": Refers to a command line parameter, indicating that white space or special characters can be used in the file name.

2. replace a string (or matching mode) in a file with another string (or matching mode)
Sed-e "s/$ old_pattern/$ new_pattern/g" $ file_name
S: substitution replacement
"G", that is, the global flag, used to automatically replace

3. Write log files
Echo "'date' + 'whoam' + $ operation" $ @ ""> $ file_name
$ Operation: before the operation is executed, it is recorded in the log file.

4. A two‑dimensional table that prints ASCII codes
#! /Bin/bash
# Pr-ascii.sh: a two-dimensional table that prints ASCII codes.

Start = 33 # printable ASCII character range (decimal ).
End = 125

Echo "decimal hex character" # header.
Echo "-------------------"

For (I = start; I <= end; I ++ ))
Do
Echo $ I | awk '{printf ("% 3d % 2x % C/N", $1, $1, $1 )}'
# In this context, the built-in printf command of Bash is not run:
# Printf "% C" "$ I"
Done

Exit 0

 

 

 

 

1. # delete all original empty rows and add an empty row after each row.
# In this way, each row in the output text is followed by an empty row.
Sed '/^ $/d; G'
2. # delete all even rows
Sed 'n'; D'
3. # insert an empty row before and after the row matching the style "RegEx"
Sed '/RegEx/{X; P; X; G ;}'
4. # insert an empty row before and after the row matching the style "RegEx"
Sed '/RegEx/{X; P; X; G ;}'
# Number each row in the file (simple left alignment ). The "tab" is used here"
# (Tab, see the '/t' usage description at the end of this article) instead of spaces to align edges.
Sed = filename | sed 'n'; S // N // t /'

# Number of all rows in the file (the row number is left and the right of the text is aligned ).
Sed = filename | sed 'n'; S/^ //; S/*/(./{6,/}/)/N // 1 /'

# Number of all rows in the file, but only the row numbers of non-blank rows are displayed.
Sed '/./= 'filename| sed'/./N; S // N //'

# Calculate the number of rows (simulate "WC-L ")
Sed-n' $ ='

# Replace "foo" with "bar" only when the string "Baz" appears in the row"
Sed '/Baz/S/Foo/BAR/G'
# Replace "foo" with "bar", and replace it only when "Baz" is not displayed in the row.
Sed '/Baz /! S/Foo/BAR/G'

 

Others
1、abc-> ABC for all content in myfile.txt
Sed-E's/ABC/G' myfile.txt:
Sed-E's:/ABC: G' myfile.txt

2. perform this operation within the specified number of rows:
Sed-e '1, 10 s/ABC/G' myfile.txt
Only in the first to tenth rows (including the two rows)

3. <B> This </B> is what <B> I </B> meant.
Sed-E's/<. *> // G' myfile.html-> meant.
Sed-E's/<[^>] *> // G' myfile.html-> This is what I meant.
'[^>]' Specifies the "not"> '"character, and the' * 'after it completes this expression to indicate" zero or multiple ">" characters ".

4. [A-Z] [A-Z]

5. Assume that you are replying to a message. The following example adds the phrase "Ralph said:" in front of each line :":
Sed-E's/. */Ralph said: &/'origmsg.txt

The output is as follows:
Ralph said: Hiya Jim, Ralph said:
I sure like this sed stuff! Ralph said:

6. Delete content starting #
Sed-E/^ #/d myfile.txt

Main () function in the output file
Sed-n-e '/main [[: Space:] * (/,/^}/P' file.txt
Sed-n-e '/main [-v] * (/,/^}/P' file.txt

Reverse row
Sed-e'1! G; h; $! D' forward.txt> backward.txt
Explanation:
The 'G' command applies only the first line. However, there is another '! 'Character -- this '! The 'character ignores this address, that is, the 'G' command applies to all rows except the first line.
If the command is '$ d', only the 'D' command will be applied to the last line in the file (' $ 'address is a simple way to specify the last line ). However, '! ',' $! D' applies the 'D' command to all rows except the last row.

7. DOS/Windows-based text files have a Cr (Press ENTER) and LF (line feed) at the end of each line, while UNIX text has only one line feed.

Migrate UNIX text to Windows
Sed-E's/$ // R/'myunix.txt> mydos.txt

Convert DOS/Windows text to trusted UNIX format
Sed-E's/. $ // 'mydos.txt> myunix.txt

10. script language:
Change. SH: sed-E's //(. */)/(. */)/(. */)/Victor/1-/2 von/3/'myfile.txt
Myfile.txt: Foo bar oni eeny meeny miny Larry curly Moe Jimmy the Weasel
Run:./change. Sh
Output: Victor Foo bar oni eeny meeny miny Larry curly Moe Jimmy the weasel-von

 

Reference:

Http://www.tsnc.edu.cn/default/tsnc_wgrj/doc/abs-3.9.1_cn/html/wrapper.html#EX3

Http://sed.sourceforge.net/sed1line_zh-CN.html

Http://www.ibm.com/developerworks/cn/linux/shell/sed/sed-1/

 

 

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.