The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
read list of filenames from text file and remove these files in multiple directories fxvisions Shell Programming and Scripting 5 08-07-2008 04:59 PM
grep on multiple words to match text template rider29 Shell Programming and Scripting 6 05-23-2008 12:21 PM
Replace text in multiple files on9west Shell Programming and Scripting 1 05-20-2008 02:35 AM
deleting lines from multiple text files vrms Shell Programming and Scripting 3 04-25-2008 12:01 PM
Replace text in multiple files Tonka52 Shell Programming and Scripting 10 03-24-2008 08:11 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 08-22-2002
coppertone coppertone is offline
Registered User
  
 

Join Date: Aug 2002
Posts: 1
Question grep multiple text files in folder into 1 text file?

How do I use the grep command to take mutiple text files in a folder and make one huge text file out of them. I'm using Mac OS X and can not find a text tool that does it so I figured I'd resort to the BSD Unix CLI for a solution... there are 5,300 files that I want to write to one huge file so that I can import it into a database in one shot... any help would be geratly appreciated. Thanx.
  #2 (permalink)  
Old 08-22-2002
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
Why do you have to use the grep command?

cd your_folder
for i in `ls *`
do
pg $i >> all
done
  #3 (permalink)  
Old 08-22-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,122
With 5,300 files, the `ls *` will probably exceed the max line length and pg is a slow way to cat a file.

Code:
#! /usr/bin/ksh
ls | while read filename ; do
      cat $filename
done > all
exit 0
will work. If the xargs program is available,

(ls * | xargs cat ) > all
will be a very fast solution.

I also wonder about the mention of grep. If you need to extract certain lines switch "cat" with "grep string" in both solutions.
  #4 (permalink)  
Old 08-22-2002
oombera's Avatar
oombera oombera is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2002
Location: Cleveland, OH
Posts: 804
Wow - I tried
(ls * | xargs cat) > all

It works the same and it's cool that it's so small.. but I keep getting the error:

cat: input/output files 'all' identical

The large file is created like it should be, but is there something additional to get rid of / suppress this error?
  #5 (permalink)  
Old 08-22-2002
Optimus_P Optimus_P is offline Forum Advisor  
flim flam flamma jamma
  
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
that error is just letting you know that it also tried to run on the "all" file and it failed.

nothing to worry about.
  #6 (permalink)  
Old 08-23-2002
crazykelso crazykelso is offline
Registered User
  
 

Join Date: Jul 2002
Posts: 7
error messages

the error message doesn't meen anything however if there are a lot of files in the folder it can be annoying to get all of those messages

you can get rid of those error messages by sending them to another file.

the command

ls * | xargs cat > all 2> /dev/null

works on my system (Sun,solaris)
it may work on yours also
  #7 (permalink)  
Old 08-23-2002
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,122
Quote:
Originally posted by oombera
Wow - I tried
(ls * | xargs cat) > all

It works the same and it's cool that it's so small.. but I keep getting the error:

cat: input/output files 'all' identical

The large file is created like it should be, but is there something additional to get rid of / suppress this error?
Ouch! First, that line has the same problem that your original solution did, we are asking the shell to replace * with a list of all files in the current directory. And that won't fly with 5,300 files. What I meant to type is:

(ls | xargs cat) > all

I also didn't think about the output file popping into existence before the ls ran. The best solution for that is something like:

(ls | xargs cat) > /some/dir/all

cat is smart enough to catch this, but grep isn't. And I'm still thinking that the op wanted to use grep to extract certain lines.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0