Skip to content
Advertisement

Check if line starts with digit then put those lines in a separate file using Bash

I would like to check in bash if line starts with digit then put those lines in a separate file. I tried ^[[0-9]] but it doesn’t work.

Here is the code which I tried:

JavaScript

Advertisement

Answer

The correct operator to use regex in Bash script is =~. Moreoever you don’t need to double [ in range of characters. Try this:

JavaScript

Edit:

But you don’t need a Bash loop for that job. You can do it with a sed one-liner:

JavaScript

Explanations(from right to left):

  • !d: do not delete
  • /^[0-9].*.tests./: all lines that start with one or more digits and that contain .tests. string
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement