[英]What is the difference between cat -b and cat -n and how cat -s does work
I am currently developing a cat command in C and unix. 我目前正在用C和UNIX开发cat命令。 My task is design cat -b and cat -s commands this time.
这次,我的任务是设计cat -b和cat -s命令。 However I have already have created other commands such as -t, -v, -n, -e.
但是我已经创建了其他命令,例如-t,-v,-n,-e。 I got stuck at command -b as whenever I trigger the command it returns the same output what -n returns.
我被命令-b困住了,因为每当我触发命令时,它就会返回-n返回的相同输出。
for example 例如
cat -n hello.c
1 #include<studio.h>
2 int main() {
3 printf("hello");
4 return 0;
5 }
cat -b hello.c
1 #include<studio.h>
2 int main() {
3 printf("hello");
4 return 0;
5 }
My second question is how cat -s does work? 我的第二个问题是cat -s如何工作? as I again fired the command but the output displays a simple c file without any change.
当我再次触发命令时,输出显示了一个简单的c文件,没有任何更改。
for instance 例如
cat -s hello.c
#include<studio.h>
int main() {
printf("hello");
return 0;
}
Thanks in advance and for your time. 在此先感谢您和您的宝贵时间。
Let's start with a simple, six-line, file called qq.in
, with two non-empty lines, two empty lines and another two non-empty: 让我们从一个简单的六行文件
qq.in
开始,该文件包含两个非空行,两个空行和另外两个非空:
a
b
c
d
The difference between cat -n
and cat -b
is that the former will number all lines while the latter will number only non-empty lines: cat -n
和cat -b
之间的区别在于,前者将对所有行进行编号,而后者将仅对非空行进行编号:
$ cat -n qq.in $ cat -b qq.in
1 a 1 a
2 b 2 b
3
4
5 c 3 c
6 d 4 d
On the other hand, cat -s
will collapse consecutive empty lines into a single empty line, as per the following (combined with -n
so the effect is obvious): 在另一方面,
cat -s
将折叠连续的空线成一个单一的空行,按以下的(结合-n
所以效果是明显的):
$ cat -ns qq.in
1 a
2 b
3
4 c
5 d
Note that the lines must be truly empty, these flags won't do what you expect if the lines contain a space or some other white-space. 请注意,这些行必须真正为空,如果这些行包含空格或其他空白,则这些标志将无法实现您的期望。 If you want to treat white-space lines as empty, you can filter them first with something like:
如果要将空白行视为空行,可以先使用类似以下内容的过滤器:
sed -E 's/^\s+$//' qq.in | cat -b
Question: 题:
What is the difference between cat -b and can -n and how cat -s does work
cat -b和can -n和cat -s的工作原理有什么区别
From the output of man cat
: 从
man cat
的输出中:
-b, --number-nonblank number nonempty output lines, overrides -n -n, --number number all output lines -s, --squeeze-blank suppress repeated empty output lines
When you use cat -b
, the empty lines will not be numbered. 当您使用
cat -b
,空行将不会被编号。
Here' what I see with a simple file: 这是我通过一个简单文件看到的内容:
cat -b socc.cc
1 #include 2 int main() 3 { 4 return 0; 5 }
cat -n socc.cc
1 #include 2 3 int main() 4 { 5 return 0; 6 } 7 8
cat -s socc.cc
#include int main() { return 0; }
-b, --number-nonblank number nonblank output lines -n, --number number all output lines
ref: cat manpage 参考: 猫手册
and the second question, from man page: 第二个问题来自手册页:
-s, --squeeze-blank
-s,--squeeze-blank
never more than one single blank line
you can implement this feature like this 您可以像这样实现此功能
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.