Reputation: 137
I am currently developing a cat command in C and unix. My task is design cat -b and cat -s commands this time. However I have already have created other commands such as -t, -v, -n, -e. I got stuck at command -b as whenever I trigger the command it returns the same output what -n returns.
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? as I again fired the command but the output displays a simple c file without any change.
for instance
cat -s hello.c
#include<studio.h>
int main() {
printf("hello");
return 0;
}
Thanks in advance and for your time.
Upvotes: 1
Views: 693
Reputation: 881303
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:
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 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 -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
Upvotes: 3
Reputation: 2544
-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
never more than one single blank line
you can implement this feature like this
Upvotes: 1
Reputation: 206577
Question:
What is the difference between cat -b and can -n and how cat -s does work
From the output of 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.
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; }
Upvotes: 3