recurse grep and cut and paste

recurse grep and cut and paste

Post by vi » Fri, 11 Jun 2004 21:55:46



Hello,

I am running into this issue of cut and paste, and would like to know
of any possible solution to this problem. Below is the problem

I have a bunch of files x.txt under /export/home/myhome. The contents
of each x.txt vary within each sub diretory. i want to recursively
grep for a particular string "/a/b/c" in x.txt and then cut and paste
the lines that contain the string "/a/b/c" into another file called
y.txt, which happens to be present under /export/home/yourhome. So in
essense there are multiple x.txt files under /export/home/myhome and
multiple y.txt under /export/home/yourhome.

Appreciate any help

Thanks

 
 
 

recurse grep and cut and paste

Post by Alan Conno » Sat, 12 Jun 2004 08:50:25



Quote:

> Hello,

> I am running into this issue of cut and paste, and would like to know
> of any possible solution to this problem. Below is the problem

> I have a bunch of files x.txt under /export/home/myhome. The contents
> of each x.txt vary within each sub diretory. i want to recursively
> grep for a particular string "/a/b/c" in x.txt and then cut and paste
> the lines that contain the string "/a/b/c" into another file called
> y.txt, which happens to be present under /export/home/yourhome. So in
> essense there are multiple x.txt files under /export/home/myhome and
> multiple y.txt under /export/home/yourhome.

> Appreciate any help

> Thanks

Would you mind re-describing, in careful detail, what you want to do?

AC

--
http://angel.1jh.com./nanae/kooks/alanconnor.html
http://www.killfile.org./dungeon/why/connor.html

 
 
 

recurse grep and cut and paste

Post by Ed Morto » Sat, 12 Jun 2004 09:02:36



> Hello,

> I am running into this issue of cut and paste, and would like to know
> of any possible solution to this problem. Below is the problem

> I have a bunch of files x.txt under /export/home/myhome. The contents
> of each x.txt vary within each sub diretory. i want to recursively
> grep for a particular string "/a/b/c" in x.txt and then cut and paste
> the lines that contain the string "/a/b/c" into another file called
> y.txt, which happens to be present under /export/home/yourhome. So in
> essense there are multiple x.txt files under /export/home/myhome and
> multiple y.txt under /export/home/yourhome.

> Appreciate any help

Something like this:

cd /export/home/myhome
find . -name x.txt -print | while IFS= read xtxt
do
        dir=`dirname "$xtxt"`
        grep "/a/b/c" "$xtxt" > "/export/home/yourhome/$dir/y.txt"
done

Regards,

        Ed.

Quote:> Thanks

 
 
 

recurse grep and cut and paste

Post by Stephane CHAZELA » Sat, 12 Jun 2004 17:00:39


2004-06-10, 19:02(-05), Ed Morton:
[...]
Quote:> cd /export/home/myhome
> find . -name x.txt -print | while IFS= read xtxt

[...]

Note that IFS= is not necessary as the lines start with "./" and
end with "/x.txt" anyway (except if some directories contain
newline characters, but then the script would be broken anyway,
best is to use "-exec" or zsh). But it doesn't harm anyway.

On the contray, directories might contain backslash characters,
and then you need the "-r" option to read. I know it's silly but
that's how shells were designed (not to be programming languages
in the first place): the way to read one line (up to \n or end
of file) of (text, read can't cope with binary data ('\0'
character)) input is:

IFS= read -r

read -r: is to read a line where leading and trailing blanks are
removed

read: is to read a line where a "\" is supposed to be used as a
line continuation character (and skipped if not followed by a
"\n") (and leading and trailing blank removed).

--
Stephane

 
 
 

recurse grep and cut and paste

Post by vi » Sun, 13 Jun 2004 04:09:14



> 2004-06-10, 19:02(-05), Ed Morton:
> [...]
> > cd /export/home/myhome
> > find . -name x.txt -print | while IFS= read xtxt
> [...]

> Note that IFS= is not necessary as the lines start with "./" and
> end with "/x.txt" anyway (except if some directories contain
> newline characters, but then the script would be broken anyway,
> best is to use "-exec" or zsh). But it doesn't harm anyway.

> On the contray, directories might contain backslash characters,
> and then you need the "-r" option to read. I know it's silly but
> that's how shells were designed (not to be programming languages
> in the first place): the way to read one line (up to \n or end
> of file) of (text, read can't cope with binary data ('\0'
> character)) input is:

> IFS= read -r

> read -r: is to read a line where leading and trailing blanks are
> removed

> read: is to read a line where a "\" is supposed to be used as a
> line continuation character (and skipped if not followed by a
> "\n") (and leading and trailing blank removed).

Thanks guys, script works....