mutliple files in the same directory


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting mutliple files in the same directory
# 1  
Old 05-12-2008
CPU & Memory mutliple files in the same directory

I have over 900 files that have the same name except for a unique numeric assignment. For all files I would like to cut the 2nd column and paste all into one new file. All in bash.
sample input format for each file:
1 2 3
1 2 3
1 2 3

sample command for what I want to do:
cut -d' ' -f2 file_in >> file_out

Ultimately I would like process the files through a loop.

Thanks in advance!!!
Edit/Delete Message
# 2  
Old 05-12-2008
With that many files * might overflow the exec limits on command length...
Code:
cd /path/to/files
find . -type f  -name 'common_name_fragment*' | \
while read file
do
     cut -d' ' -f2 $file
done > ./tmp
mv ./tmp file_out

# 3  
Old 05-12-2008
What's the temporary file for? Couldn't you just as well redirect to file_out directly?

And of course, if your shell can expand all files, the simple thing might work just fine:

Code:
cut -d ' ' -f2 * >file_out

If you get "argument list too long", you need a workaround like the find command which jim posted.
# 4  
Old 05-12-2008
CPU & Memory

#!/bin/bash
for file in /home/epi/tmurray/reich_stuff/21
do cut -d' ' -f2 *.21 >> out.txt ; done

The above loop does the extraction but appends the column from each file vertically. How can I get it to append horizontally? e.g:
input:
file 1
1 2
1 2
1 2

file 2
1 2
1 2
1 2

current output
2
2
2
2
2
2

desired output:
2 2
2 2
2 2

thanks.

And Jim --that code you gave me doesn't work. I tweaked it and it still doesn't work. I keep getting errors about synthax


ThanksSmilie
# 5  
Old 05-12-2008
Your "loop" only loops over the directory, once, then ignores the directory when actually doing the cut, but never mind.

cut has a friend paste which places stuff next to each other. For a large number of files, it might not be workable, though; you'd need a temporary file, or a temporary stream, for each file in the set.

Sounds to me like at this point you would be best served by a simple awk or perl script.

I don't see anything wrong with the syntax in jim's command; can you post the actual error message?
# 6  
Old 05-12-2008
For a small number of files, this works, at least roughly, but it causes my Bash to dump core with a malloc error when I try it on a large number of files. I'm posting it mainly for its curiosity value.

Code:
eval paste "<("cut -d\" \" -f2 `echo *  | sed 's/ /) <(cut -d " " -f2 /g'`")"

This constructs a command line consisting of paste <(cut file1) <(cut file2) <(cut file3) via some rather black magic. The key is really the use of eval and the quoting and backslashes required to pull it off.

Last edited by era; 05-12-2008 at 05:25 PM.. Reason: Belated typo fix
# 7  
Old 05-12-2008
And here finally is a simple Perl one-liner (or to be really honest, two-liner) which collects an array for each input line number, and at the end prints out each array in line number order.

Code:
perl -ane 'push @{$L->[$.]}, $F[1]; close ARGV if eof;
END { shift @{$L}; for $l (@{$L}) { print join (" ", @{$l}), "\n"; } }' *

It's regrettable that you can't do this with just the basic Unix tools.

The close stuff is to reset the line numbering for each file (see perldoc -f eof) and the shift is to get rid of line number zero, which should be empty anyway. $F[1] is the second, space-padded input field, in case you want to change it to something else (Perl arrays are zero-based, so the first field is $F[0]). It's easy enough to make it split on a different separator if you like; see the -a and -F options. This uses references for efficiency (or simply because I'm past bedtime), so it's not particularly elegant or readable.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directory containing files,Print names of the files in the directory that are exactly same content.

Given a directory containing say a few thousand files, please output a list of all the names of the files in the directory that are exactly the same, i.e. have the same contents. func(a_directory_name) output -> {“matches”: , ... ]} e.g. func(“/home/my/files”) where the directory... (7 Replies)
Discussion started by: anuragpgtgerman
7 Replies

2. Shell Programming and Scripting

Load mutliple files into Netezza table

Hi All, I get ~1000 files everyday and the current unix script process one file at a time... like it unzips the file, loads into table . This process will repeat how many ever files were present in the directory. I was wondering can we process multiple files (like unzipping and loading X amount... (1 Reply)
Discussion started by: rajasony
1 Replies

3. Shell Programming and Scripting

Print mutliple patterns in a line using sed

Hi, I am trying to print multiple patterns in a line using sed. But it is printing only the last occurance of a pattern. If the line is the the output should be Lookup Procedure|Stored proc But the output I am getting is Stored proc The code I am using is echo... (9 Replies)
Discussion started by: kedar_laveti
9 Replies

4. Shell Programming and Scripting

Awk with mutliple filed separators

I try to trim a FTP log using awk, but the escape sequences does not work the way I want it. Input data Wed 3Oct10 21:48:00 - (002117) Sent file d:\ftp\home\tools\htmake.rar successfully (48.2 kB/sec - 40997 Bytes) Wed 3Oct10 22:25:46 - (002118) Sent file d:\ftp\files\main\oct\camera1... (4 Replies)
Discussion started by: Jotne
4 Replies

5. Shell Programming and Scripting

Executing mutliple commands in Unix

Hi I always do a ftp in Unix to bring in a file from Mainframes. Then run a set of commands say 8 commands one by one to execute the brought in file. Upon completion of this commands, the data from the file gets transfered to Mainframe database. The problem is i can execute only 1 file at a time.... (2 Replies)
Discussion started by: cnelatur
2 Replies

6. Shell Programming and Scripting

Problem with mutliple sed

Hi I'm working on a file which looks like: 00001| |XXX|ABCD|27-SEP-1983|31-DEC-2099|Name|00001|N| |31|L| | | | |25-AUG-1990 I need to change the date format from DD-MMM-YYYY to MM/DD/YYYY So i using cat <filename> | sed... (2 Replies)
Discussion started by: Alim
2 Replies

7. Shell Programming and Scripting

Need help on Mulitple files mutliple actions

Hi all, I have mistkanely gzipped twice an entire folder and sub folders, and also renamed the files during that process. I am trying to undo this, and I need help to create the batch to work on it. All folders are under my images directory, I have a output.txt file that holds all the... (1 Reply)
Discussion started by: saariko
1 Replies

8. UNIX for Dummies Questions & Answers

process mutliple files in the same directory

I have over 900 files that have the same name except for a unique numeric assignment. For all files I would like to cut the 2nd column and paste all into one new file. All in bash. sample input format for each file: 1 2 3 1 2 3 1 2 3 sample command for what I want to do: cut -d' ' -f2... (1 Reply)
Discussion started by: epi8
1 Replies

9. UNIX for Dummies Questions & Answers

Please Help:Need to Split the file into mutliple files depends on the KEY field value

Hi Gurus, I am new to UNIX(HP). my requirmnet is File needs to needs to be split into multiple files dependa on one key value. example 1 abc 333 us 2 bbc 444 ch 5 nnn 333 kk 7 hhh 555 ll 3 hhh 333 hh now the requirment is line with 333 should be copied into test1.txt and... (14 Replies)
Discussion started by: arund_01
14 Replies

10. Shell Programming and Scripting

Help Needed - print mutliple lines

I have the following input -- appl = host = user = / pid = 76 elapsed = 0.000 seconds server_elapsed = 2.999 select emp_no, dept_no from emp where empname like 'Rob%' and empno=10 ; -- appl = host = user = / pid = 76 elapsed = 0.000 seconds server_elapsed = 0.999 select emp_no, dept_no... (5 Replies)
Discussion started by: newlearner
5 Replies
Login or Register to Ask a Question