[Reprint] Common UNIX command Usage-cut and paste

Source: Internet
Author: User
Original link: http://www.pczpg.com/html/caozuoxitong/Lunix_Unix/20090807/12861.html

Cut is used for cutting columns or fields from standard input or text files. The cut text can be pasted into a text file.

Cut General format is:
Cut [options] file1 file2
The following options are available:
-C list Specifies the number of cut characters.
The-f field specifies the number of clipping fields.
-d Specifies a field separator that differs from the Space and tab keys.
-C is used to specify the clipping range, as follows:
-c1,5-7 cuts the 1th character, then the 5th to the 7th character.
-C1-50 cut the first 50 characters.
The-f format is the same as-C.
-f1,5 Cut the 1th field, the 5th field.
-f1,10-12 Cut the 1th field, the 10th field to the 12th field.


Example:
Wangnc> PG Pers
P.jones:office runner:id897
S.round:unix admin:id666
L.clip:personl chief:id982


The file uses the colon ":" as the field delimiter, so you can use the-D option to specify a colon, such as-D:. If you are interested in the 3rd field, you can use-F3. To extract the ID field. The commands you can use are as follows:
Wangnc> cut-d:-f3 Pers
ID897
ID666
ID982


The cut commands are separated by commas, such as the Cut fields 1 and 3, the name and ID number, which you can use:
Wangnc> cut-d:-f1,3 Pers
p.jones:id897
s.round:id666
l.clip:id982

Use the-C option to specify the exact number of cuts. This method needs to know exactly what the start and end characters are. Usually I don't use this method unless it's on a fixed-length field or file name.
Who-u | Cut-c1-8


Cut is used to extract data columns or fields from a text file or standard output, and then use paste to paste the data into related files.
When you paste data from two different sources, you first classify it and make sure that two rows are the same number of files. Paste will put the different file line information on one line by row. By default, when paste a connection, separate text in a new row is separated by a space or TAB key, and it becomes a field separator unless the-d option is specified.
The paste format is:
Paste-d-s-file1 File2
The options have the following meanings:
-d Specifies a field separator that differs from the Space or TAB key. For example, with the @ Separator field, use the-D @.
-S to merge each file into rows instead of pasting it by row.
-Use standard input. For example, ls-l |paste, meaning that the output is displayed only on one column.


Example:
Wangnc> PG PAS1
ID897
ID666
ID982
Wangnc> PG PAS2
P.jones
S.round
L.clip


The basic Paste command pastes the PAS1 and pas2 two files into two columns:
wangnc> Paste Pas1 Pas2
ID897 P.jones
ID666 S.round
ID982 L.clip


You can specify which column is sticky by swapping the file name:
wangnc> Paste Pas2 pas1
P.jones ID897
S.round ID666
L.clip ID982


To create a field separator that is different from the Space or TAB key, use the-D option. The following example uses a colon as a field separator.
Wangnc> paste-d: Pas2 pas1
p.jones:id897
s.round:id666
l.clip:id982


To combine two rows instead of pasting by rows, you can use the-s option. In the example below, the first line is pasted with the name and the second line is the ID number.
wangnc> paste-s pas1 PAS2
ID897 ID666 ID982
P.jones S.round L.clip

The


Paste command also has a useful option (-). Read the data once for each (-), from the standard input. Use a space as the field separator to display a list of directories in a 4-column format. The method is as follows:
wangnc> ls/etc | paste-d ""-----
Manpath PATH shlib_path snmpagent.d/timezone x11/
Acct/alias es@ arp@ audeventstab audomon@ auto_master
auto_parms.log auto_parms.log.old backup@ backup.cfg bcheckrc@ bootpd@
bootpquery@ bootptab btmp@ catman@ checklist@ chroot@
clri@ cmcluster/cmcluster.conf cmom.conf conf@ Br>copyright cron@ csh.login d.cshrc@ d.exrc@ d.login@ can also display output in a column format:
wangnc> ls/etc | paste-d ""-
Manpath
path
Shlib_path
snmpagent.d/
TIMEZONE
x11/
acct/
aliases@
arp@
Audeventstab
audomon@
Auto_master
Auto_parms.log
auto_parms.log.old
backup@
backup.cfg

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.