0

My file looks like this

#Default GitHub
Host github.com
  #Username username2
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_username2

Host github-username1
  #Username username1
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_username1

I'd like to find the first username after github.com so it would return in this case, username2.

Daniel Kobe
  • 9,376
  • 15
  • 62
  • 109

2 Answers2

1

To grab first Username after Host github.com:

$ awk '/Host github.com/ {f=1; next} f==1&& /Username/ {f=0; print $2}' file
username2
James Brown
  • 36,089
  • 7
  • 43
  • 59
  • 1
    @DanielKobe It prints the output you requested, no other extraneous lines. – Ed Morton Nov 19 '16 at 00:35
  • how do I pipe the output to a file, when I do `awk 'the command here' file > newfile.txt` it adds nothing to the newfile` – Daniel Kobe Nov 19 '16 at 01:00
  • @DanielKobe You don't pipe anything to a file, you redirect. If you want to _add_ to a file as in append you use `>>`. Other than that, you need to restate your question. – James Brown Nov 19 '16 at 01:09
  • @JamesBrown yes sorry, I meant the redirection doesn't work, it doesn't create a new file – Daniel Kobe Nov 19 '16 at 01:10
0
awk 'f&&/Username/{print $2; exit} /github\.com/{f=1}' file
Ed Morton
  • 188,023
  • 17
  • 78
  • 185