Search code examples
linuxbashunixclearcase

Getting a specifit string from a text using bash


I have an output that look like this:

Tag: abrodov_linux_vlsi07_test1
  Global path: /net/vposeidon02/vlsifs/vlsi02/vlsi_ccstore/07/abrodov/abrodov_linux_vlsi07_test1.vws
  Server host: ta1vlsi07
  Region: tlv_unix
  Active: YES
  View tag uuid:26075e8c.4e2e11e4.9cc0.a4:ba:db:3c:fc:0e
View on host: ta1vlsi07
View server access path: /net/vposeidon02/vlsifs/vlsi02/vlsi_ccstore/07/abrodov/abrodov_linux_vlsi07_test1.vws
View uuid: 26075e8c.4e2e11e4.9cc0.a4:ba:db:3c:fc:0e
View owner: abrodov

I need to get th server host which is in this example is ta1vlsi07

I wrote some command that worked for in order to get the View uuid, but in this case it didn't work, here is the command:

ct lsview -l abrodov_linux_vlsi07_test1 |grep -i 'Server host:' | cut -d" " -f3

the output that i get is Server instead of the host name.


Solution

  • You could do this in a single grep command if it supports -P parameter.

    ct lsview -l abrodov_linux_vlsi07_test1 | grep -oP 'Server host:\s*\K\S+'
    

    Explanation:

    • Server host: Matches the string Server host:
    • \s* Matches zero or more spaces.
    • \K Discards the previously matched characters from getting printed. So in our case Server host: plus the following spaces got discarded.
    • \S+ Now the following one or more non-space characters would be matched and printed out.