Recursive Cat?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recursive Cat?
# 1  
Old 08-31-2012
Recursive Cat?

I have a directory, say "DirA" with a bunch of subfolders 'Sub1, Sub2... etc.' Each subfolder has a number of csv files.

I want to delete the top ten rows of each csv file, then concatenate them and save the output as a csv file with the same name as the subfolder.

My code currently looks like this, but I really don't know what I'm doing at all.
Code:
for dir in ./; do 
	cat **/*.csv > $dir.csv
done

Thanks,
extnic
# 2  
Old 08-31-2012
What do you mean by 'delete the top ten rows'? Do you mean physically remove the rows from the file, or just not put them in your output?

* matches anything, two *'s in a row is redundant.

If there's few enough files, it could be as simple as

Code:
tail -n +11 folder/*/*.csv > output

# 3  
Old 08-31-2012
Code:
cd DirA
find . -type d > /tmp/mydirs.txt
while read dir
do
     cd $dir
     ls *.csv | while read csv
     do
           awk 'FNR>10' "$csv" >> big.csv
     done
     mv big.csv  ${dir}.csv
     cd ..
done < /tmp/mydirs.txt

This just creates the csv files without whacking off the top 10 lines of the csv's.
The final large csv for the directory is in the directory.
# 4  
Old 08-31-2012
Quote:
Originally Posted by Corona688
What do you mean by 'delete the top ten rows'? Do you mean physically remove the rows from the file, or just not put them in your output?

* matches anything, two *'s in a row is redundant.

If there's few enough files, it could be as simple as

Code:
tail -n +11 folder/*/*.csv > output

Thanks! But the output file looks like it is just listing the names of the csv files. I have 150 subdirectories, so I would love to be able to automate it instead of doing this for each of the subdirectories.
# 5  
Old 08-31-2012
If it prints no lines, then the files don't have more than 10 lines. You can give it -q to tell it to omit the file names.

Automate what? It's already selecting everything under folder/whatever/whatever.csv, what other folders do you want? You seemed to imply they were all at the same depth, folder/folder2/file.csv. If they're not, you need to use find and use its output to feed the tail command. xargs can turn a list of files on stdout into a list of arguments, as long as the filenames and folder names contain no spaces.

Code:
find /path/to/folderholdinghundredsoffolders/ -name '*.csv' | xargs tail -q -n +11 > output


Last edited by Corona688; 08-31-2012 at 02:41 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. OS X (Apple)

Search recursive

before posting, I have tried to find my answer elsewhere. no luck. I need to find a file buried in a folder somewhere. Master folder has 10 sub folders. each sub folder has folders too. I found this but it does nothing I am on Mac and use Applescript. do shell script "find... (2 Replies)
Discussion started by: sbrady
2 Replies

2. UNIX for Dummies Questions & Answers

Help with wget recursive

I want to download every page on a particular website that has: "http://www.website.com/dir/?function=" as the beginning so any page that begins with that will be downloaded, so for example "http://www.website.com/dir/?function=summary&cat=1" and "http://www.website.com/dir/?function=detail&id=1"... (1 Reply)
Discussion started by: guitarscn
1 Replies

3. UNIX for Dummies Questions & Answers

recursive wc on a directory?

Hi all, I need to count the number of lines in all the files under a directory (several levels deep). I am feeling extremely dumb, but I don't know how to do that. Needless to say, I am not a shell script wiz... Any advice? thanks in advance! (13 Replies)
Discussion started by: bimba17
13 Replies

4. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

5. Shell Programming and Scripting

for i in `cat myname.txt` && for y in `cat yourname.txt`

cat myname.txt John Doe I John Doe II John Doe III ----------------------------------------------------------------------- for i in `cat myname.txt` do echo This is my name: $i >> thi.is.my.name.txt done ----------------------------------------------------------------------- cat... (1 Reply)
Discussion started by: danimad
1 Replies

6. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

7. UNIX for Advanced & Expert Users

recursive nice value

Hi all, I have a running process that will spawn a large number of perl processes. How can I set that these all get spawned with a low priority nice value? I don't mind if all perl related processes take this level. Note, the executing script is compiled and can not be altered at a code... (1 Reply)
Discussion started by: nhatch
1 Replies

8. Shell Programming and Scripting

recursive rcp

I wrote a shell script (AIX) to extract the file "/rep1/toto" from all the hosts referred in a list and send them to one local directory named ~/$host-$file with the hostname as prefix rcp -p user@host:/rep1/$file ~/$host-$file where file = toto ==> it works ! I would do the same thing... (6 Replies)
Discussion started by: Nicol
6 Replies

9. UNIX for Dummies Questions & Answers

recursive GREP ?

Hi! Suppose I have a directory (no symbolic links) called /WORK that contains 3 subdirectories: /A /B /C My problem is this: I want to look for a file that contains an order number. So far, I obtain what I want by doing this /home/acb% cd /WORK/A /home/acb/WORK/A% grep '093023553' *.*... (3 Replies)
Discussion started by: alan
3 Replies
Login or Register to Ask a Question