grep the word if matching with condition - linux

I have 2 words "staticed2f.css" and staticec3f.css . The bold char do change offen. I want to grep these words matching 'static' using grep command or any other script
Thank you

Try this,
grep -o \static[a-zA-Z0-9]*\.css
To change with other word,
var="otherword"
grep -o \(static\|$var)[\-a-zA-Z0-9.]*\.(css|js)

Related

GREP to show files WITH text and WITHOUT text

I am trying to search for files with specific text but excluding a certain text and showing only the files.
Here is my code:
grep -v "TEXT1" *.* | grep -ils "ABC2"
However, it returns:
(standard input)
Please suggest. Thanks a lot.
The output should only show the filenames.
Here's one way to do it, assuming you want to match these terms anywhere in the file.
grep -LZ 'TEXT1' *.* | xargs -0 grep -li 'ABC2'
-L will match files not containing the given search term
use -LiZ if you want to match TEXT1 irrespective of case
The -Z option is needed to separate filenames with NUL character and xargs -0 will then separate out filenames based on NUL character
If you want to check these two conditions on same line instead of anywhere in the file:
grep -lP '^(?!.*TEXT1).*(?i:ABC2)' *.*
-P enables PCRE, which I assume you have since linux is tagged
(?!regexp) is a negative lookahead construct, so ^(?!.*TEXT1) will ensure the line doesn't have TEXT1
(?i:ABC2) will match ABC2 case insensitively
Use grep -liP '^(?!.*TEXT1).*ABC2' if you want to match both terms irrespective of case
(standard input)
This error is due to use of grep -l in a pipeline as your second grep command is reading input from stdin not from a file and -l option is printing (standard input) instead of the filename.
You can use this alternate solution in a single awk command:
awk '/ABC2/ && !/TEXT1/ {print FILENAME; nextfile}' *.* 2>/dev/null

Grep multiple strings

I want to find a list of files that have A but do not have B and C.
grep -r -L 'B\|C' finds the ones without B and C, but how do I add the condition of having A as well?
If I understand your question correctly:
grep -l "A" $(grep -r -E -L "B|C" *)
i.e. search for files containing "A" in the list of files that your original command generates.
You can use negative lookahead in grep using options -P or --perl-regexp
grep -r -P -L '^(?!.*A).*$|B|C'
If I understood your question correctly, you can do it like this:
grep "A" file.txt | grep -v -e "B" -e "C"
The first grep finds lines containing A, the second greptakes the result and removes lines containing either "B" or "C". This works by the -v flag which inverses matches.

How to get the no of matched occurrences using grep command in linux?

If we use grep -c option it will give you the each occurrences only once per line. But I need the total no of matched occurrences not line count.
Use this
grep -o pattern path | wc -l
You can use -o flag to output only the matched part and then pipe it to wc -w to get word count.
Eg: ls ~ | grep -o pattern | wc -w

grep url pattern matching

I'm looking to count url pattern in access log like
action.php?show_page=next&offset=1&xyzzzzz
Note that I need all url where offset values are between 1 to 9. Examples:
action.php?show_page=next&offset=1&xyzzzzz
action.php?show_page=next&offset=2&xyzzzzz
action.php?show_page=next&offset=3&xyzzzzz
.............
action.php?show_page=next&offset=9&xyzzzzz
This is what I tried:
grep "action.php?show_page=next" access.log.2 | grep "offset=[1-9]&"| wc -l
One way using grep:
grep -oc "action.php?show_page=next&offset=[1-9]&xyzzzzz" file.txt
You should scape the "?" of the first grep.
try with the regex:
action.php\?show_page=next&offset=[1-9]

Removing the </p> tag from grep output

I have I bash script that will find phones numbers inside .htm or .html files in a directory (or recursivly down if I want it) to find phone numbers in the format (ddd)ddd-dddd or ddd-ddd-dddd (Where d represents a digit).
This is my code:
find ./ -maxdepth 1 -regex ".*\(html\|htm\)$" | xargs grep '\(([0-9]\{3\})\|[0-9]\{3\}\)[-]\?[0-9]\{3\}-[0-9]\{4\}'
The output is:
./dash_only_phone.htm:800-555-1212</p>
./paren_phone.htm:(800)555-1212</p>
I was wondering how I would change the grep command to remove the html p tag printout at the end.
Thanks,
If your grep supports Perl Compatible Regular Expressions, as do GNU and OS X grep:
grep -Po '(\([0-9]{3}\)|[0-9]{3})-?[0-9]{3}-[0-9]{4}(?=</p>)'
Note the changes in escaping (which are similar to or the same as for grep -E).
Why not just pass the output through a sed filter to remove it, as in the following transcript:
pax$ echo './dash_only_phone.htm:800-555-1212</p>' | sed 's?</p>$??'
./dash_only_phone.htm:800-555-1212
This will get rid of any </p> sequences that appear at the end of a line.
You can just add the -o switch to get the IP
find ./ -maxdepth 1 -regex ".*\(html\|htm\)$" | xargs grep -o '\(([0-9]\{3\})\|[0-9]\{3\}\)[-]\?[0-9]\{3\}-[0-9]\{4\}'

Resources