Find file dont have that string


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Find file dont have that string
# 8  
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.:
Code:
#!/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:-
Code:
::::::::::::::
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:-
Code:
$ 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
# 9  
Quote:
Originally Posted by bakunin
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:

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

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

I hope this helps.

bakunin
Supposed the -l option is standard, then the following is better
Code:
grep -lv search <fileglob>

But it is not the same as
Code:
grep -L search <fileglob>

if there are files with matching and not matching lines.
The following function emulates it
Code:
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>

This User Gave Thanks to MadeInGermany For This Post:
# 10  
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:
Code:
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:
Code:
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:
Code:
grep -Flv ...

is equivalent to:
Code:
grep -FL ...

and:
Code:
grep -Flvx ...

is equivalent to:
Code:
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
This User Gave Thanks to Don Cragun For This Post:
# 11  
Don, the difference is with empty files and multi-line files:
Code:
$ 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.
This User Gave Thanks to MadeInGermany For This Post:
# 12  
Quote:
Originally Posted by MadeInGermany
Don, the difference is with empty files and multi-line files:
Code:
$ 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.
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.
These 2 Users Gave Thanks to Don Cragun For This Post:
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #784
Difficulty: Easy
The firmware for the TRS-80 Model 100 was developed by Steve Jobs.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

2. Shell Programming and Scripting

Find string in file and find the all records by string

Hello I would like to get know how to do this: I got a big file (about 1GB) and I need to find a string (for instance by grep ) and then find all records in this file based on a string. Thanks for advice. Martin (12 Replies)
Discussion started by: mape
12 Replies

3. Shell Programming and Scripting

Find string in file and append new string after

Hi All, I'm trying to insert a string into a file at a specific location. I'd like to add a string after the parent::__construct(); in my file. <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); class MY_Controller extends CI_Controller { function... (6 Replies)
Discussion started by: jjkilpatrick
6 Replies

4. Shell Programming and Scripting

find string and replace with string in other file

Dear all, I need your help, I have file like this: file1:23456 01910964830098775635 34567 01942809546554654323 67589 26546854368698023653 09778 58716868568576876878 08675 86178546154065406546 08573 54165843543054354305 . .file2: 23456 25 34567 26 67589 27 (2 Replies)
Discussion started by: attila
2 Replies

5. UNIX for Dummies Questions & Answers

How to find a file if we dont know exact location of file ?

Hi I want know "How to find a file if we dont know exact location of file ?" Thanks, Tushar Joshi:) (9 Replies)
Discussion started by: tusharjoshi
9 Replies

6. Shell Programming and Scripting

Find multiple string in one file using find command

Hi, I want find multiple string in one file using find coomand. And keeping it in one variable.grep is not working. (5 Replies)
Discussion started by: vivek1489
5 Replies

7. Linux

Find String in FileName and move the String to new File if not found

Hi all, I have a question.. Here is my requirement..I have 500 files in a path say /a/b/c I have some numbers in a file which are comma seperated...and I wanted to check if the numbers are present in the FileName in the path /a/b/c..if the number is there in the file that is fine..but if... (1 Reply)
Discussion started by: us_pokiri
1 Replies

8. Shell Programming and Scripting

How to find a certain string in a file and replace it with a value from another file using sed/awk?

Hi Everyone, I am new to this forum and new to sed/awk programming too !! I need to find particular string in file1(text file) and replace it with a value from another text file(file2) the file2 has only one line and the value to be replaced with is in the second column. file 1: (assert (=... (21 Replies)
Discussion started by: paramad
21 Replies

9. UNIX for Dummies Questions & Answers

Search for Files that DONT contain a string

How do I search for files that dont contain a certain string? I am currently trying find ./logs -size +1c -exec grep -l 'Process Complete' {} \; -exec ls -l {} \; > $TOD Which gives me files that are reater han 0 file size and contain the string 'Process complete' but I want files that DONT... (13 Replies)
Discussion started by: tonydsam
13 Replies

10. Shell Programming and Scripting

dont't find right regex

I have a string which contains following information: <SZ.T><P ALIGN="CENTER"><FONT FACE="Arial, Helvetica, sans-serif" SIZE="+3">Bundesregierung nimmt sich dicke Deutsche vor</FONT></P></SZ.T> <SZ.UT><P ALIGN="CENTER"><FONT SIZE="+1"><I> Seehofer und Schmidt planen Kampagne gegen... (3 Replies)
Discussion started by: trek
3 Replies

Featured Tech Videos