Grep or cat The Whole Directory PROBLEMS :(


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Grep or cat The Whole Directory PROBLEMS :(
# 1  
Old 02-03-2015
Grep or cat The Whole Directory PROBLEMS :(

Hi Guys

This is my first post so I am not sure how things go here. I'm sorry if I'm breaking the rule or something. Feel free to correct me about that Smilie

So as I was saying...

I'd been trying to grep this folder containing 900,000 txt files but seems no luck. I get either "No such file or directory" or "Argument list too long" error each time I tried.

Here is the 2 final commands I used (I'm trying to grab all lines containing "[" btw)

"
Code:
find .location -name "*.txt" | while read file; do grep -f '\[' $file > grep.txt; done

"
[I get too many "No such file or directory"]

"
Code:
find .location -name "*.txt" -exec grep -l '\[' $file >> grep.txt '{}' \;

"
[No errors for this but it didn't do what it suppose to, only got 1 file result in the end.]

If grep is really impossible to achieve this, I do not mind using "cat" to compile all txt into one and "grep" that one big chunk of txt file. I'd also tried that but I could never escaped from "No such file or directory" or "Argument list too long" error.

Thank you so much for your help in advance guys. Smilie


Cheers

Last edited by Nexeu; 02-04-2015 at 02:38 AM..
# 2  
Old 02-03-2015
Hi what is it the you are trying to do? Get a list of files that contain a opening sqare bracket?

In the first command, grep should probably be a grep -l, rather than grep -f ?

In the second command the >> grep.txt redirection is in the wrong place
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 02-04-2015
Hi
Sorry, yes it was suppose to be
Code:
grep -l

. I tried with both
Code:
-f

and
Code:
-l

but still get the same error.

And yes I am trying to compile all the lines containing "[" into one txt file from 900,000.

I'd been trying to do it many ways but I always end up with "No such file or directory" or "Argument list too long" error Smilie Some txt file name contain special characters but I managed to convert all of them from ".html" to become ".txt" by using -exec . I was hoping that could do the same to this execution. But my coding knowledge is not that flexible Smilie

Last edited by Nexeu; 02-04-2015 at 01:58 AM..
# 4  
Old 02-04-2015
With the first approach try:
Code:
find .location -name "*.txt" | while read file; do grep -l '\[' "$file" ; done > grep.txt

or with the second approach ( which which I would expect to be faster )
Code:
find .location -name "*.txt" -exec grep -l '\[' {} + > grep.txt



--
You could also try and see if grep -Fl '[' rather than grep -l '\[' is any faster

Last edited by Scrutinizer; 02-04-2015 at 02:21 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 02-04-2015
THANK YOU SO MUCH! IT WORKED! Smilie No errors or whatsoever Smilie


I used this command :

Code:
find . -name "*.txt" -exec grep '\[' {} + > grep.txt

I removed -l to grab the content as with -l , it seemed to get only the directory.

Thank you so much! Smilie Been googling whole day yesterday until my brain almost explode. This forum is the best Smilie
# 6  
Old 02-04-2015
You're welcome. Careful with the "." and "grep.txt" . Maybe give it a file extension other than ".txt", otherwise it is also part of the find operation..
# 7  
Old 02-04-2015
yes I redirected the output text outside the working folder Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace cat and grep with <

Hello someone told me to use OS=`awk '{print int($3)}' < /etc/redhat-release` instead of OS=cat /etc/redhat-release | `awk '{print int($3)}'` any idea for the reason ? (5 Replies)
Discussion started by: nimafire
5 Replies

2. Shell Programming and Scripting

Problems with using grep to make backups to a different directory

I am trying to program a script that is using grep to find lines in a file in my home directory, and then use a pipe command to copy what is shown into different files in a different directory to create backup files. When I run it, though, it only creates a copy of the file that grep was going... (7 Replies)
Discussion started by: sammythesp3rmy
7 Replies

3. UNIX for Dummies Questions & Answers

Grep and cat combined

Hello, i need to search one word (snp1) from many files and copy the content of the columns of this word in new file. example: file 1: SNP BP CHR P snp1 1 3 0.01 snp2 2 2 0.05 . . file 2: SNP BP CHR P snp1 1 3 0.06 snp2 2 2 0.3 output... (6 Replies)
Discussion started by: biopsy
6 Replies

4. Shell Programming and Scripting

Problems on cat and substring replacement, shell

Hi I'm new in the Shell world, and I've been trying the whole day to get my script to work. I'm under Ubuntu 10.04. Let me explain: I want to list all the files matching some regex in a directory, and then make some some string replacement on the name of the files, and then a cat on the... (2 Replies)
Discussion started by: albh
2 Replies

5. Shell Programming and Scripting

grep or cat using sed

Is there a way using grep or cat a file to create a new file based on whether the first 9 positions of each record is less than 399999999? This is a fixed file format. (3 Replies)
Discussion started by: ski
3 Replies

6. UNIX for Dummies Questions & Answers

cat a list of directory paths only to a file

Hi! I would like to funnel a series of directories and subdirectories into a text file. This is the output I would like to see from a find command: /mypath/ABC_01/VISIT_01 /mypath/ABC_01/VISIT_02 /mypath/ABC_01/VISIT_03 /mypath/ABC_02/VISIT_01 /mypath/ABC_03/VISIT_01 I've tried: find... (2 Replies)
Discussion started by: goodbenito
2 Replies

7. Shell Programming and Scripting

cat -n and grep

I am not sure if using cat -n is the most efficient way to split a file into multiple files, one file per line in the source file. I thought using cat -n would make it easy to process the file because it produces an output that numbers each line that I could then grep for with the regex "^ *$i".... (3 Replies)
Discussion started by: kapu
3 Replies

8. Shell Programming and Scripting

cat /etc/passwd and grep -v on /etc/shells

Hi All, I'd like to do this cat /etc/passwd and grep -v on the /etc/shells list I'd like to find all shell that doesn't exist on the /etc/passwd. Is there an easy way without doing a egrep -v "/bin/sh|/bin/bash................"? How do I use a file /etc/shells as my list for... (4 Replies)
Discussion started by: itik
4 Replies

9. UNIX for Advanced & Expert Users

cat and grep not working

I am trying to cat a file and then grep that file for a number. I can do it fine on other files but this particular file will not do anything. I tried running it on an older file from the same device but it is just not working. The file is nothing more than a flat file on a unix box. Here is just a... (3 Replies)
Discussion started by: jphess
3 Replies

10. Shell Programming and Scripting

variable= 'cat file|wc -l' String problems

Hi, does anybody knows about wc -l, how to transform it inot a just number? this script ALWAYS executes the command3!!, However, the value of BMU_RUNNING is 1 case $BMU_RUNNING in *0) command1 ;; *1) command 2;; *)command 3;; esac The... (3 Replies)
Discussion started by: Santiago
3 Replies
Login or Register to Ask a Question