0

I have a text file for which I need to do three things:

  1. Modify the "cat" command to highlight keywords whilst showing the complete contents of the file. SOLUTION FOUND
  2. Modify the "cat" command to automatically number the paragraphs. These are separated by a single blank line.
  3. Modify the "cat" command to display the final paragraph only.

The tricky part is that all of these need to be done by modifying the cat command.

I've seen many forum posts explaining that cat is not meant for this - and I can entirely see why. However, the challenge is that I have to use cat!

  • Two questions: Is this homework? Have you read the cat manual? – fd0 Sep 5 '16 at 20:25
  • Not homework. More like a project. I've not read it cover-to-cover, but the stuff I've looked at was either too complex for me or doesn't look possible. – test_user123 Sep 5 '16 at 20:35
  • 1
    Your SOLUTION FOUND only "uses cat" in the sense that it (superfluously) uses cat to provide input to grep. 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 input – steeldriver Sep 5 '16 at 23:04
  • 4
    cat 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. – Gilles 'SO- stop being evil' Sep 5 '16 at 23:12
  • 3
    Your classmate has asked this on Ask Ubuntu. It doesn't make any more sense the second time. (Unless you mean to modify cat rather than use cat? i.e. is this a C programming exercise rather than a shell exercise?) – Gilles 'SO- stop being evil' Sep 8 '16 at 16:18
1

You really need to use cat?

cat -n  thefile | tail -n1 

What do you mean with "Highlight keywords whilst showing the complete contents of the file."

What keywords you want to highlight?

What is the purpose to number all the paragraphs but to show only the last one? (NOTE: the command that i write will do exactly this, cat -n to number the paragraphs, and tail -n1 to show only the last paragraph.)

  • Thank you - that actually solves 2 and 3 nicely. As for highlighting - sorry I should be more specific. I need to be able to highlight a particular word whenever it occurs within the text. So for example, in the sentence 'This is my paragraph', I may want to highlight (add colour) to the word 'paragraph'. – test_user123 Sep 5 '16 at 20:49
  • I'm afraid I absolutely have to use cat. I know there are, and have found, far better solutions without it. – test_user123 Sep 5 '16 at 20:52
  • @ifellover just a note: the above numbers and/or selects lines - not "paragraphs" that are "separated by a single blank line" – steeldriver Sep 6 '16 at 0:43
  • cat -n thefile | tail -n1 | grep --color "paragraph" ... See if that help... – Luciano Andress Martini Sep 6 '16 at 11:38
  • 1
    Also -n will also number the empty lines -b will number non-empty lines. – Arronical Sep 9 '16 at 8:21
0

Good luck with teaching gcse computing coursework ;) It doesn't specify to use cat in the question, at least in the one on the question sheet!

I just did

perl -00pe ‘s/^/$. /’ textfile

for 7ii and

perl -00pe ‘s/^/$. /’ jamestextfile | tail -n1

for 7iii

The important thing in question 7 is using pipe and stuff.

  • 1
    Thanks for this. I'm not sure I entirely agree with your interpretation of the question - you may want to check again. But my question was not for the controlled assessment task; I'm not a teacher who provides his students with solutions to these problems. I posted only to clarify my own subject knowledge. As for being your teacher, it's highly unlikely. But if you were my student, I would commend you for your contribution to this thread and recognise your potential as a Computer Scientist. I like to learn just as much from my students as they learn from me. Keep going! – test_user123 Dec 4 '16 at 11:45
  • Right @james.edwardsiscoolatschool, the intension of the quesitons seems to be more using pipe than Use Of Cat only. – U880D May 17 '18 at 10:58
0

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.