Sponsored Content
Top Forums Shell Programming and Scripting [Solved] Script to concatenate 2 files with the same number of lines Post 302837425 by gacanepa on Thursday 25th of July 2013 06:20:55 PM
Old 07-25-2013
[Solved] Script to concatenate 2 files with the same number of lines

Hi everyone,
I have two files, namely:
file1:
Code:
file1Col1Row1;file1Col2Row1;file1Col3Row1
file1Col1Row2;file1Col2Row2;file1Col3Row2
file1Col1Row3;file1Col2Row3;file1Col3Row3

file2:
Code:
file2Col1Row1;file2Col2Row1;file2Col3Row1
file2Col1Row2;file2Col2Row2;file2Col3Row2
file2Col1Row3;file2Col2Row3;file2Col3Row3

I need to concatenate both files line by line so that the output will be:
Code:
file1Col1Row1;file1Col2Row1;file1Col3Row1;file2Col1Row1;file2Col2Row1;file2Col3Row1
file1Col1Row2;file1Col2Row2;file1Col3Row2;file2Col1Row2;file2Col2Row2;file2Col3Row2
file1Col1Row3;file1Col2Row3;file1Col3Row3;file2Col1Row3;file2Col2Row3;file2Col3Row3

The only way I can think of is using 2 nested while loops that read from each file and output to another file, but I don't know if there's a more efficient way?
Any ideas will be more than welcome. Thanks in advance.

---------- Post updated at 07:18 PM ---------- Previous update was at 07:02 PM ----------

I found the answer:
Code:
gacanepa@Gabriel-PC ~/stuff/scripts/bash $ cat file1
file1Col1Row1;file1Col2Row1;file1Col3Row1
file1Col1Row2;file1Col2Row2;file1Col3Row2
file1Col1Row3;file1Col2Row3;file1Col3Row3
gacanepa@Gabriel-PC ~/stuff/scripts/bash $ cat file2
file2Col1Row1;file2Col2Row1;file2Col3Row1
file2Col1Row2;file2Col2Row2;file2Col3Row2
file2Col1Row3;file2Col2Row3;file2Col3Row3
gacanepa@Gabriel-PC ~/stuff/scripts/bash $ paste -d";" file1 file2
file1Col1Row1;file1Col2Row1;file1Col3Row1;file2Col1Row1;file2Col2Row1;file2Col3Row1
file1Col1Row2;file1Col2Row2;file1Col3Row2;file2Col1Row2;file2Col2Row2;file2Col3Row2
file1Col1Row3;file1Col2Row3;file1Col3Row3;file2Col1Row3;file2Col2Row3;file2Col3Row3

---------- Post updated at 07:20 PM ---------- Previous update was at 07:18 PM ----------

I need the help of a moderator here to mark this thread as solved. I believe I can't do it since I found the answer before any one replied to the post.

Last edited by Don Cragun; 07-26-2013 at 12:12 AM.. Reason: Note in thread Title that issue has been solved.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to concatenate several files

I need a script to concatenate several files in one step, I have 3 header files say file.S, file.X and file.R, I need to concatenate these 3 header files to data files, say file1.S, file1.R, file1.X so that the header file "file.S" will be concatenated to all data files with .S extentions and so on... (3 Replies)
Discussion started by: docaia
3 Replies

2. Shell Programming and Scripting

Script to split files based on number of lines

I am getting a few gzip files into a folder by doing ftp to another server. Once I get them I move them to another location .But before that I need to make sure each gzip is not more than 5000 lines and split it up . The files I get are anywhere from 500 lines to 10000 lines in them and is in gzip... (4 Replies)
Discussion started by: gubbu
4 Replies

3. Shell Programming and Scripting

perl script on how to count the total number of lines of all the files under a directory

how to count the total number of lines of all the files under a directory using perl script.. I mean if I have 10 files under a directory then I want to count the total number of lines of all the 10 files contain. Please help me in writing a perl script on this. (5 Replies)
Discussion started by: adityam
5 Replies

4. Shell Programming and Scripting

[Solved] How to concatenate files in subdirectories?

I have many log files in various subdirectories of a parent directory. The directory structure e something like this: Parent directory: /home/linus/logfiles/ This parent directory has many subdirectories, like this: .../logfiles/jack .../logfiles/helen .../logfiles/sue... (13 Replies)
Discussion started by: zetetic
13 Replies

5. UNIX for Advanced & Expert Users

Concatenate lines in file shell script

Hi colleagues, I have a file in this format. "/cccc/pppp/dddd/ggg/prueba.txt". ERROR" THE error bbbbbbbbbb finish rows. "/kkkk/mmmm/hhhh/jjj/ejemplo.txt". ERROR This is other error rows.I need my file in this format. "/cccc/pppp/dddd/ggg/prueba.txt". ERROR" THE error bbbbbbbbbb finish rows.... (3 Replies)
Discussion started by: systemoper
3 Replies

6. Shell Programming and Scripting

Concatenate lines with unique string AND number

In Bash using AWK or sed I need to convert the following file: ... numitem_tab0 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p><p>5 KEYWORD</p>...<p>25 KEYWORD</p> subitem_tab0 =<p></p><p></p> ... numitem_tab6 =<p>1 KEYWORD</p><p>2 KEYWORD</p><p>3 KEYWORD</p><p>4 KEYWORD</p>... (2 Replies)
Discussion started by: pioavi
2 Replies

7. UNIX for Dummies Questions & Answers

Writing a script to print the number of lines in multiple files

Hi I have 1000 files labelled data1.txt through data1000.txt. I want to write a script that prints out the number of lines in each txt file and outputs it in the following format: Column 1: number of data file (1 through 1000) Column 2: number of lines in the text file Thanks! (2 Replies)
Discussion started by: evelibertine
2 Replies

8. Shell Programming and Scripting

[Solved] Counting The Number of Lines Between Values with Multiple Variables

Hey everyone, I have a bunch of lines with values in field 4 that I am interested in. If these values are between 1 and 3 I want it to count all these values to all be counted together and then have the computer print out LOW and the number of lines with those values in between 1 and 3,... (2 Replies)
Discussion started by: VagabondGold
2 Replies

9. Shell Programming and Scripting

Concatenate select lines from multiple files

I have about 6000 files of the following format (three simplified examples shown; actual files have variable numbers of columns, but the same number of lines). I would like to concatenate the ID (*Loc*) and data lines, but not the others, as shown below. The result would be one large file (or... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

10. UNIX for Beginners Questions & Answers

Print number of lines for files in directory, also print number of unique lines

I have a directory of files, I can show the number of lines in each file and order them from lowest to highest with: wc -l *|sort 15263 Image.txt 16401 reference.txt 40459 richtexteditor.txt How can I also print the number of unique lines in each file? 15263 1401 Image.txt 16401... (15 Replies)
Discussion started by: spacegoose
15 Replies
DIFF(1) 						      General Commands Manual							   DIFF(1)

NAME
diff - differential file comparator SYNOPSIS
diff [ -efbh ] file1 file2 DESCRIPTION
Diff tells what lines must be changed in two files to bring them into agreement. If file1 (file2) is `-', the standard input is used. If file1 (file2) is a directory, then a file in that directory whose file-name is the same as the file-name of file2 (file1) is used. The normal output contains lines of these forms: n1 a n3,n4 n1,n2 d n3 n1,n2 c n3,n4 These lines resemble ed commands to convert file1 into file2. The numbers after the letters pertain to file2. In fact, by exchanging `a' for `d' and reading backward one may ascertain equally how to convert file2 into file1. As in ed, identical pairs where n1 = n2 or n3 = n4 are abbreviated as a single number. Following each of these lines come all the lines that are affected in the first file flagged by `<', then all the lines that are affected in the second file flagged by `>'. The -b option causes trailing blanks (spaces and tabs) to be ignored and other strings of blanks to compare equal. The -e option produces a script of a, c and d commands for the editor ed, which will recreate file2 from file1. The -f option produces a similar script, not useful with ed, in the opposite order. In connection with -e, the following shell program may help maintain multiple versions of a file. Only an ancestral file ($1) and a chain of version-to-version ed scripts ($2,$3,...) made by diff need be on hand. A `latest version' appears on the standard output. (shift; cat $*; echo '1,$p') | ed - $1 Except in rare circumstances, diff finds a smallest sufficient set of file differences. Option -h does a fast, half-hearted job. It works only when changed stretches are short and well separated, but does work on files of unlimited length. Options -e and -f are unavailable with -h. FILES
/tmp/d????? /usr/lib/diffh for -h SEE ALSO
cmp(1), comm(1), ed(1) DIAGNOSTICS
Exit status is 0 for no differences, 1 for some, 2 for trouble. BUGS
Editing scripts produced under the -e or -f option are naive about creating lines consisting of a single `.'. DIFF(1)
All times are GMT -4. The time now is 05:34 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy