Combining lines of files to new file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Combining lines of files to new file
# 1  
Old 03-04-2010
Combining lines of files to new file

Hi everybody.
I have a number of files that i would like to combine. however not concatenating, but rather extract lines from the files.
Example:

File1 ------ File2 ------File3 ...
line11 ---- line21 ---- line31 ...
line12 ---- line22 ---- line32 ...
line13 ---- line23 ---- line33 ...
... ...

and i want to have something like
File1 ----- File2 ------ File3
line11 ---- line12 ---- line13
line21 ---- line22 ---- line23
line31 ---- line32 ---- line33

That means a file containing all the first lines, one for the 2nd lines, one for the 3rd, and so on. Is there an easy way to do this?
Appreciate any help.
# 2  
Old 03-04-2010
This will create lines in files named file0 .., 1 2 3 4 5 6 7 8 9, since your example put line 3 13 23 and 33 in file3, I assume you only want lines%10 output filenames
Code:
awk { fname=sprintf("file%d", FNR%10); print $0 >> fname}[list of input files goes here]

# 3  
Old 03-04-2010
Sorry, i think my explanation was unclear. By line13 i meant line3 from file 1

mybe this is clearer:
FileA ------ FileB ------FileC ...
lineA1 ---- lineB1 ---- lineC1 ...
lineA2 ---- lineB2 ---- lineC2 ...
lineA3 ---- lineB3 ---- lineC3 ...
... ...

and i want to have something like
File1 ----- File2 ------ File3
lineA1 ---- lineA2 ---- lineA3
lineB1 ---- lineB2 ---- lineB3
lineC1 ---- lineC2 ---- lineC3

Do I have to replace the division by 10 in the awk command?
# 4  
Old 03-05-2010
Try this with your input file.

Code:
 
#!/bin/sh
if [ $# -lt 3 ]
then
        echo "Please Give positional parameters as filenames"
        echo "Usage : sh <script> <file1> <file2> <file3>"
        exit
else
no_of_lines=` wc -l $1 | cut -d " " -f 1 `
j=1
for((i=1; i<=no_of_lines; i++))
do
        `sed -n "${i}p" $1 > file${j} ; sed -n "${i}p" $2 >> file${j}; sed -n "${i}p" $3  >> file${j} `
        j=`expr $j + 1`
done
fi

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Combining certain columns of multiple files into one file

Hello Unix gurus, I have a large number of files (say X) each containing two columns of data and the same number of rows. I would like to combine these files to create a unique merged file containing X columns corresponding to the second column of each file (with a bonus of having the first... (3 Replies)
Discussion started by: ksennin
3 Replies

2. Shell Programming and Scripting

Combining files(every 15 min) as one file(hourly)

Hello, My system is generating two files every 15 minutes and file names are given automatically as below. (98,99,89,90 are the sequence numbers) File1_09242013131016_000000098 File1_09242013131516_000000099 File2_09242013124212_000000089 File2_09242013124712_000000090 I want to combine... (6 Replies)
Discussion started by: phoenex11
6 Replies

3. UNIX for Dummies Questions & Answers

Need help combining txt files w/ multiple lines into csv single cell - also need data merge

:confused:Hello -- i just joined the forums. I am a complete noob -- only about 1 week into learning how to program anything... and starting with linux. I am working in Linux terminal. I have a folder with a bunch of txt files. Each file has several lines of html code. I want to combine... (2 Replies)
Discussion started by: jetsetter
2 Replies

4. Shell Programming and Scripting

Combining multiple column files into one with file name as first row

Hello All, I have several column files like this $cat a_b_s1.xls 1wert 2tg 3asd 4asdf 5asdf $cat c_d_s2.xls 1wert 2tg 3asd 4asdf 5asdf desired put put $cat combined.txt s1 s2 (2 Replies)
Discussion started by: avatar_007
2 Replies

5. Shell Programming and Scripting

Combining 2 lines in a file into 1 line

Hi all, I have a file with lot of lines with repeating pattern. ( TABLE_NAME line followed by Total line). I would like combine these two lines into one line seperated by cama and create a new file. Is there a simple way to do this. Current Format ( just a sample 4 lines ) TABLE_NAME:... (10 Replies)
Discussion started by: MKNENI
10 Replies

6. Shell Programming and Scripting

Combining columns from multiple files to one file

I'm trying to combine colums from multiple file to a single file but having some issues, appreciate your help. The filenames are the same except for the extension, path1.m0 --------- a b c d e f g h i path1.m1 --------- m n o p q r s t u File names are path1.m The... (3 Replies)
Discussion started by: rkmca
3 Replies

7. Shell Programming and Scripting

searching thru or combining multiple lines in a unix file

This is the problem actually: This regex: egrep "low debug.*\".*\"" $dbDir/alarmNotification.log is looking for data between the two quotation marks: ".*\" When I hate data like this: low debug 2009/3/9 8:30:20.47 ICSNotificationAlarm Prodics01ics0003 IC... (0 Replies)
Discussion started by: ndedhia1
0 Replies

8. Shell Programming and Scripting

combining lines in files

hey, I want to combine every three lines in a file onto one i.e old file: 82 67 32 62 58 39 29 47 58 27 34 50 27 35 69 38 58 70 new file: 82 67 32 62 58 39 29 47 58 27 34 50 27 25 69 38 58 70 At the moment I am using the following code: gawk 'BEGIN {x=0} { if(x<3)... (14 Replies)
Discussion started by: sme
14 Replies

9. Shell Programming and Scripting

combining 2 files with more than one match in second file

Hello, I am attempting to combine two files where the second file can have more than one match with the lookup field (node) in the first file, onto one line of the output file. Also alerting if a lookup was not found in file2 =-=-=-=-=-=-= Example of file1 node,type =-=-=-=-=-=-= bob,232... (5 Replies)
Discussion started by: johnes42
5 Replies

10. Shell Programming and Scripting

need help appending lines/combining lines within a file...

Is there a way to combine two lines onto a single line...append the following line onto the previous line? I have the following file that contains some blank lines and some lines I would like to append to the previous line... current file: checking dsk c19t2d6 checking dsk c19t2d7 ... (2 Replies)
Discussion started by: mr_manny
2 Replies
Login or Register to Ask a Question