I found the question and the comments interesting, so I had a deeper look into it. According the comments given I understand that the question is more about "How to modify the output of the cat command?", maybe just by using command switches and if possible. Just a note, the question would be easier to understand if a sample input is given and the expected output is shown.
It is assumed the following TEST_FILE
is given:
This is the first (1) paragraph. This is the second (2) sentence. This is the third (3) sentence.\n The fourth (4) sentence of the first (1) paragraph is the second (2) line of the first (1) paragraph.
This is the second (2) paragraph. This is the second (2) sentence of the second (2) paragraph. This is the third (3) sentence of the second (2) paragraph.
This is the third (3) paragraph. This is the second (2) sentence. This is the third (3) sentence of the second (3) paragraph.
This is the fourth (4) paragraph. This is the second (2) sentence of the fourth (4) paragraph. This is the third (3) sentence.
This is the fifth (5) paragraph. This is the second (2) sentence. This is the third (3) sentence.\n The fourth (4) sentence of the fifth (5) paragraph is the second (2) line of the fifth (5) paragraph.
The paragraphs are not separated by a single blank line (as it was requested), but it is good to learn how some of the commands are working.
Suppress repeated empty output lines:
cat -s TEST_FILE
This is the first (1) paragraph. ...
This is the second (2) paragraph. ...
This is the third (3) paragraph. ...
This is the fourth (4) paragraph. ...
This is the fifth (5) paragraph. ...
Additionally number all output lines:
cat -s -n TEST_FILE
1 This is the first (1) paragraph. ...
2
3 This is the second (2) paragraph. ...
4
5 This is the third (3) paragraph. ...
6
7 This is the fourth (4) paragraph. ...
8
9 This is the fifth (5) paragraph. ...
And number nonempty output lines only:
cat -s -n -b TEST_FILE
1 This is the first (1) paragraph. ...
2 This is the second (2) paragraph. ...
3 This is the third (3) paragraph. ...
4 This is the fourth (4) paragraph. ...
5 This is the fifth (5) paragraph. ...
According your "SOLUTION FOUND" it seems to be agreed to use pipe and a second command to modify the output of the cat
command. At least for your third requirement it would be necessary.
Therefore you could achieve the same result with number lines nl
(but only if the the empty lines are really empty and do not contain white spaces):
cat TEST_FILE | nl
1 This is the first (1) paragraph. ...
2 This is the second (2) paragraph. ...
3 This is the third (3) paragraph. ...
4 This is the fourth (4) paragraph. ...
5 This is the fifth (5) paragraph. ...
It would then be possible to Remove blank lines with grep
also:
cat -s -n -b TEST_FILE | grep .
In case of lines with whitespaces it would be necessary to remove them first:
grep -v -e '^[[:space:]]*$' TEST_FILE | nl
To Echo newline in Bash you can use:
echo -e $(cat -s TEST_FILE | tail -1)
echo -e $(cat -s -n -b TEST_FILE | tail -1)
You could also show certain lines numbers only. But this is already leaving the scope of the question.
cat
manual?cat
" in the sense that it (superfluously) usescat
to provide input togrep
. If that's all that you need to complete the "challenge" then for parts (2) and (3) you can just use any commands that accept standard inputcat
is completely irrelevant for this problem. Why on earth would you insist on using it? Even as a homework assignment it doesn't make sense.