Using paste command every nth number of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using paste command every nth number of file
# 1  
Old 10-20-2012
Using paste command every nth number of file

Hi,

I want to use paste command in a loop that does it every 6 files. My sample files are like the ones below.
Code:
20010101.txt 20010106.txt 20010111.txt 
20010116.txt 20010121.txt 20010126.txt
20010131.txt 20010205.txt 20010210.txt
20010215.txt 20010220.txt 20010225.txt
20010302.txt 20010307.txt 20010312.txt
20010317.txt 20010322.txt 20010327.txt
...20011227.txt
each file contains 1-column data which I need to paste together to make a 6-column file

paste 20010101.txt 20010106.txt 20010111.txt 20010116.txt 20010121.txt 20010126.txt > 200101.txt

paste 20010131.txt 20010205.txt 20010210.txt 20010215.txt 20010220.txt 20010225.txt > 200102.txt

and so on...

how can I automate the pasting process every 6 files? Thanks much for any help.
# 2  
Old 10-20-2012
Code:
ls *txt | xargs -n6 paste

These 2 Users Gave Thanks to msabhi For This Post:
# 3  
Old 10-20-2012
Thanks much, worked perfectly,Smilie

---------- Post updated at 05:44 AM ---------- Previous update was at 05:33 AM ----------

one more question, how can I redirect the output to unique file name? Thanks much.
# 4  
Old 10-20-2012
Try this...i don't think with xargs its achievable...
Code:
1>Write all the file names into a text file
ls *txt > temp
2>Execute this command to see what you really desired to see
perl -lne '{if($.%6==0){$x++;push(@array,$_);print "paste @array > 2001$x.txt";$#array=-1}else {push(@array,$_);}} END{$x++;print "paste @array >2001$x.txt";}' temp

3>Finally execute this command once you are ok with the output you have seen in 2nd step
perl -lne '{if($.%6==0){$x++;push(@array,$_);print "paste @array >  2001$x.txt";$#array=-1}else {push(@array,$_);}} END{$x++;print "paste  @array >2001$x.txt";}' temp | sh

Wonder if there was a simple solution Smilie
This User Gave Thanks to msabhi For This Post:
# 5  
Old 10-20-2012
Thanks much msabhi. I tried the xargs and used split to divide the lines to individual files.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Paste many files with different row number in one file

Hi everybody I am trying to merge many files in one files using paste or pr command but I am not able to resolve this issue. The file are tab delimited like these: 1 4721519 4723118 1 5022468 5024918 1 7093519 7098118 2 19736573 19741172 2 21907973 21910572... (4 Replies)
Discussion started by: giuliangiuseppe
4 Replies

2. Shell Programming and Scripting

Use of the PASTE command in a script file

Hi, When I use the paste command in the terminal window it works fine, but when i try to use it in a bash script file i get errors. I'm not sure how to use the paste command in a bash script file. my paste command looks like this paste <( code1 ) <(code2) thanks (7 Replies)
Discussion started by: eboye
7 Replies

3. Shell Programming and Scripting

Calculating average for every Nth line in the Nth column

Is there an awk script that can easily perform the following operation? I have a data file that is in the format of 1944-12,5.6 1945-01,9.8 1945-02,6.7 1945-03,9.3 1945-04,5.9 1945-05,0.7 1945-06,0.0 1945-07,0.0 1945-08,0.0 1945-09,0.0 1945-10,0.2 1945-11,10.5 1945-12,22.3... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

4. Shell Programming and Scripting

Using AWK to find top Nth values in Nth column

I have an awk script to find the maximum value of the 2nd column of a 2 column datafile, but I need to find the top 5 maximum values of the 2nd column. Here is the script that works for the maximum value. awk 'BEGIN { subjectmax=$1 ; max=0} $2 >= max {subjectmax=$1 ; max=$2} END {print... (3 Replies)
Discussion started by: ncwxpanther
3 Replies

5. Shell Programming and Scripting

find string nth occurrence in file and print line number

Hi I have requirement to find nth occurrence in a file and capture data from with in lines (between lines) Data in File. <QUOTE> <SESSION> <ATTRIBUTE NAME='Parameter Filename' VALUE='file1.parm'/> <ATTRIBUTE NAME='Service Name' VALUE='None'/> </SESSION> <SESSION> <ATTRIBUTE... (6 Replies)
Discussion started by: tmalik79
6 Replies

6. UNIX for Dummies Questions & Answers

Getting number of lines of nth occurrency

Hi all, I would like to extract the line number of the n-th occurrency of a given string in a file. e.g. xxx yyy xxx zzz xxx the second occurrency of xxx is at line 3. What is the fastest way to do it in bash? Thank you, (8 Replies)
Discussion started by: f_o_555
8 Replies

7. UNIX for Dummies Questions & Answers

paste command

input1 15 150 input2 x 10 100 input3 y 20 200 z 34 44 cmd paste -d "\t" input1 input2 input3 >>output output (1 Reply)
Discussion started by: repinementer
1 Replies

8. Shell Programming and Scripting

read file column and paste it in command

Hi Unix gurus I have a file containing 2 coloumns. I would like to do a script which reads the lines and executes a command like this: command <field1> parameters <field2> some more parameters Please let me know how you would do this without AWK, SED or any other mini language (for special... (5 Replies)
Discussion started by: BearCheese
5 Replies

9. UNIX for Dummies Questions & Answers

Grepping nth line number

How do you grep every nth line number from a file? (2 Replies)
Discussion started by: shabs1985
2 Replies

10. UNIX for Advanced & Expert Users

paste command

I wonder if any body can help me with a command i am struggling with. I have a file with around 400 lines in, in a program i have it pulls out each line at a time so that data from the line can be cross referenced with another file. If it finds a match it pulls out a ocde from the second file, this... (5 Replies)
Discussion started by: mariner
5 Replies
Login or Register to Ask a Question