Hot answers tagged

16 votes

How to join a line with a pattern with the next line with sed?

What you want to do is to join the module lines with the next line. Using sed: $ sed '/^module/N;s/\n//' file module x(a,b,c) module y(d,e,f, g,h,i) module z(j,k,l) This is with your data copied ...
Kusalananda's user avatar
  • 327k
12 votes
Accepted

Merge multiple files with join

Basically like this for your 3 files example $ join file2 file3| join file1 - 1 test1 example1 foo1 2 test3 example2 foo2 3 test4 example3 foo3 4 test5 example4 foo4 But important all your input ...
rudimeier's user avatar
  • 10.1k
10 votes
Accepted

Join two files each with two columns, including non-matching lines

You want -o auto: join -t, -j 1 -a 1 -a 2 -o auto john jane From man join: -o FORMAT obey FORMAT while constructing output line      ︙ If FORMAT is the keyword 'auto', then the first line of each ...
kos's user avatar
  • 2,857
10 votes

Join two files with different fields number

That's what join does: join -2 2 -a 1 pbas.txt s2.txt The options say: -2 2: the second file uses the second column to store the key -a 1: output all lines from file 1, even if there's no match in ...
choroba's user avatar
  • 46.4k
9 votes
Accepted

How to do anti-join or inverse join in bash

I wouldn't use join for this because join requires input to be sorted, which is an unnecessary complication for such a simple job. You could instead use grep: $ grep -vxFf list2 list1 a b Or awk: $ ...
terdon's user avatar
  • 238k
8 votes
Accepted

Unix join command complexity

The BSD join implementation is quite simple to follow and seems to be linear with regards to the number of lines in the files. This has gone mostly unchanged in all BSD systems since at least BSD 4.4 ...
Kusalananda's user avatar
  • 327k
7 votes

Join two files based on a column

Using join: $ join -t, -a 1 -a 2 -o0,1.2,2.2 -e ' -' file1 file2 a, 1, 2 b, 5, - c, 2, - f, 7, 9 g, -, 3 The standard join utility will perform a relational JOIN operation on the two sorted input ...
Kusalananda's user avatar
  • 327k
7 votes
Accepted

Joining multiple column from different file using awk

$ cat tst.awk BEGIN { FS=OFS="\t" } { datasets[$1]; fnames[FILENAME]; vals[$1,FILENAME] = $2 } END { printf "%s", "dataset" for (fname in fnames) { printf "%s%s", OFS, fname } ...
Ed Morton's user avatar
  • 30k
7 votes

Print Arrays Without Duplicates in AWK

Use join as you are looking to keep it as simple as possible; join -1 2 -2 1 -o 2.2 1.1 2.1 <(sort -unk2,2 file1) <(sort -unk1,1 file2) 2>/dev/null join on second field of first file -1 2 ...
αғsнιη's user avatar
  • 41.1k
7 votes
Accepted

Print Arrays Without Duplicates in AWK

The reason the partial line is printed, is that in your code you are not deleting the values you want to remove from the array, but replacing their values with empty strings. This causes the check $1 ...
user000001's user avatar
  • 3,528
6 votes

join : "File 2 not in sorted order"

Note that if you see this error, and you have already sorted on a specific column and are beating your head against the wall e.g. sort -k4,4 then you may also need to set the separator for the sort ...
Colin D's user avatar
  • 430
6 votes
Accepted

Join seven files with awk line-by-line

As steeldriver said, the reasonable way to do this is with paste: $ paste -d';' file* 1.001;2.001;3.001;4.001;5.001;6.001;7.001;8.001 1.002;2.002;3.002;4.002;5.002;6.002;7.002;8.002 1.003;2.003;3.003;...
John1024's user avatar
  • 74k
6 votes

join replaces tabs by spaces

The character used by join with -t as delimiter will be used for both input and output. The issue is the post-processing step, column -t, that you use. It will replace tabs with the appropriate ...
Kusalananda's user avatar
  • 327k
6 votes
Accepted

The "join" utility reports: file is not sorted, but in fact it is sorted

They’re sorted numerically, but join requires them to be sorted lexicographically: 24968, then 479; and 24965, 24968, 88, then 96.
Stephen Kitt's user avatar
5 votes

join : "File 2 not in sorted order"

LOCALE=C sort ... LOCALE=C join ... This will solve your problem. The issue, as pointed out by @Michael, is collation sequence, which depends on your LOCALE setting.
Jignesh Smart's user avatar
5 votes

I would like join two csv files

Uh, self answering question join -t ';' file1 file2 19/02/2016 22:00;16.70;39.50 19/02/2016 22:30;16.80;41.00 19/02/2016 23:00;16.80;40.30 19/02/2016 23:30;16.70;40.40
glenn jackman's user avatar
5 votes

Join two textfiles on 1st column keeping order and unpairable lines from 1st file

The easy way is via awk - just read the 2nd file, save each line into an array (where the index is $1) and when reading 1st file check if the line is already an index in array - if so replace with the ...
don_crissti's user avatar
  • 80.9k
5 votes
Accepted

Join two unsorted files with POSIX?

You can do it with two named pipes (or of course you could use one named pipe and stdin): mkfifo a b sort file1 > a & sort file2 > b & join a b Process substitution works essentially ...
derobert's user avatar
  • 108k
5 votes
Accepted

Numeric sort fails to properly sort file

If you want to use a file with join, you need to make sure it’s sorted lexicographically on the join key, which means you shouldn’t use -n with sort. You can always sort the result of the join ...
Stephen Kitt's user avatar
5 votes

Bash: join data from two csv files

Using awk, first read the usernames of users whose account are locked from the second file, then extract the email addresses of these from the first file (then hope that they don't need to log in to ...
Kusalananda's user avatar
  • 327k
5 votes
Accepted

Merge two files using first column

if the number of fields in both files are the same then you can use -o auto to fill-up the number of fields in each line based on the first line of each file (by default it fills the missing fields ...
αғsнιη's user avatar
  • 41.1k
4 votes

how to run a command in many files by running the command only one time?

If your shell supports brace expansion you can do: sed 's/.\{5\}/& /g' chr{1..31}.txt >output.txt chr{1..31}.txt will be expanded to chr1.txt, chr2.txt .. chr31.txt. The whole output (from ...
heemayl's user avatar
  • 55.4k
4 votes
Accepted

ZSH, concatenate passed in arguments into a single string

ZSH is delightfully free of the word-splitting behaviour seen in other shells (unless for some bizarre reason the SH_WORD_SPLIT option has been turned on), so there is no need to use strange double-...
thrig's user avatar
  • 34.6k
4 votes
Accepted

Removing a field from a comma delimited text with accented chars

Since I'm not sure you want a perl code so much, here is a similar awk code: awk -F';' -v OFS=';' '{ $NF=""; print }' data.csv => This code empties the last field of each line ($NF=""). Input fields ...
xhienne's user avatar
  • 17.5k
4 votes
Accepted

join returns nothing

That's simply not how the joincommand works - it joins lines based on a common (matching field) - which your input files don't have. You could do something like this using paste and awk: paste a01 ...
steeldriver's user avatar
  • 79.6k
4 votes
Accepted

What does `join` do in terms of equijoin in SQL or operations in relational algebra?

The join utility, by default, does what's called an "inner join" in SQL, resulting in the combined records of those entries whose join field is identical in both files. Yes, this is an "equi-join" ...
Kusalananda's user avatar
  • 327k
4 votes
Accepted

Why do I have "join: extra operand '/dev/fd/62'" error?

"$outer" is a quoted scalar variable so it always expands to one argument. If empty or unset, that still expands to one empty argument to join (and when you call your script with -o2, that's one -a 2 ...
Stéphane Chazelas's user avatar
4 votes

How to join a line with a pattern with the next line with sed?

Using awk: ~ awk '/^module/ {l = $0; getline; printf "%s", l} 1' input-file module x(a,b,c) module y(d,e,f, g,h,i) module z(j,k,l) For each line that starts with module, save the line in l, move ...
muru's user avatar
  • 71.2k
4 votes
Accepted

how can I merge two text files together

If file1 is your first text file, and file2 is your second text file, this should work in Bash: join -a 1 <(sort -k1,1 file2) <(sed -E -e 's/([^\s])\(/\1 (/' file1 | sort -k1,1) Join is called ...
ozzy's user avatar
  • 835
4 votes
Accepted

join replaces tabs by spaces

It is because column -t, with the table mode automatically determines the column width and creates a readable table output and delimits output with spaces and not tabs. To do this explicitly using ...
Inian's user avatar
  • 12.6k

Only top scored, non community-wiki answers of a minimum length are eligible