Shell – How to output file & ignoring lines that start with “?”

shell-scriptsubversiontext processing

I do svn status --show-updates and then I want to either

  • Q1:
    ignore (not to display) lines that start with ?
  • Q2:
    display only lines that start with * Note that there are few spaces before * occurs. That means that * is not the first character on the line.

How can I do that?

Best Answer

You can express those conditions using regular expressions and use grep to filter the results based on those.

The first one is ^?. The carat is a special character that represents the beginning of a line; so that expression matches the beginning of the line immediately followed by a ?.

The second one is ^ *\*. The * is a special character that qualifies the preceding character - it means the preceding character may appear zero or more times. Since * is a special character, the one you're looking for needs to be escaped, hence, \*. So that expression will match the beginning of a line followed by zero or more spaces, followed by an asterisk.

For your first condition, use the -v option for grep to negate the results.

So finally,

svn status --show-updates | grep -v '^?'

or

svn status --show-updates | grep '^ *\*'

Regular expressions are very powerful, so many Unix tools can use them. They are very much worth learning. There is a great tutorial at regular-expressions.info.