Unix shell programming (4)

Source: Internet
Author: User
Tags printable characters

Unix shell programming (4)

 

Cut command
You can extract the required data fields from the data file or command output.
Command Format: Cut-cchars File
Chars indicates the characters to intercept, which can be numbers.
File indicates a file. If file is not specified, the cut command can be used as a filter of the pipeline to read the input from the standard output.
For example:
[Root @ localhost MISC] # Who
Root pts/1 (10.3.34.117)
Fedora tty7 (: 0)
[Root @ localhostmisc] # Who | cut-c1-8
Root
Fedora
Note:
Cut-c1-8: truncates 1st to 8 characters in each line of input and serves as a standard output.

-D and-F options of the cut command
Command: Cut-ddchar-ffields File
Dchar is the delimiter used to separate fields in data.
Fields indicates the fields to be truncated from the file.
For example:
[Root @ localhost MISC] # Cat/etc/passwd
Root: X: 0: 0: Root:/root:/bin/bash
Bin: X: 1: 1: Bin:/bin:/sbin/nologin
Daemon: X: 2: 2: daemon:/sbin/nologin
ADM: X: 3: 4: ADM:/var/adm:/sbin/nologin
LP: X: 4: 7: LP:/var/spool/lpd:/sbin/nologin
Sync: X: 5: 0: Sync:/sbin:/bin/Sync
Fedora: X: 500: 500: fedora:/home/Fedora:/bin/bash
[Root @ localhost MISC] # Cut-D:-F1/etc/passwd
Root
Bin
Daemon
Adm
Lp
Sync
Fedora

The-F option of cut is the corresponding tab.
For example:
[Root @ localhost programs] # Cat phonebook
Alica chebb 973-555-2015
Barbara Swingle 201-555-9257
Liz stachiw 212-555-2298
Susan Goldberg 201-555-7776
Tony iannino 973-555-1295
[Root @ localhost programs] # Cut-F1 phonebook
Alica chebb
Barbara Swingle
Liz stachiw
Susan Goldberg
Tony iannino
[Root @ localhost programs] # Cut-F2 phonebook
973-555-2015
201-555-9257
212-555-2298
201-555-7776
973-555-1295

Paste command: Unlike cut, it can combine multiple rows.
For example:
[Root @ localhost programs] # Cat names
Tony
Emanuel
Lucy
Ralph
Fred
[Root @ localhost programs] # Cat addresses
55-23 Vine Street, Miami
39 University Place, New York
17E. 25th Street, New York
38 Chauncey st., Bensonurst
17 E. 25th Street, New York
[Root @ localhost programs] # Cat numbers
(307) 555-5356
(212) 555-3456
(212) 555-9959
(212) 555-7741
(212) 555-0040
[Root @ localhost programs] # paste names numbers addresses
Tony (307) 555-5356 55-23 Vine Street, Miami
Emanuel (212) 555-3456 39 University Place, New York
Lucy (212) 555-9959 17E. 25th Street, New York
Ralph (212) 555-7741 38 Chauncey st., Bensonurst
Fred (212) 555-0040 17 E. 25th Street, New York

Use the-D option -- to separate multiple characters of each row without a tab, as shown below:
[Root @ localhost programs] # paste-d '+ 'names numbers addresses
Tony + (307) 555-5356 + 55-23 Vine Street, Miami
Emanuel + (212) 555-3456 + 39 University Place, New York
Lucy + (212) 555-9959 + 17E. 25th Street, New York
Ralph + (212) 555-7741 + 38 Chauncey st., Bensonurst
Fred + (212) 555-0040 + 17 E. 25th Street, New York

-S option, telling paste to paste the rows in the same file together.
For example:
[Root @ localhost programs] # Cat names
Tony
Emanuel
Lucy
Ralph
Fred
[Root @ localhost programs] # paste-s names
Tony Emanuel Lucy Ralph Fred

Sed is a program used to edit data. It refers to a stream editor. Sed cannot be used for interaction.
Format: sed command file
Command is a command in the same style as Ed for each row of the specified file.
If no file is specified, the standard input is used as the processing object.
Example:
[Root @ localhost programs] # Cat intro
The UNIX operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
In the late 1960 s. One of the primary goals in
The design of the UNIX system was to create
Environment that promoted efficient Program
Developments.
Change UNIX to Unix.
[Root @ localhost programs] # sed S/Unix/intro
The UNIX operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
In the late 1960 s. One of the primary goals in
The design of the UNIX system was to create
Environment that promoted efficient Program
Developments.

-N option of SED
Extract the specified row of the file.
For example:
[Root @ localhost programs] # sed-n'1, intr' intro
The UNIX operating system was pioneered by Ken
Thompson and Dennis Ritchie at Bell Laboratories
Print only UNIX lines:
[Root @ localhost programs] # sed-n'/Unix/P' temp
The UNIX operating system was pioneered by Ken
The design of the UNIX system was to create

Delete a row, for example:
[Root @ localhost programs] # sed '1, 2D 'intro
In the late 1960 s. One of the primary goals in
The design of the UNIX system was to create
Environment that promoted efficient Program
Developments.
Delete a row that contains Unix:
[Root @ localhost programs] # sed '/Unix/d' temp
Thompson and Dennis Ritchie at Bell Laboratories
In the late 1960 s. One of the primary goals in
Environment that promoted efficient Program
Developments.

Sed command summary example:
--------------------------------------
Sed '5d 'deletes 5th rows
Sed '/[TT] est/d' to delete rows containing test and test
Sed-n'20, 25p' text displays the rows 20th to 25 of the text file
Sed '1, 10 s/Unix/G' intro changes the first 10 rows of UNIX in the intro file to Unix
Sed '/Jan/S/-1/-5/' changes the first-1 in all rows containing Jan to-5
Sed's/... // 'data deletes the first three characters in each row of the data file
Sed's/... $ // 'data deletes the last three characters in each row of the data file
Sed-N 'l' text displays all rows in the text file, and displays all non-printable characters as/nn, and tabs as/T
--------------------------------------

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.