剪切和粘贴 cut和paste

cut剪切命令示例

cut命令使您能够从文件中提取一列信息列。要指定要提取的列,我们使用-c参数。然后是列号。要提取多个列,可以传递以逗号分隔的列表。也可以使用-f来指定字段。分隔符也可以用-d参数指定。除非指定,否则默认的分隔符是制表符。

john@john-desktop:~/test_examples$ cat cutfile1.txt
harry,25,16200
gill,46,17500
bill,45,20000
john,43,100000
barry,27,42000
paul,18,26500
john@john-desktop:~/test_examples$ cut -d, -f 1,3 cutfile1.txt
harry,16200
gill,17500
bill,20000
john,100000
barry,42000
paul,26500

在上面的例子中,我们指定了分隔符,。我们指定要在文件cutfile1.txt中剪切字段1和3。

john@john-desktop:~/test_examples$ cat cutfile1.txt
harry,25,16200
gill,46,17500
bill,45,20000
john,43,100000
barry,27,42000
paul,18,26500
john@john-desktop:~/test_examples$ cut -c 1-4 cutfile1.txt
harr
gill
bill
john
barr
paul

上面是cut命令最简单形式的示例。我们从文件cutfile1.txt中剪切前4个字母。

paste粘贴命令示例

paste命令对于将文件合并在一起很有用。每个文件的第一行以Tab字符分隔。可以使用-d参数指定其他定界符。

john@john-desktop:~/test_examples$ cat pastefile1.txt
one
two
three
four
five
john@john-desktop:~/test_examples$ cat pastefile2.txt
six
seven
eight
nine
ten
john@john-desktop:~/test_examples$ paste pastefile1.txt pastefile2.txt
one	six
two	seven
three	eight
four	nine
five	ten

下一个示例相同,但是,我们将默认的制表符分隔符更改为

john@john-desktop:~/test_examples$ cat pastefile1.txt
one
two
three
four
five
john@john-desktop:~/test_examples$ cat pastefile2.txt
six
seven
eight
nine
ten
john@john-desktop:~/test_examples$ paste -d: pastefile1.txt pastefile2.txt
one:six
two:seven
three:eight
four:nine
five:ten
日期:2019-04-29 03:17:27 来源:oir作者:oir