cmd:fgrep 

fgrep -f tl *.txt
grep -F -f - $tmpf

cmd:grep and lists 

Quick helps 

-l, --files-with-matches
       Suppress  normal  output; instead print the name of each input
       file from which output would normally have been printed.   The
       scanning will stop on the first match.
-L, --files-without-match
       Suppress  normal  output; instead print the name of each input
       file from which no output would normally  have  been  printed.
       The scanning will stop on the first match.

Synopsis 

There is no way for grep to search stringz input (produced by 'find -print0' etc). The -Z, —null option is only for file name output:

-Z, --null
       Output a zero byte (the ASCII NUL character)  instead  of  the
       character  that  normally  follows  a file name.  For example,
       grep -lZ outputs a zero byte after each file name  instead  of
       the  usual newline.

Solution Synopsis 

The solution is not to use stringz but normal input, then work on the grep search output:

tr '\n' '\0' | xargs -0

regular expression summery 

Subject:     Re: Can anybody make some summery here?
Newsgroups:  comp.unix.shell
Date:        Tue, 05 Dec 2006 08:58:55 GMT
>   when I use regular expression in the grep, perl, sed.
>   awk commands, I feel there are some differences in
>   them. Could anyone point them out or give me some
>   document to find what they are?

standard grep, expr, sed, more, vi, ed, pax, ex, csplit, nl use BRE (basic regular expressions). awk and grep -E (formerly egrep) use ERE (extended regular expressions). lex uses ERE with additions/restrictions.

See http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html for the standard for BRE and ERE.

Some implementations of those tools have extensions over the standard that you shouldn't use in scripts but should be described in their respective manuals.

perl has its own regexps (also found (similar) in tools that use the PCRE library as/or in php, newer TCLs, zsh, exim, apache and more). Run "perldoc perlre" or "man perlre".

vim has yet another sort of regular expressions that provide with the same functionalities as perl's but with a different syntax. AT&T ksh has AT&T regexps support…

Stephane CHAZELAS

replace string with another 

Newsgroups:  comp.unix.shell
Date:        Mon, 4 Dec 2006 21:47:26 +0000
> I need to replace one word with another in all the files under a given
> directory and not its sub-directories.[...]
>
> in my cheat sheet I have the following to find all occurrence of
> "string" in all files under a given directory and its sub_directories.
> $ grep -FHwire string /path/
>
> as well as this perl command to replace strings
> # perl -pie 's/old/new/' /path-to-file
>
> but I am sure there is a more efficient/easier way than to work out a
> syntheses of the 2 commands above.

As you seem to have a GNU find:

grep -FZ old /path/* | xargs -r0 perl -pi -e 's/old/new/g'

With standard utilities:

cd /path &&
 find . ! -name . -prune -type f -exec grep -q old {} \; \
 -exec sh -c '
   cp -i "$1" "$1.back" &&
   sed 's/old/new/g' < "$1.back" > "$1" &&
   rm "$1.back"' {} {} \;

(untested).

Stephane CHAZELAS

Replace several lines by a line 

Newsgroups:  comp.unix.shell
Date:        Thu, 16 Jun 2005 23:04:04 +0200

In a file, I want to replace first line which begins with "string1" to the next line which begins with "string2" by just a line with follwing text "TEXT DELETED".

For example, test_file.c :

line 1:  ..........................
line 2:  ..........................
line 3:  string0...................
line 4:  string1................... <----------------------
line 5:  .......................... <----------------------
line 6:  .......................... <----------------------
line 7:  string2................... <----------------------
line 8:  string3...................
line 9:  string4...................

After having to launch shell-script, the file becomes :

line 1:  ..........................
line 2:  ..........................
line 3:  string0...................
line 4:  TEXT DELETED               <----------------------
line 5:  string3...................
line 6:  string4...................

Somebody would know how to make with a shell-script ?

Replace several lines by a line 

perl -i.bak -lne'
$a=/^string1/.../^string2/,$a eq 0+$a?next:s/^string2/TEXT DELETED/,print
' test_file.c

John W. Krahn

Replace several lines by a line 

awk '/string1/{print "TEXT DELETED"}/string1/,/string2/{next}1' file

Ed Morton

Replace several lines by a line 

sed '/string1/,/string2/c\
TEXT DELETED' file > newfile

Bill Marcum

Replace several lines by a line 

$ jot 9 | sed -e '/4/,/7/c\' -e 'TEXT DELETED'
1
2
3
TEXT DELETED
8
9

tong