In Linux How to Join 2 Files

join multiple files

man join:

NAME
join - join lines of two files on a common field

SYNOPSIS
join [OPTION]... FILE1 FILE2

it only works with two files.

if you need to join three, maybe you can first join the first two, then join the third.

try:

join file1 file2 | join - file3 > output

that should join the three files without creating an intermediate temp file. - tells the join command to read the first input stream from stdin

Join two files including unmatched lines in Shell

Could you please try following.

awk '
FNR==NR{
a[$1]=$2
next
}
($1 in a){
print $0,a[$1]
b[$1]
next
}
{
print $1,$2 " ----- "
}
END{
for(i in a){
if(!(i in b)){
print i" ----- "a[i]
}
}
}
' Input_file2 Input_file1

Output will be as follows.

207.46.13.90  37556 62343
157.55.39.51 34268 58451
40.77.167.109 21824 21824
157.55.39.253 19683 -----
157.55.39.200 ----- 37675

How to append contents of multiple files into one file

You need the cat (short for concatenate) command, with shell redirection (>) into your output file

cat 1.txt 2.txt 3.txt > 0.txt

How to combine two files into a third using only linux system calls?

You can use the copy_file_range system call for this. It is faster than using read and write calls as the copying is done inside the kernel. From the man page:

The copy_file_range() system call performs an in-kernel copy between two file descriptors without the additional cost of transferring data from the kernel to user space and then back into the kernel.

Here is an example of using it:

#define _GNU_SOURCE
#include <fcntl.h>
#include <limits.h>
#include <stdint.h>
#include <unistd.h>

int do_copy(int infd, int outfd)
{
ssize_t bytes = 0;
do
{
bytes = copy_file_range(infd, NULL, outfd, NULL, SSIZE_MAX, 0);
}
while(SSIZE_MAX == bytes);

return bytes;
}

int concatenate(const char *inpath1, const char *inpath2, const char *outpath)
{
int infd1 = -1;
int infd2 = -1;
int outfd = -1;
int res = -1;

infd1 = open(inpath1, O_RDONLY);
if(infd1 < 0)
goto close;

infd2 = open(inpath2, O_RDONLY);
if(infd2 < 0)
goto close;

outfd = open(outpath, O_WRONLY|O_CREAT|O_TRUNC);
if(outfd < 0)
goto close;

res = do_copy(infd1, outfd);
if(res < 0)
goto close;

res = do_copy(infd2, outfd);

close:
if(infd1 >= 0)
close(infd1);

if(infd2 >= 0)
close(infd2);

if(outfd >= 0)
close(outfd);

return res;
}

The loop in do_copy allows for very large files which may exceed the maximum copy possible in a single call.

How to merge two files consistently line by line

You can use paste to format the files side by side:

$ paste -d" " file1.txt file2.txt
/etc/port1-192.9.200.1-255.555.255.0 /etc/port1-192.90.2.1-255.555.0.0
/etc/port2-192.9.200.1-255.555.255.0 /etc/port2-192.90.2.1-255.555.0.0
/etc/port3-192.9.200.1-255.555.255.0 /etc/port3-192.90.2.1-255.555.0.0
/etc/port4-192.9.200.1-255.555.255.0 /etc/port4-192.90.2.1-255.555.0.0
/etc/port5-192.9.200.1-255.555.255.0 /etc/port5-192.90.2.1-255.555.0.0

E.g.:

$ paste -d" " file1.txt file2.txt | while read from to; do echo mv "${from}" "${to}"; done
mv /etc/port1-192.9.200.1-255.555.255.0 /etc/port1-192.90.2.1-255.555.0.0
mv /etc/port2-192.9.200.1-255.555.255.0 /etc/port2-192.90.2.1-255.555.0.0
mv /etc/port3-192.9.200.1-255.555.255.0 /etc/port3-192.90.2.1-255.555.0.0
mv /etc/port4-192.9.200.1-255.555.255.0 /etc/port4-192.90.2.1-255.555.0.0
mv /etc/port5-192.9.200.1-255.555.255.0 /etc/port5-192.90.2.1-255.555.0.0

Of course you would want to throw in some safety checks ([ -f "${from}" ], ...).

Disclaimer: Works only if there are no spaces in your filenames.

Joining multiple fields in text files on Unix

you can try this

awk '{
o1=$1;o2=$2;o3=$3
$1=$2=$3="";gsub(" +","")
_[o1 FS o2 FS o3]=_[o1 FS o2 FS o3] FS $0
}
END{ for(i in _) print i,_[i] }' file1 file2

output

$ ./shell.sh
foo 1 scaf 3 4.5
bar 2 scaf 3.3 1.00
foo 1 boo 2.3

If you want to omit uncommon lines

awk 'FNR==NR{
s=""
for(i=4;i<=NF;i++){ s=s FS $i }
_[$1$2$3] = s
next
}
{
printf $1 FS $2 FS $3 FS
for(o=4;o<NF;o++){
printf $i" "
}
printf $NF FS _[$1$2$3]"\n"
} ' file2 file1

output

$ ./shell.sh
foo 1 scaf 3 4.5
bar 2 scaf 3.3 1.00

How to combine multiple files in linux with delimiter seperation?

you can use the following command for combining multiple files with --- delimiter.

awk 'FNR==1 && NR!=1 {print "---"}{print}' file1 file2 > newfile

command is copied from this post of Unix stack excahnge
https://unix.stackexchange.com/questions/163782/combine-two-text-files-with-adding-some-separator-between

Inner join on two text files

Should not the file2 contain LUA at the end?

If yes, you can still use join:

join -t'|' -12 <(sort -t'|' -k2 file1) file2


Related Topics



Leave a reply



Submit