Find file dont have that string

I have 13 text files and almost all of them contain the same string.
but some file has diffrent string inside.
I want to send that file which has a diffrent string inside

Hello Sagar,

Request you to please do always provide 3 important things required by the forums.

1st- Sample of Input_file(which has your sample input(s) etc) in CODE TAGS.

2nd- Sample of your expected output(which should come after running script/code) in CODE TAGS too.

3rd- Most important thing, your efforts. We encourage all users to add their efforts which they have put in order to solve their own problems in their posts in CODE TAGS, don't bother either your written code(s) are correct or wrong just post them in your posts and when you get guidance from this community then you could see where you were lacking or missing the things etc and that's the thought behind it(LEARNING).

Kindly do mention all details mentioned above clearly in your post and do let us know I am pretty sure you will get good guidance on this site, Keep learning, keep sharing on this site, cheers :b:

Thanks,
R. Singh

Hello R.Singh sir,

file01
Hello Sagar

file02
Hello Sagar

file03
Hello Rohit

file04
Hello Sagar

file 05
Hello Vishal

file06 

file07
Hello Sagar

++++++++++++++++++++++++++++++++++++++++++++++++++

These are my 7 input file,
I want the file which does not contain "Hello Sagar" as an Output file

Code Used by me

grep "Hello Sagar"  file0?     

to get the file contain "Hello Sagar"
After that i am unable to proceed

Hello Sagar Singh,

Could you please try following(not tested it though).

grep -L "Hello Sagar" *

Thanks,
R. Singh

Input files

file01

Hello Sagar

file02

Hello Sagar

file03

Hello Rohit

file04

Hello Sagar

file 05

Hello Vishal

file06

file07

Hello Sagar

=======================================================

Output required

file03
Hello Rohit

file 05
Hello Vishal

file06 

==============================

how can I get that files as output which do not contain " Hello Sagar "

Hello Sagar Singh,
I am having difficulty understanding what you are trying to do.

In post #1 you said you have thirteen files to search. In post #3 you said you have seven files and sort of showed us their contents. It seems that you are saying that each of your input files has a filename that is the string file followed by two octal or decimal digits, each file contains exactly one line of text, and if that line of text is not " Hello Sagar you want to print the filename and the contents of each file that does not contain that text and only that text with each output filename and contents separated from other filenames and their contents by an empty line of text.

But since none of your sample input files contain the leading <space> character that you say must be present, it would seem that the output should include every input file that you have shown us.

Have I understood your requirements correctly?

If you are not looking for a leading <space> character before " Hello Sagar " and just want a list of lines in file that do not contain the fixed string " Hello Sagar ", then the command:

grep -Fv "Hello Sagar"

would give you a list of files that contain lines that do not match your pattern with the filename prepended to each line that does not match.

If you just want the filenames of files that do not contain your pattern, the code Ravinder suggested my be exactly what you want, but the grep -L option is an extension to the standards and will not be available on all systems. And, since you haven't told us what operating system you're using, we can't know if his suggestion will work in your environment.

If you really want the output to contain empty lines between the filenames and contents of each file that does not contain any line that matches your pattern, we could help you write a shell script that would do that, but we would need to know what shell you are using as well as what operating system you're using.

In your first post in this thread you said: "I want to send that file which has a diffrent string inside". Where do you want to send those files?

Please help us help you by providing details about the operating system you' are using, the shell you are using, more details about the format of the files you are processing, and an English description of the output you are trying to produce.

1 Like

All grep implementations i have seen so far have ":" (colon) as the separator between the matched contents and the filename (to be honest i don't know if this is required by the standards or just convention), so:

grep -Fv <fileglob here> | cut -d':' -f1

should provide a standards-conformant alternative to grep -L , no??

I hope this helps.

bakunin

Hello Sagar Singh,

These are obviously demonstration files to me. Will the live files actually have more than just a single line with/without Hello Sagar in them?

You might need a simple loop to check each in turn and return the filenames you are looking for, e.g.:

#!/bin/bash

# Define list of files.  This could be pattern matched if more appropriate
file_list="file01 file02 file03 file04 file05 file06 file07"

# This can be an extended regular expression, depending on your needs
search_expression="Hello Sagar"

for file in ${file_list}                                     # Not quoted to allow for filename expansion, however will have a problem with filenames containing whitespace.
do
   grep -Eq "${search_expression}" "${file}" || printf "File %s does not contain expression \"%s\"\n"  "${file}"  "${search_expression}"
done

From the following input files shown with more f*|more :-

::::::::::::::
file01
::::::::::::::
Hello Sagar
::::::::::::::
file02
::::::::::::::
Hello Sagar
I will ignore this file
::::::::::::::
file03
::::::::::::::
This file should be found
::::::::::::::
file04
::::::::::::::
Not starting the line Hello Sagar
::::::::::::::
file05
::::::::::::::
Hello Sagar not ending the line
::::::::::::::
file07
::::::::::::::
Hello Sagar

..... I get the following output from the above:-

$ pwd
/tmp/unix281673
$ ./search
File file03 does not contain expression "^Hello Sagar$"
File file04 does not contain expression "^Hello Sagar$"
File file05 does not contain expression "^Hello Sagar$"
grep: file06: No such file or directory
File file06 does not contain expression "^Hello Sagar$"

If this gives you the sort of thing you need, you may care to extend it to check that the files exists before running the grep just to tidy it up.

Does this help?

Robin

Supposed the -l option is standard, then the following is better

grep -lv search <fileglob>

But it is not the same as

grep -L search <fileglob>

if there are files with matching and not matching lines.
The following function emulates it

grep_L(){
 grep -l "$@" |
 (
 shift
 read g
 for a
 do
  if [ "$a" != "$g" ]
  then
   echo "$a"
  else
   read g
  fi
 done
 )
}

grep_L search <fileglob>
1 Like

Hi Robin, MadeInGermany, & bakunin,
Isn't it amazing how hard it is to understand how a common utility like grep works even though we all use it several times a day?

For the record, the standard NAME and SYNOPIS sections for grep are:

NAME
        grep - search a file for a pattern
SYNOPSIS
        grep [�'E|�'F] [�'c|�'l|�'q] [�'insvx] �'e pattern_list
            [�'e pattern_list]... [�'f pattern_file]... [file...]

        grep [�'E|�'F] [�'c|�'l|�'q] [�'insvx] [�'e pattern_list]...
            �'f pattern_file [�'f pattern_file]... [file...]

        grep [�'E|�'F] [�'c|�'l|�'q] [�'insvx] pattern_list [file...]

First note that the vertical bars between -E and -F and between -c , -l , and -q indicate that grep has two sets of mutually exclusive options. And since nothing in the description of grep says otherwise, this means that if you invoke grep with both of those capital letter options or if you invoke grep with more than one of those three mutually exclusive lowercase options, the behavior is completely undefined.

The standard requires that when none of the -c , -l , and -q options is specified AND two or more file operands are present when grep is invoked, each line meeting the criteria will be output on a line that starts with the name of the file it came from immediately followed by a <colon> character. And, if the -n option is also specified, that is followed by the line number of the line in that was selected immediately followed by a colon. And then, the contents of the selected line are also written to that output line. It is not clear to me exactly what the requirements are if the -n option is specified along with one of the three mutually exclusive lowercase letter options.

As you can see, the -l option is included in the standard, but the output it produces is just the names of files that contain at least one line that meets the specified match criteria. Unfortunately this means that one of:

grep -Flv 'Hello Sagar' file??
grep -Flvx 'Hello Sagar' file??
grep -FL 'Hello Sagar' file??
grep -FLx ' Hello Sagar' file??
grep -Flv ' Hello Sagar' file??
grep -Flvx ' Hello Sagar' file??
grep -FL ' Hello Sagar' file??
grep -FLx ' Hello Sagar' file??

probably does exactly what Sagar Singh wants, but we don't know if the grep utility on his system recognizes the -L option, we aren't sure whether the fixed string to be used for the search pattern should contain a leading <space>, and we aren't sure whether Sagar Singh wants to know about lines that do not contain the search pattern, about lines that are not an exact match (with no other leading or trailing characters), nor whether any of his seven or thirteen input files might be empty files.

Note that if and only if there its no possibility that an empty file will ever be evaluated for this project, then:

grep -Flv ...

is equivalent to:

grep -FL ...

and:

grep -Flvx ...

is equivalent to:

grep -FLx ...

If there might be empty files and the requirement is to list files that do not contain any line meeting the match criteria, the commands using the -l instead of the -L option will not fully meet the requirements. But, I am hesitant to try to reinvent the -L option until Sagar Singh confirms that that is what is needed and that grep on the system he is using does not already support the -L option.

Hello Sagar Singh,
Please tell us what operating system and shell you're using and clarify exactly what you are trying to do so we can help you create a script that will meet your needs.

Sincerely,
Don

1 Like

Don, the difference is with empty files and multi-line files:

$ printf "a\n" > a1
$ printf "b\n" > b1
$ printf "a\nb\n" > ab2
$ printf "" > e0
$ grep -FL "a" a1 b1 ab2 e0
b1
e0
$ grep -Flv "a" a1 b1 ab2 e0
b1
ab2

Only the one-line files a1 and b1 behave the same.

1 Like

Yes. I apologize for being vague. What I should have said was:

The grep -Flv "a" ... will list a file operand if that file contains one or more lines that do not contain "a".

The grep -FL "a" ... will list a file operand if that file does not contain any lines that contain "a".

No matter what pattern you are searching for, grep -FL pattern file... will always list every file operand that names an empty file and grep -Flv pattern file... will never list any file operand that names an empty file.

2 Likes