-2

I need to write a script that compiles some pascal programme, launches it and than deletes all lines of the output containing anything else than numbers and spaces. After that, I should print the first five lines.

It's a homework so we're allowed to use just this basic commands.

I know how to compile and launch the program. I also know how to print the first five lines in the end. But I'm stuck at deleting the lines.

1

The regular expression [0-9 ] matches one digit or a space.

The regular expression [^0-9 ] matches one character that is not a digit nor a space.

The grep command can be used to extract lines from a file or from the output of a program (via a pipe) that matches a particular regular expression. The grep command has a -v flag that inverts the sense of the match so that only lines not matching the given regular expression are let through.

So, with those tools, you should be able to say something like

./myprogram | grep ...

You will need to fill in the dots yourself (as this is homework).

  • Kusalananda, you said The regular expression [^0-9 ] matches one character that is not a digit nor a space. Shouldn't it match all leading single digit and/or space? – user9303970 Mar 3 '18 at 20:18
  • 1
    @user9303970 The ^ character, in the beginning of a regular expression, anchors the expression to the start of the line. A bracket expression, [...], matches one character from within that expression. When a ^ character is first in a [...], it inverts the the sense of matching so that a character not within the brackets is matched. It's ^ in both places, but its meaning is different. – Kusalananda Mar 3 '18 at 20:38
  • 1
    @user9303970 So you could, for example, have ^[^=], which would mean "any line starting with something that isn't a =". – Kusalananda Mar 3 '18 at 20:40
  • Oh, I confused an external ^ (before []) with the internal one. Sorry. Very new to regex, aspiring to be more sensitive for that now. – user9303970 Mar 3 '18 at 21:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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