cat command usage

cat命令: concatenate files and print on the standard output 连结并输出文件到标准输出。

语法: cat [OPTION]… [FILE]…
常用选项:
-n:显示行号。行号不是文件内容。
-E:show ends 显示每一行的结束符,linux是$ window是$\n ??
-A:show ALL  显示所有的字符,包括制表符,空格等。
实例:

$cat test.data 
This is a test file
Test file begin.
This is the first line.
This is the second line.
    This is a tab start line

说明:把文件内容输出到标准输出,可以一次性输出多个文件。

$ cat -n test.data 
 1  This is a test file
 2  Test file begin.
 3  This is the first line.
 4  This is the second line.
 5          This is a tab start line

说明:输出文件和行号。

$ cat -E test.data 
This is a test file$
Test file begin.$
This is the first line.$
This is the second line.$
    This is a tab start line$
    
说明:输出文件中每一行的结束符号。

$ cat -A test.data 
This is a test file$
Test file begin.$
This is the first line.$
This is the second line.$
^IThis is a tab start line$

说明: -A选项,显示文件中所有的字符,制表符,换行符。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是利用无名管道通信编写实现命令cat的功能的程序: ```c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> #define BUFFER_SIZE 1024 int main(int argc, char *argv[]) { int pipe_fd[2]; if (argc != 2) { fprintf(stderr, "Usage: %s <filename>\n", argv[0]); exit(EXIT_FAILURE); } if (pipe(pipe_fd) == -1) { perror("pipe"); exit(EXIT_FAILURE); } pid_t pid = fork(); if (pid == -1) { perror("fork"); exit(EXIT_FAILURE); } else if (pid == 0) { // child process close(pipe_fd[1]); // close write end of pipe dup2(pipe_fd[0], STDIN_FILENO); // redirect stdin from pipe close(pipe_fd[0]); // close read end of pipe execlp("cat", "cat", NULL); // execute cat command perror("execlp"); exit(EXIT_FAILURE); } else { // parent process close(pipe_fd[0]); // close read end of pipe int input_fd = open(argv[1], O_RDONLY); if (input_fd == -1) { perror("open"); exit(EXIT_FAILURE); } char buffer[BUFFER_SIZE]; ssize_t num_read; while ((num_read = read(input_fd, buffer, BUFFER_SIZE)) > 0) { write(pipe_fd[1], buffer, num_read); // write data to pipe } close(pipe_fd[1]); // close write end of pipe close(input_fd); // close input file } return 0; } ``` 该程序会创建一个无名管道,然后创建一个子进程。子进程将从管道中读取数据,并使用cat命令将其输出到标准输出。父进程将从命令行参数指定的文件中读取数据,并将其写入管道中。这样,子进程就可以从管道中读取数据,并将其输出到标准输出,就像执行cat命令一样。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值