Paste multiple files, but only the sorted head -50


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Paste multiple files, but only the sorted head -50
# 1  
Old 04-25-2013
Paste multiple files, but only the sorted head -50

Hello,
I want to merge multiple files (under hundreds folders) side by side. File name are the same but folder are different.
like
Code:
 
folder1/same_name.txt
folder2/same_name.txt
folder3/same_name.txt
......

Normally it can be done as
Code:
paste /different_path*/same_name.txt > merged_file.txt

but my challenge is I want to only merge the first 50 lines of each file as
Code:
head -50 folder1/same_name.txt | sort 
head -50 folder2/same_name.txt | sort 
...
head -50 folder299/same_name.txt | sort

to create intermediate files, then merge the intermediate files to have the result. I'd like to combine looping thru the folders with paste such as:
Code:
paste `head file*.txt | sort`
or
paste $(head file*.txt | sort)

Of course this did not work. My question is how to pipe the multiple "head | sort" with paste to have single line command.
Thanks a lot!
# 2  
Old 04-25-2013
Quote:
Originally Posted by yifangt
Normally it can be done as
Code:
paste /different_path*/same_name.txt > merged_file.txt

If your system "normally" handles the command line length generated by that pathname expansion, then there's no reason you can't use it for this task.
Code:
paste /different_path*/same_name.txt | head -n50 | sort > merged_file.txt

Regards,
Alister
# 3  
Old 04-25-2013
Thanks alister!
What I meant is to head -50 | sort each file first then do paste to merge them as
Code:
paste (head -50 | sort /different_path*/same.txt) > merged_file.txt.

But the bracket part can not be piped like that.

Last edited by yifangt; 04-25-2013 at 05:04 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to copy particular files from a multiple directories and paste in a new directory?

Dear all I have a multiple directories, say for example org1, org2, org3 ..... org100 and each directory having a file namely dnaG.fasta. I need to copy all the dnaG.fasta file from each directory and paste in another directory fastconcatg. Therefore, my script has to copy dnaG.fasta file from... (5 Replies)
Discussion started by: dineshkumarsrk
5 Replies

2. UNIX for Beginners Questions & Answers

How to copy a column of multiple files and paste into new excel file (next to column)?

I have data of an excel files as given below, file1 org1_1 1 1 2.5 100 org1_2 1 2 5.5 98 org1_3 1 3 7.2 88 file2 org2_1 1 1 2.5 100 org2_2 1 2 5.5 56 org2_3 1 3 7.2 70 I have multiple excel files as above shown. I have to copy column 1, column 4 and paste into a new excel file as... (26 Replies)
Discussion started by: dineshkumarsrk
26 Replies

3. UNIX for Beginners Questions & Answers

How to paste multiple files in parallel?

Hi all, I am trying to paste thousands of files together into a matrix. Each file has only 1 column and all the files have the same number of rows (~27k rows). I tried paste * > output as well as some other for loops but the output only contains the columns from the 1st and last files. The... (8 Replies)
Discussion started by: notimenocall
8 Replies

4. Shell Programming and Scripting

Paste columns based on common column: multiple files

Hi all, I've multiple files. In this case 5. Space separated columns. Each file has 12 columns. Each file has 300-400K lines. I want to get the output such that if a value in column 2 is present in all the files then get all the columns of that value and print it side by side. Desired output... (15 Replies)
Discussion started by: genome
15 Replies

5. Shell Programming and Scripting

sort the files based on timestamp and execute sorted files in order

Hi I have a requirement like below I need to sort the files based on the timestamp in the file name and run them in sorted order and then archive all the files which are one day old to temp directory My files looks like this PGABOLTXML1D_201108121235.xml... (1 Reply)
Discussion started by: saidutta123
1 Replies

6. Shell Programming and Scripting

combine multiple files by column into one files already sorted!

I have multiple files; each file contains a certain data in a column view simply i want to combine all those files into one file in columns example file1: a b c d file 2: 1 2 3 4 file 3: G (4 Replies)
Discussion started by: ahmedamro
4 Replies

7. UNIX for Advanced & Expert Users

makefile head-scratcher: multiple targets in one go

Hi! I've got a build process where scripts create multiple targets from their sources. But here I'm running into a conceptual problem of GNU make: If one has multiple targets in a dependency, make applies the rules once for every target that is out of sync - which is correct for normal... (3 Replies)
Discussion started by: treczoks
3 Replies

8. UNIX for Dummies Questions & Answers

write a program in c in unix that display the files(includ sub-direc and files within) in a sorted

the sorting is based on name of file, file size modification time stamps o f file it should dislay the output in the following format "." and ".." enteries should be ignored please give some idea how to do it (1 Reply)
Discussion started by: pappu kumar jha
1 Replies

9. Shell Programming and Scripting

head / tail combination returns multiple rows

Hi, As part of our project, we need to load historical data for a year before our system is live. We have the data feed files that we need to load. However, I need to make sure that the file structure (number of fields separated by a comma) on the field is same for all the files of the same... (1 Reply)
Discussion started by: raj.jha
1 Replies

10. Shell Programming and Scripting

Compare 2 sorted files

Hi all, please give me the commands using which i can compare 2 sorted files and get the difference in third file, indiating where the difference is from either file1 or file2. as: File1 (Original file) GARRY JOHN JULIE SAM --------------- File2 DEV GARRY JOHN JOHNIEE (7 Replies)
Discussion started by: varungupta
7 Replies
Login or Register to Ask a Question