Shell awk sed Combat

Source: Internet
Author: User

Combat one: File a006 take out the numbers inside

Tl_fmt_chg_shm_key=7100;export Tl_fmt_chg_shm_key

Tl_msq_shm_key=7200;export Tl_msq_shm_key

Tl_txn_srv_shm_key=7300;export Tl_txn_srv_shm_key

Sed method

Sed ' s/\ (. *\) =\ (. *\); \ (. *\)/\2/' a006

7100

7200

7300

awk-f\; ' {print $} ' a006 | Awk-f= ' {print $} '//awk methods There are a lot of things I only use in one simple way

7100

7200

7300

Combat two: List command return values, from the second row to the fourth row

ll | Sed-n ' 2,4p '

-rw-r--r--. 1 root root 14:07 a001

-rw-r--r--. 1 root root 08:45 a002

-rw-r--r--. 1 root root 357 Mar 09:01 a003

Extension, listing second and fourth lines

ll | Sed-n ' 2p;4p '

-rw-r--r--. 1 root root 14:07 a001

-rw-r--r--. 1 root root 357 Mar 09:01 a003

Combat three: Realize Cat function

Sed-n ' P '/etc/passwd

Combat four: using SED scripts

Cat test-script.sed

/^root/p

/^nobody/p

[Email protected] redx]# sed-n-F test-script.sed/etc/passwd

Root:x:0:0:root:/root:/bin/bash

Nobody:x:99:99:nobody:/:/sbin/nologin

Combat five: Multi-instruction execution

Sed-n ' {

/^root/p

/^nobody/p

} '/etc/passwd

Root:x:0:0:root:/root:/bin/bash

Nobody:x:99:99:nobody:/:/sbin/nologin

or Sed-n-e '/^root/p '-e '/^nobody/p '/etc/passwd

Combat Six: Address range application

You can modify the address range by using commas, plus signs, and tilde numbers.

In the example above, a comma has been used to participate in the designation of the address range. The meaning is clear: n,m represents nth to

Line M.

Plus + with commas, you can specify several lines of the phase, rather than absolute lines. If n,+m means to open from nth row

M line after the start

Tilde ~ You can also specify an address range. It specifies the number of rows to skip each time. If n~m means starting from line N, each time

Skip M Line:

Cat Employee.txt

101,john Doe,ceo

102,jason Smith,it Manager

103,raj Reddy,sysadmin

104,anand Ram,developer

105,jane Miller,sales Manage

Cat Employee.txt | Sed-n ' 1,3p '//1 to 3 lines

101,john Doe,ceo

102,jason Smith,it Manager

103,raj Reddy,sysadmin

Cat Employee.txt | Sed-n ' 1,+3p '//1th row and back 3 rows

101,john Doe,ceo

102,jason Smith,it Manager

103,raj Reddy,sysadmin

104,anand Ram,developer

Cat Employee.txt | Sed-n ' 1~3p '//Every 3 lines starting from the first line

101,john Doe,ceo

104,anand Ram,developer

Combat Seven: matching applications

Cat Employee.txt | Sed-n '/anand/p '//Match Anand line and print

104,anand Ram,developer

Cat Employee.txt | Sed-n '/anand/,$ p '//Match Anand line to end and print

104,anand Ram,developer

105,jane Miller,sales Manage

Combat eight: Delete file contents

Sed-i ' d ' employee.txt

Note: If you have multiple commands, sed encounters command D, deletes the entire row of data that matches, and the remaining commands will not

The row to which the operation was deleted.

Cat Employee.txt

Combat Nine: Copy files

Sed ' w bak.txt ' employee.txt//plus-n does not display content

101,john Doe,ceo

102,jason Smith,it Manager

103,raj Reddy,sysadmin

104,anand Ram,developer

Cat Bak.txt

101,john Doe,ceo

102,jason Smith,it Manager

103,raj Reddy,sysadmin

104,anand Ram,developer

The above address range or matching function can also be implemented

Combat Ten: Specify replacement

Cat Employee.txt | Sed-n '/102/s/s/a/p '

102,jason Amith,it Manager

Cat Substitute-locate.txt

Locate command is used to locate files

Locate command uses database to locate files

Locate command can also use regex for searching

Sed ' S/LOCATE/FIND/2 ' substitute-locate.txt//replace the second matching

Locate command is used to find files

Locate command uses database to find files

Locate command can also use regex for searching

Sed-n ' S/LOCATE/FIND/2 p ' substitute-locate.txt//Ibid. The difference is to show only the matching to the

Locate command is used to find files

Locate command uses database to find files

Sed-n ' s/john/johnny/w output.txt ' employee.txt

# cat Output.txt

101,johnny Doe,ceo

Sed-n ' S/JOHN/JOHNNY/IP ' employee.txt//i ignoring case

101,johnny Doe,ceo

File:///C:/Users/Administrator/Downloads/51CTO%E4%B8%8B%E8%BD%BD-Sed%20and%20Awk%20101%20Hacks%20-%E4%B8%AD%E6 %96%87%e7%89%88.pdf

This article is from the "Blue Sky" blog, make sure to keep this source http://shurk.blog.51cto.com/1134443/1757867

Shell awk sed Combat

Related Article

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.