searching multiple strings in a single file - unix

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;

Related

Remove all files except some with constant name and some containing a special in unix

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.

Omit "Is a directory" results while using find command in Unix

I use the following command to find a string recursively within a directory structure.
find . -exec grep -l samplestring {} \;
But when I run the command within a large directory structure, there will be a long list of
grep: ./xxxx/xxxxx_yy/eee: Is a directory
grep: ./xxxx/xxxxx_yy/eee/local: Is a directory
grep: ./xxxx/xxxxx_yy/eee/lib: Is a directory
I want to omit those above results. And just get the file name with the string displayed. can someone help?
grep -s or grep --no-messages
It is worth reading the portability notes in the GNU grep documentation if you are hoping to use this code multiple places, though:
-s
--no-messages
Suppress error messages about nonexistent or unreadable files. Portability note: unlike GNU grep, 7th Edition Unix grep did not conform to POSIX, because it lacked -q and its -s option behaved like GNU grep’s -q option.1 USG-style grep also lacked -q but its -s option behaved like GNU grep’s. Portable shell scripts should avoid both -q and -s and should redirect standard and error output to /dev/null instead. (-s is specified by POSIX.)
Whenever you are saying find ., the utility is going to return all the elements within your current directory structure: files, directories, links...
If you just want to find files, just say so!
find . -type f -exec grep -l samplestring {} \;
# ^^^^^^^
However, you may want to find all files containing a string saying:
grep -lR "samplestring"
Exclude directory warnings in grep with the --exclude-dir option:
grep --exclude-dir=* 'search-term' *
Just look at the grep --help page:
--exclude-dir=PATTERN directories that match PATTERN will be skipped.

Matching a date in a file on Unix

I have the following unix command, which I'm using to try finding a date in the format yyyy-mm-dd in a file:
grep -i -w [\d]{4}-[\d]{2}-[\d]{2}? <filename>
but for some reason I'm getting an empty answer. Am I matching the regex correctly for grep?
The following is working, using bash extended regex (-E, --extended-regexp):
grep -E -i -w "[0-9]{4}-[0-9]{2}-[0-9]{2}" <filename>
But, in this case you should use [0-9] instead of \d.
If you want to use \d, you need to specify the PERL regex (-P, --perl-regexp):
grep -P -i -w "\d{4}-\d{2}-\d{2}" <filename>
You could try the below grep commands,
grep -oP '\d{4}-\d{2}-\d{2}' file
OR
$ grep -oE '[0-9]{4}-[0-9]{2}-[0-9]{2}' file
OR
grep -o '[0-9]\{4\}-[0-9]\{2\}-[0-9]\{2\}' file

How to correctly use the "grep" command

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

Unix grep command

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.

Resources