ehime
ehime

Reputation: 8415

grep not matching first part of line?

When listing the branches in my repo I have...

01:23:36 Sun Dec 20 [ehime@localhost: +1] ~/Repositories
$ git branch -a
* (HEAD detached at origin/qastg)
  demo
  devel
  master
  mheprod
  pqa
  pre
  prod
  qalv
  qastg
  remotes/origin/HEAD -> origin/development
  remotes/origin/demo
  remotes/origin/development
  remotes/origin/master
  remotes/origin/mheprod
  remotes/origin/pci
  remotes/origin/pcipqa
  remotes/origin/pqa
  remotes/origin/predevelopment
  remotes/origin/qalv
  remotes/origin/qastg

But running grep with ^ does not match as expected? Did I miss something?

01:23:39 Sun Dec 20 [ehime@localhost: +1] ~/Repositories
$ git branch -a |grep ^demo

Expected output demo

Upvotes: 1

Views: 39

Answers (3)

fedorqui
fedorqui

Reputation: 290185

The solution given by Mureinik is completely fine.

For completeness, let's use awk: since it kind of slurps the leading spaces from the fields, it is enough to check if the first field starts with demo:

...something... | awk '$1 ~ /^demo/'

Storing your data in a file, I test it and it produces the desired output:

$ awk '$1 ~ /^demo/' file
  demo

Upvotes: 1

Mureinik
Mureinik

Reputation: 311998

The relevant line does not start with demo - it has two whitespaces before it, so you have to address it. Consider, e.g.,:

$ git branch -a | grep '^  demo'

Or, if you want to be more generic:

$ git branch -a | grep '^\s*demo'

Upvotes: 1

Hermann Döppes
Hermann Döppes

Reputation: 1373

The demo is not at the beginning of the line.

Try ... | grep "^[ ]*demo".

Upvotes: 0

Related Questions