简体   繁体   中英

merging two .txt files together with data columns side by side

I am trying to merge two text files together with the data stored in separate columns in an output file merged_file.txt .

File_1:

 Temp[K] Time(s) dT
 314.1 1.2 0.4
 317.4 4.2 0.3
 319.9 7.2 0.5

File_2:

 Temp[K] Time(s) dT
 312.1 1.2 0.2
 315.3 4.2 0.3
 316.7 7.2 0.2

Merged_file:

Temp[K] Time(s) dT Temp[K] Time(s) dT
314.1 1.2 0.4 312.1 1.2 0.2

I'm relatively new to Unix and the bash shell, but I've done some research and found paste and pr . For paste I tried to use the delimiter ' ' but it does not work.

$ paste -d' ' File_1.txt File_2.txt | column -s $'\t' -t > merged_file.txt 
$ pr -m -t File_1.txt File_2.txt  > merged_file.txt

The data is always stored beneath each other and it wouldn't work under any circumstances.

You can use awk and the built-in variable FILENAME to read each of the files into an array and then output them side-by-side using the END block. You can use a simple check if ($0 == "") to check for and discard empty lines.

For example where your data is in files f1 and f2 you can do:

awk '
    FILENAME=="f1"{if ($0 != "") a[i++] = $0} 
    FILENAME=="f2"{if ($0 != "") b[j++] = $0} 
    END {for (k = 0; k < i; k++) print a[k], b[k]}' f1 f2

Explanation

  • FILENAME=="f1"{if ($0 != "") a[i++] = $0} if the file is f1 , and the line isn't empty, read it into array a[] ;
  • ditto for file f2 but read into the b[] array;
  • END {for (k = 0; k < i; k++) print a[k], b[k]} after all lines in both files are processed, loop i times printing the contents of the a[] and b[] arrays separated by a space, (you can add an additional check to ensure i == j )

Example Use/Output

$ awk '
>     FILENAME=="f1"{if ($0 != "") a[i++] = $0}
>     FILENAME=="f2"{if ($0 != "") b[j++] = $0}
>     END {for (k = 0; k < i; k++) print a[k], b[k]}' f1 f2
Temp[K] Time(s) dT Temp[K] Time(s) dT
314.1 1.2 0.4 312.1 1.2 0.2
317.4 4.2 0.3 315.3 4.2 0.3
319.9 7.2 0.5 316.7 7.2 0.2

You use column -s $'\\t' . The -s option of column specifies the delimiter used in the input file. When you have spaces in your input files and use paste -d' ' leave it as default.

$ paste -d' ' File_1 File_2 | column -t
Temp[K]  Time(s)  dT   Temp[K]  Time(s)  dT
314.1    1.2      0.4  312.1    1.2      0.2
317.4    4.2      0.3  315.3    4.2      0.3
319.9    7.2      0.5  316.7    7.2      0.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM