I have an utility script, that displays an information about deployed java app. Here is an example output of this script:
Name: TestAPP
Version : SNAPSHOT
Type : ear, ejb, webservices, web
Source path : /G/bin/app/TESTAPP_LIVE_1.1.9.1.1.ear
Status : enabled
Is it possible to grep Version and source path values using grep command? Right now im able to do this using following command:
| grep Version
But it outputs the whole string (e.g. Version: Snapshot) when i am need only a values (e.g Snapshot to use in further script commands)
grep Version | cut -d ':' -f 2
Here is a pure grep solution.
Use the -P option for regex mode, and -o option for retrieving only what is matching.
grep -Po "(?<=^Version : ).*"
Here is what you would do for Source:
grep -Po "(?<=^Source : ).*"
It uses a postive lookbehind.
Here's a solution using awk if you're interested:
grep Version | awk '{print $3}'
$3 means to print the third word from that line.
Note that:
This displays one word only
This assumes you have spaces between the colon (and therefore the version is actually the third "word"). If you don't, use $2 instead.
Related
I want to remove all files in a directory except some in UNIX. Part I desired files have a known name and for the other part, I'm using ls|grep command. But the ls | grep is working when there is only one occurrence and not when there is more than one. it is the same with find|grep. here are my commands:
rm -v !("R1.r"|"R2.r"|"r2.par"|$(ls|grep nario)|"sh.sh")
rm -v !("R1.r"|"R2.r"|"r2.par"|$(find|grep nario)|"sh.sh")
Is there any problem with my commands???
It looks like you're trying to use BASH specific extglob syntax. ls|grep won't work in the middle of the glob, because each pattern needs to be separated by a |, which won't happen with ls|grep. The easier way to do what you want is to use the shell globbing to find the files under the directory you're looking for instead. Make sure you do the following:
Are using BASH
Have extglob enabled: shopt -s extglob
Have globstar enabled: shopt -s globstar
Use file globbing rather than ls | grep
Then try again:
rm -v !("R1.r"|"R2.r"|"r2.par"|**/*nario*|"sh.sh")
Note: globstar requires BASH version 4 or higher.
I am new to linux and commands.
Basically I understand "grep" command.
But I do not understand what to do with following command, what it do, how to type command correctly.
grep -R -e
Examples to use correctly are welcome.
Calling grep with those flags mean search recursively in the specified directory and all it's children for lines that match a regex.
grep -R -e /p.t/ .
Should find all lines with a p and t that has any single character in between that are in the current directory or any of it's children.
-e PATTERN, --regexp=PATTERN
Use PATTERN as the pattern; useful to protect patterns beginning
with -.
-R, -r, --recursive
Read all files under each directory, recursively; this is equiv-
alent to the -d recurse option.
http://unixhelp.ed.ac.uk/CGI/man-cgi?grep
I would like to extract the first part of this hostname testsrv1
from testsrv1.main.corp.loc.domain.com in UNIX, within a shell script.
What command can I use? It would be anything before the first period .
Do you have the server name in a shell variable? Are you using a sh-like shell? If so,
${SERVERNAME%%.*}
will do what you want.
You can use cut:
echo "testsrv1.main.corp.loc.domain.com" | cut -d"." -f1
To build upon pilcrow's answer, no need for new variable, just use inbuilt $HOSTANME.
echo $HOSTNAME-->my.server.domain
echo ${HOSTNAME%%.*}-->my
Tested on two fairly different Linux's.
2.6.18-371.4.1.el5, GNU bash, version 3.2.25(1)-release (i386-redhat-linux-gnu)
3.4.76-65.111.amzn1.x86_64, GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
try the -s switch:
hostname -s
I use command cut, awk, sed or bash variables
Operation
Via cut
[flying#lempstacker ~]$ echo "testsrv1.main.corp.loc.domain.com" | cut -d. -f1
testsrv1
[flying#lempstacker ~]$
Via awk
[flying#lempstacker ~]$ echo "testsrv1.main.corp.loc.domain.com" | awk -v FS='.' '{print $1}'
testsrv1
[flying#lempstacker ~]$
Via sed
[flying#lempstacker ~]$ echo "testsrv1.main.corp.loc.domain.com" | sed -r 's#([^.]*).(.*)#\1#g'
testsrv1
[flying#lempstacker ~]$
Via Bash Variables
[flying#lempstacker ~]$ hostName='testsrv1.main.corp.loc.domain.com'
[flying#lempstacker ~]$ echo ${hostName%%.*}
testsrv1
[flying#lempstacker ~]$
You could have used "uname -n" to just get the hostname only.
You can use IFS to split text by whichever token you want. For domain names, we can use the dot/period character.
#!/usr/bin/env sh
shorthost() {
# Set IFS to dot, so that we can split $# on dots instead of spaces.
local IFS='.'
# Break up arguments passed to shorthost so that each domain zone is
# a new index in an array.
zones=($#)
# Echo out our first zone
echo ${zones[0]}
}
If this is in your script then, for instance, you'll get test when you run shorthost test.example.com. You can adjust this to fit your use case, but knowing how to break the zones into the array is the big thing here, I think.
I wanted to provide this solution, because I feel like spawning another process is overkill when you can do it easily and completely within your shell with IFS. One thing to watch out for is that some users will recommend doing things like hostname -s, but that doesn't work in the BSD userland. For instance, MacOS users don't have the -s flag, I don't think.
Assuming the variable $HOSTNAME exists, so try echo ${HOSTNAME%%.*} to get the top-most part of the full-qualified hostname. Hope it helps.
If interested, the hint is from the below quoted partial /etc/bashrc on a REHL7 host:
if [ -e /etc/sysconfig/bash-prompt-screen ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
else
PROMPT_COMMAND='printf "\033k%s#%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
fi
;; ... ```
I want to find the lines in which atleast one of the strings among string1 and string2 are present in the file.
grep 'string1' 'string2' file;-this is an error even though!!
how could i do the above on unix command line?
If your version of grep supports the -E flag (e.g. the GNU version), you can use extended regular expressions, which allows you to perform queries like this:
$ grep -E 'string1|string2'
or
$ grep -E 'string[12]'
See pcresyntax(3) and pcrepattern(3) for further information on PCRE (Perl Compatible Regular Expressions).
Found it!!
grep -e 'string1' -e 'string2' file;
Im using Ubuntu Karmic as my operating system . I frequently need to search my project folder for a particular string, to see if its there in any of the files in the project folder or its subfolders.
I currently use the find command to do that, and have written a script that accepts the string im looking for as the parameter.
find . -exec grep -l $1 {} \;
But the problem with this is that it does not work with strings having a space in them. So, is there any way to search for space separated strings as well, or is there any available tool that does the job ?
Thank You.
How are you invoking your script?
If you want to search for space separated strings you need to do the
invocation in the form:
%./script_name.sh 'search string'
and also change the find invocation to :
find . -exec grep -l "$1" {} \;
A better version of that command is simply grep -rl "$1" ., or possibly grep -rl "$*" ..
If your string contains the correct amount of space, and the problem is simply the shell parsing the arguments, then you can refer to every arg with "$*" and you can prevent the shell from breaking at word boundaries (but still allow parameter expansion) by using the soft double-quotes.
grep -R "phrase with spaces" /folder/folder
find / -type d -name "folder name" 2> /dev/null
I believe you may simply do grep -lR ${1} . to achieve what you need.