date
int64
1,220B
1,719B
question_description
stringlengths
28
29.9k
accepted_answer
stringlengths
12
26.4k
question_title
stringlengths
14
159
1,391,717,026,000
I have two files with approximately 12900 and 4400 entries respectively, that I want to join. The files contain location information for all landbased weather observing stations around the globe. The largest file is updated biweekly, and the smaller once a year or so. The original files can be found here (http://www.w...
awk 'BEGIN { while(getline < "file2" ) { codes[$1] = $2 } } { printf "%4s%s\n", codes[$1], substr($0, 5) }' file1
Joining two files with unique identifier
1,391,717,026,000
I have two files having a primary key value as a first field, and a corresponding value(s) as the remaining fields, and some primary key values are missing in one of them but are present in another, and vice versa: $ cat jointest1.txt jointest2.txt a 1 b 2 d 4 e 5 a 10 b 11 c 12 d 13 I'd expect a...
What implementation of join are you using? With join (GNU coreutils) 5.97, I can use [0 1021] ~/temp/jointest % join -a1 -a2 jointest1.txt jointest2.txt a 1 10 b 2 11 c 12 d 4 13 e 5 and the "plain" join works, too (but omits c and e). There is an -e option which supposedly lets you choose the marker for empty fiel...
Join, filling in missing key values
1,391,717,026,000
I want to write a shell script that get two files A and B, and get a result like this: File A: user_a tel_a addr_a user_b tel_b addr_b File B: process_1 user_a process_2 user_a process_3 user_b And the result: user_a process_1 tel_a addr_a user_a process_2 tel_a addr_a user_b process_3 tel_b addr_b How can i do thi...
join ... join -1 2 -2 1 FileB FileA Output user_a process_1 tel_a addr_a user_a process_2 tel_a addr_a user_b process_3 tel_b addr_b The input files need to be sorted by the key field ... Your example files are already sorted, so there was no need, but otherwise you could incorporate the sort as follows. join -1...
A shell script for joining two files
1,391,717,026,000
I'm trying to match and merge two sets of sorted data, one set per file. Each file contains two columns: the key field and the associated value. The resulting output should contain three columns: the key field, the value (if any) from the first file, and the value (if any) from the second file. I need to include 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 file determines the number of fields output for each line. Or better explained from GNU Coreutils: join invocation (foll...
Join two files each with two columns, including non-matching lines
1,391,717,026,000
How can I get two files A and B, and out put a result like this: File A: 001 Apple, CA 020 Banana, CN 023 Apple, LA 045 Orange, TT 101 Orange, OS 200 Kiwi, AA File B: 01-Dec-2013 01.664 001 AAA CAC 1083 01-Dec-2013 01.664 020 AAA CAC 0513 01-Dec-2013 01.668 023 AAA CAC 1091 01-Dec-2013 01.668 101 ...
A simple solution with awk: awk -v FILE_A="file-A" -v OFS="\t" 'BEGIN { while ( ( getline < FILE_A ) > 0 ) { VAL = $0 ; sub( /^[^ ]+ /, "", VAL ) ; DICT[ $1 ] = VAL } } { print $0, DICT[ $3 ] }' file-B Here is a commented version: awk -v FILE_A="file-A" -v OFS="\t" ' BEGIN { # Loop on the content of file-A # to ...
Join two files, matching on a column, with repetitions
1,391,717,026,000
I have two files in file A, there are sequence_numbers in the other file B, there are many columns, and the first column is sequnce numbers, I want to get a files with all the lines in the B with the sequence numbers which are in the A how can I achieve this? thanks like file A 1 3 8 9 20 file B 1 kfjk 3243424 2 fkd...
You want join (1), I guess: For each pair of input lines with identical join fields, write a line to standard output. The default join field is the first, delimited by whitespace. When FILE1 or FILE2 (not both) is -, read standard input. [0 1075 12:50:10] ~/temp/sx % join A B 1 kfjk 324342...
intersection of two files according to the first column
1,391,717,026,000
I have long text files with space-delimited fields: cat file1.txt Id leng sal mon 25671 34343 56565 5565 44888 56565 45554 6868 23343 23423 26226 6224 77765 88688 87464 6848 66776 23343 63463 4534 cat file2.txt Id number 25671 34343 76767 34234 23343 23423 66776 23343 cat output.txt Id leng sal mo...
In order to use join, you need to make sure that FILE1 and FILE2 are sorted on the join fields. The following command should do the trick: join -v1 <(sort file1.txt) <(sort file2.txt)
Compare two file columns
1,391,717,026,000
I have this file1.txt deiauk 9 kespaul 8 luktol 7 titkur 6 and other file2.txt kespaul b deiauk a And I want to merge both files in one by first value, so my result should be deiauk 9 a kespaul 8 b luktol 7 titkur 6
sort file2.txt | join -a 1 file1.txt - join requires sorted input. The '-' specifies that standard input will be used for the second file, which allows the output of sort to be used as input. The '-a 1' specifies that non-matching lines from the first file will be included in the output.
Merge two files
1,391,717,026,000
I was wondering if anyone would know the complexity of the Unix join command? I had assumed that it might be linear since both files need to be sorted. Someone insisted to me that it was logarithmic which I doubt. Or perhaps it depends on the files and can be logarithmic (or N*log(N)) when one of the files is small an...
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 Lite2. The snippet below comes from a current OpenBSD system, for comparison, this is a link to the BSD 4.4 Lite2 code ...
Unix join command complexity
1,391,717,026,000
Given the files: 1.txt 1, abc, 123, 456, 789 2, lmn, 123, 456, 789 3, pqr, 123, 456, 789 2.txt 1, abc, 123, 000, 000 3, lmn, 123, 000, 000 9, opq, 123, 000, 000 OUTPUT.txt ID, NAME, X, 1A, 1B, 2A, 2B 1, abc, 123, 456, 789, 000, 000 2, lmn, 123, 456, 789, MISSING, MISSING 3, pqr, 123, 456, 789, 000, 000...
I don't think you can do it with join alone. You could do: join -t, -a1 -a2 -o0,1.2,1.3,1.4,1.5,2.2,2.3,2.4,2.5 -e MISSING 1.txt 2.txt | perl -F, -lape '@F[1..2]=@F[5..6] if $F[1] eq "MISSING"; $_=join",",@F[0..4],@F[7..8]' -p: use a line by line reading loop like in sed/awk -a, -F,: like awk, sp...
Join: Two files - but only append last two columns
1,391,717,026,000
I have a file1.txt USA Joe 123.123.123 Russia Marry 458.786.892 Canada Greg 151.844.165 Latvia Grace 125.895.688 and file2.txt 1 123.123.123 2 151.844.165 3 465.879.515 and I want to create a new file result.txt where I print my only those lines that adresses (xxx.xxx.xxx) are both in file1 and file2 so my result s...
You can try: awk 'FNR==NR{a[$2];next};$NF in a' file2.txt file1.txt > result.txt
Awk: compare two files
1,391,717,026,000
I have 4 tsv (tab separated) files that look like this: file_1: abc 1 def 2 ghi 3 file_2: abc 2 ghi 3 file_3: def 1 ghi 2 jkl 4 file_4: ghi 3 jkl 4 I want to join those file to get 1 tsv file like this: dataset file_1 file_2 file_3 file_4 abc 1 2 def 2 4 ghi 3 3 ...
$ 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 } print "" for (dataset in datasets) { printf "%s", dataset for (fname in fnames) { print...
Joining multiple column from different file using awk
1,391,717,026,000
I need to merge below 2 files: file1: TABLES REF-IO HEAD-IO DIFF-IO test 200 500 -300 exam 2 3 -1 final 2 1 1 mail 4 2 2 TOTAL 208 506 -298 file2: TABLES REF-SELECT HEAD-SELECT DIFF-SELECT test 5 7 -2 game 3 3 0 exam 0 7 -7 final 12...
awk ' NR==FNR {vals[$1] = $2 " " $3 " " $4; next} !($1 in vals) {vals[$1] = "0 0 0"} {$(NF+1) = vals[$1]; print} ' file2 file1 TABLES REF-IO HEAD-IO DIFF-IO REF-SELECT HEAD-SELECT DIFF-SELECT test 200 500 -300 5 7 -2 exam 2 3 -1 0 7 -7 final 2 1 1 12 6 6 mail 4 2 2 0 0 0 TOTAL 208 506 -298 20 23 -3
Merge 2 files based on all values of the first column of the first file
1,391,717,026,000
file1: abc|123|check def|456|map ijk|789|globe lmn|101112|equator file2: abc|123|check def|456|map ijk|789|equator lmn|101112|globe EXPECTED OUTPUT: ijk|789|equator lmn|101112|globe Current awk script: awk 'BEGIN{OFS=FS="|"} NR==FNR{a[$3]=$3;next}!($3 in a)' file1 file2 This does comparison based on array content. ...
If I understand you correctly, you want to print a line from file2 if the 3rd field is different to the corresponding entry in file1. If so, this should do it: awk 'BEGIN{FS="|"} NR==FNR{a[$1,$2]=$3;next}(a[$1,$2]!=$3)' file1 file2 Yours wasn't working because you were taking $3 as the key for array a and $3 is not ...
Comparing files line by line in awk with delimiter
1,391,717,026,000
I have two files with tab-separated values that look like this: file1: A 1 B 3 C 1 D 4 file2: E 1 B 3 C 2 A 9 I would like to find rows between files 1 and 2 where the string in column 1 is the same, then get the corresponding values. The desired output is a single file that looks like this: ...
GNU coreutils includes the command join that does exactly what you want if line sorting in the result is irrelevant: join <(sort file1) <(sort file2) A 1 9 B 3 3 C 1 2 If you want the tabs back, do: join <(sort file1) <(sort file2) | tr ' ' '\t' A 1 9 B 3 3 C 1 2 Or use the t option to join. (<() aka pro...
Find common elements in a given column from two files and output the column values from each file
1,391,717,026,000
I have a 2 files with the following: File1.txt A 1 B 2 C 5 Z 3 File2.txt A 4 B 7 C 10 D 11 What I would like to do is create something like A 1 4 B 2 7 C 5 10 D - 11 Z 3 - Is there a utility that does this? If not how Can this be done? Using a find and awk or something?
join -a1 -a2 -o 0,1.2,2.2 -e - file1.txt file2.txt
Joining two files
1,391,717,026,000
I have 2 files, one having 2 columns, another having 1 column. The second file is sorted using sort -u. Now the task is I need to join this column with the first column of the first file, which is not sorted. So what will be the syntax? Will join -j 1 file2.txt sort -s -n -k 1 file1.txt work? The output I want is act...
No need to use non-standard process substitution (<(...)) here: sort file1 | join -o1.2 - file2 | uniq
How to sort and join at the same time?
1,391,717,026,000
I have two files in these formats: file1 : air smell hand dude road cat file 2: air,4,21,01,13,3,2 smell,21,4,2,5,6 dude,1,31,42,1 road,1,4,2,1,4 cat,1,5,6,3,1 hand,1,4,2,1,6 mouse,1,3,5,6,2 what I want to do is print the entire row of file 2, if the first string in column 1 of file 2 is found in file 1, and I wan...
This should do it: awk -F, 'FNR==NR {a[$1]; next}; $1 in a' file1 file2 edit: Interpreted the wrong file for the ordering. A new attempt (requires gawk if that is acceptable) gawk -F, ' FNR==NR {a[NR]=$1; next}; {b[$1]=$0} END{for (i in a) if (a[i] in b) print b[a[i]]} ' file1 file2 edit 2: With nowmal ...
comparing the first column of two files and printing the entire row of the second file if the first columns match
1,391,717,026,000
Objective: Merge the contents of two files using common key present in the files file1.txt ========= key1 11 key2 12 key3 13 file2.txt ========= key2 22 key3 23 key4 24 key5 25 Expected Output : ================== key1 11 key2 12 22 key3 13 23 key4 24 key5 25 Appro...
With awk, try: awk '{a[$1]=($1 in a)?a[$1]" "$2:$2};END{for(i in a)print i,a[i]}' file1 file2 For huge files, you should use join instead of awk approach, since when awk approach will store all files content in memory before printing out.
Why isn't this awk command doing a full outer join?
1,391,717,026,000
I have multiple .txt files and would like to merge them together base on their first column (numeric), and fill in "NULL" if there's any missing data. File_1: 1 a 2 b 3 c File_2: 3 c 4 d 5 e File_3: 4 d 5 e 6 f Expected_Output: 1 a NULL NULL 2 b NULL NULL 3 c c NULL 4 NULL d d 5 NULL e e 6 NULL NULL f join -t $'\...
You are almost there. Using your command, we get: $ join -t $'\t' -a 1 -a 2 -1 1 -2 1 -e NULL -o 0,1.2,2.2 file_1 file_2 | join -t $'\t' -a 1 -a 2 -1 1 -2 1 -e NULL - file_3 1 a NULL 2 b NULL 3 c c 4 NULL d d 5 NULL e e 6 f Lines just don't have ...
Join multiple files
1,391,717,026,000
I find join pretty useful. It lets you join file1 against file2 on key fields. Is it possible to do so dynamically against the results of a command, like: join -1 1 -2 1 file1 'curl http://example.com?code=$1&fmt=csv' Maybe using xargs or named pipes? Ideally it would do one "lookup" per record/line in file1
Yes, if your shell supports process substitution (bash and ksh93 does) you may do like this: $ join file1 <( yourcommand ) This runs the join command with file1 and a file descriptor in /dev/fd connected to the standard output of yourcommand (which would be your curl thingy). Note that join expects all input to be so...
Can I `join` against a command?
1,391,717,026,000
I have two files. The first file has the following format: 10D0325 2465 0 0 -9 -9 10D0598 2567 0 0 -9 -9 10D0562 2673 0 0 -9 -9 10D0175 2457 0 0 -9 -9 10D0241 2209 0 0 -9 -9 10D0954 2312 0 0 -9 -9 10D0446 2489 0 0 -9 -9 The second file has this format: 10D0325 1 10D0598 1 10D0175 2 10D0954 1 10D0446 2 What I want to...
That's pretty easy with awk: awk 'NR==FNR{a[$1]=$2;next}{print $0,a[$1]}' file2 file1 First (when file2 is being read) we create an array a which stores second column from file2, indexed with first column. And then we print file1 adding value from an array.
Adding column based on matching of second column
1,391,717,026,000
I have a directory ballgown, in which there are around 1000 subdirectories as sample names. Each subdirectory has a file t_data.ctab. The filename is the same in all subdirectories. ballgown |_______TCGA-A2-A0T3-01A |___________ t_data.ctab |_______TCGA-A7-A4SA-01A |__...
Try this simple way: first do: awk 'FNR==1 { print substr(FILENAME,1,16) >substr(FILENAME,1,16)".tmp" } FNR >1 { print $12 > substr(FILENAME,1,16)".tmp" } NR==FNR{ print $6 >"first_column.tmp" }' TCGA-A*/t_data.ctab then paste them together with comma delimited file (remove -d, if you want to have Tab inst...
How to create a new file with required columns from different multiple files in linux?
1,391,717,026,000
I'm using join command under linux, but the results vary between different machines. I have two simple files: cat 1.txt a aaa,0.2 b bbb,0.3 c ccc,0.5 cat 2.txt a aaa,0.2 b bbb,0.3 c ccc,0.6 I'm running the following command join -a 1 -1 1 -2 1 -t "," -o 1.1' '1.2' '2.2 <(cat 1.txt| sort -t ",") <(cat 2.txt| sort -...
Fix your files with dos2unix, or if that's not installed: sed -i 's/\r$//' {1,2}.txt
Truncated result returned by JOIN
1,391,717,026,000
I would like to merge a variable from one file to another in linux. The first variable contains the name I want to merge files on. I have sorted both files using both -f and -k: sort -f -k 1,1 SCZ.N.tmp> SCZ.N.tmp.sorted and sort -f -k 1,1 1kg.tmp > 1kG.ref_file.sorted However, when I join both files with this command...
You are sorting the files with the -f option, as case-independent keys. However, join expects the keys in normal sorted sequence. You should add the -i option to the command-line for join, to have it ignore case differences. Alternatively, omit the -f option from both sorts. Edit: also found another possibility here. ...
Joining two sorted files gives error: join: <file>:112855: is not sorted:
1,391,717,026,000
I am looking to compare two files, printing only records with a matching ID number and without duplicate records. I have two files: file1.txt contains: Simons 0987768798980 West 09809867678 Vickers 768774564650 Simons 76867790987 Peterson 24346576865 Simons 76867790987 Holister 87879655456 Peterson 87686765766 And, f...
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 in id2id{ ... } to be evaluated to true for the values that are empty strings. The solution is to replace the code id2...
Print Arrays Without Duplicates in AWK
1,391,717,026,000
Is there some workaround to join multiple files at once based on the first column? Usually, I would do: join File1 File2 > File1+File2 and File1+File2 File3 > final_output Example files: File1: 1 test1 2 test3 3 test4 4 test5 7 test7 File2: 1 example1 2 example2 3 example3 4 example4 8 example8 File3: 1 foo1 2 foo2...
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 files must be sorted already (sort -k 1b,1, numerical sorted like your example may not work!). So the example above sorted ...
Merge multiple files with join
1,391,717,026,000
I want to merge two files. Files A.txt 001;abc;def;ghi;jkl;pqr 002;abc;def;ghi;jkl;pqr 003;abc;def;ghi;jkl;pqr 004;abc;def;ghi;jkl;pqr 005;abc;def;ghi;jkl;pqr . Second File B.txt 001;mno 002;mno 003;mno 004;mno 005;mno to have a text file C.txt 001;abc;def;ghi;jkl;mno;pqr I am able to merge these two files but I do...
join will print each line of sorted input files that have the same first *(by default) field. So, setting the field delimiter (-t) to ; you get: $ join -t\; A.txt B.txt 001;abc;def;ghi;jkl;pqr;mno 002;abc;def;ghi;jkl;pqr;mno 003;abc;def;ghi;jkl;pqr;mno 004;abc;def;ghi;jkl;pqr;mno 005;abc;def;ghi;jkl;pqr;mno Combini...
Merging Two Files columns in order
1,560,507,233,000
I'm trying to join two csv files, sorted, and with tab delimiter. I'm new to the join command, so I'm not too sure about how to use it, but it seems to be replacing every tab in the files with spaces (messing up the alignment). The command I'm using is: join -1 5 -2 2 -t $'\t' -o $order --header file1.csv file2.csv |...
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 column, use its output delimiter set flag with -o join -1 5 -2 2 -t $'\t' -o $order --header file1.csv file2.csv | colu...
join replaces tabs by spaces
1,560,507,233,000
I have two files t1 and t2. root@localhost:~# root@localhost:~# cat t1 udp UNCONN 0 0 0.0.0.0:68 0.0.0.0:* users:(("dhclient",pid=479,fd=7)) 479 tcp LISTEN 0 128 127.0.0.1:6060 0.0.0.0:* users:(("gggg-ruit",pid=24968,fd=5)) 24968 root@localhost:~# root@localhost:~# cat t2 root 88 0.0 0...
They’re sorted numerically, but join requires them to be sorted lexicographically: 24968, then 479; and 24965, 24968, 88, then 96.
The "join" utility reports: file is not sorted, but in fact it is sorted
1,560,507,233,000
I have two files which are pipe-delimited and may have column 1+column2 matches in both, or one file may have the entry while the other does not. Assume my match-key I am going off of equals $1"-"$2 using a pipe '|' as the FS. file1 1111|AAA|foo|50 1111|BBB|foo|30 2222|BBB|foo|10 file2 1111|AAA|bar|10 1111|CCC|bar|20...
With your approach you have to use join twice (or change your approach to do it with a single join invocation) : print the common lines and the unpairable lines from file1 with join -t'|' -e0 -a1 -o 1.2,1.3,1.5,2.5 <(<file1 awk -F'|' '{print $1"-"$2"|"$0}' | sort -t'|' -k1,1) <(<file2 awk -F'|' '{print $1"-"$2"|"$0...
Joining entries based off of column using awk/join
1,560,507,233,000
Is it possible to copy the whole rows of File1 in a new File3 following the instruction given by File2 by using a simple bash script using sed or awk? File1: /*two or more columns*/ AC 456324 DC 689712 GH 123677 KL 236587 File2: /*one column*/ AC DC File3: AC 456324 DC 689712 I'm actually perform...
With grep grep -Ff File2 File1 With awk awk 'NR==FNR {a[$1]++;next} a[$1]' File2 File1
Merging files by rows
1,560,507,233,000
My question is similar to this one: Merge multiple columns based on the first column values I have multiple files (10+) that I want to merge/join into one output file, for example: file 1 2000 0.0202094 2001 0.0225532 2002 0.02553 2003 0.0261099 2006 0.028843 file 2 2000 0.0343179 2003 0.039579 2004 0.0412106 2006 0....
Use join: join -a1 -a2 -e 1 -o auto <(join -a1 -a2 -e 1 -o auto file1 file2) file3 see in the man join -a FILENUM also print unpairable lines from file FILENUM, where FILENUM is 1 or 2, corresponding to FILE1 or FILE2 -e EMPTY replace missing input fields with EMPTY -o FORMAT obey FORMAT while constructing output li...
Merge multiple files by first column
1,560,507,233,000
I have two text files. File 2 has logs over 1,000,000. File 1 has IP addresses line by line. I want to read file 2 lines and search these lines in file 1, I mean: file 1: 34.123.21.32 45.231.43.21 21.34.67.98 file 2 : 34.123.21.32 0.326 - [30/Oct/2013:06:00:06 +0200] 45.231.43.21 6.334 - [30/Oct/2013:06:00:06 +0200] ...
Join + sort If you're trying to find IP's that are present in both, you can use the join command but you'll need to use sort to pre-sort the files prior to joining them. $ join -o 2.2 <(sort file1) <(sort file2) Example $ join -o 2.2 <(sort file1) <(sort file2) 1.765 0.326 4.754 3.673 6.334 Another example file 1a: ...
compare files line by line and create new one bash programming
1,560,507,233,000
Because the input to join must be sorted, often the command is called similarly to: join <(sort file1) <(sort file2) This is not portable as it uses process substitution, which is not specified by POSIX. join can also use the standard input by specifying - as one of the file arguments. However, this only allows for...
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 by setting up those fifos (using /dev/fd/ instead of named pipes where available). For example, in bash: $ echo join <(sort file1) <(so...
Join two unsorted files with POSIX? [duplicate]
1,560,507,233,000
I have N files like so: file1.txt Header1,Header2,Header3,Header4,Header5 A,B,RANDOM,1,2 C,D,RANDOM,3,4 fileN.txt Header1,Header2,Header3,Header4,Header5 A,B,RANDOM,1,2 C,D,RANDOM,3,4 They all have the same headers. I would like to sum all of Header4 and Header5 based on Header1 and Header2. So all items with the A...
Assuming ordering of output is no requirement... awk ' BEGIN { FS=OFS=SUBSEP="," } { s4[$1,$2]+=$4 ; s5[$1,$2]+=$5 } END { for (k in s4) print k,s4[k],s5[k] } ' file1 ... fileN (Edited and restructured for better legibility.)
Awk sum csv columns based on fields
1,560,507,233,000
I need to match two unequal files using $1 of file 1 and $2 of file 2 and print $1 of file 2 on file 1. Input file 1 101 2 101 5 101 7 103 2 103 3 103 4 105 3 105 2 Input file 2 24 101 23 103 26 105 Desired output 101 2 24 101 5 24 101 7 24 103 2 23 103 3 23 103 4 23 105 3 26 105 2 26 I have tried the following...
A classic job for join: join -1 1 -2 2 file1 file2 -1 1 specifies the field in the first file. -2 2 specifies the field in the second file.
How can I match a field in two different files and append together in output file?
1,560,507,233,000
I have two files. I want to compare the content of one file with the other one. If there is a matching line between both files then print the line and its line number in each file. Example: File 1: ABC PQR MNO XYZ File 2: qqqq wewe ABC acdd abcc nop MNO Expected output: ABC 1 3 MNO 3 7 ..
With awk you could process the first file, store the lines ($0) and their corresponding line numbers (NR) (as indices/values) in an associative array (l[$0]) then process the second file and if a line is in the the array index print it along with the value of l[$0] and current line number (FNR): awk 'FNR==NR{l[$0]=...
Print common lines between two files along with their line numbers in those files
1,560,507,233,000
I have: File 1 like: sting_of_printable_characters*sting_of_printable_characters*sting_of_printable_characters*ALPHANUMERIC_PATTERN File 2 like: sting_of_printable_characters*ALPHANUMERIC_PATTERN where * is a field separator and the alphanumeric pattern is always the last field in the line. I am completely stumped o...
With join you could try like this: join -t\* \ <(sed 's/\(.*\)\(\*\)\(.*\)/\3\2\1/' file1 | sort -t\* -k1,1) \ <(sed 's/\(.*\)\(\*\)\(.*\)/\3\2\1/' file2 | sort -t\* -k1,1) The two seds move the last field to the beginning of line, e.g. field1*field2*...field(N-1)*field(N) becomes field(N)*field1*field2*...*field(N-...
Join (large) files on alphanumeric pattern
1,560,507,233,000
bash-3.2$ cat sample.log sample.log.1 sample.log.2 ID COL1 COL2 COL4 1 col1 col2 col4 2 c1 c2 c4 3 co1 co2 co4 ID COL3 COL1 1 col3 col1 2 c3 c1 3 co3 co1 ID COL1 COL2 COL3 1 col1 col2 col3 2 c1 c2 c3 3 co1 co2 co3 I need to write an awk script such that it gives me the values of the columns f...
You can use the join command to perform this task: join -1 1 -2 1 sample.log sample.log.1 -o 1.1,1.2,1.3,2.2 The output will be 'single space' separated, but you can use awk to reformat it to be column aligned. Note that the join input files must be sorted.
merging files and getting column values based on id field
1,560,507,233,000
I have file1 (sample): 60108903374 60172485121 60108919381 60174213128 60108919951 60108919970 601112020106 601112020107 601112020108 601112020113 601112020114 60175472940 And file2: 60179970001,A 60172681920,A 60174202041,A 60172514180,A 60174314679,A 60174325306,A 60175472940,A 60174213128,A 60175328984,A 601753498...
join -t, <(sort file1) <(sort -t, file2) The above does the job.
How to merge two files of different lines and column and output matching lines with colums?
1,560,507,233,000
I have a file name "conf1" containing variables like: name='john' last='' custom='1000' and another file name conf2 like this: name='john' last='star' I want to merge between them to one file but in a way that the merged file contain the variable in the same order I source them. for example if I source conf1 and the...
You could do: $ awk -F= '{l[$1]=$0};END{for (i in l) print l[i]}' conf1 conf2 custom='1000' last='star' name='john' Note that the order of the lines in the output is not guaranteed (based on how awk stores the array internally in a hash table), but settings in conf2 will override those in conf1. where awk -F= ... co...
source multiple files and output one file
1,560,507,233,000
I want to perform what some data analysis software call an anti-join: remove from one list those lines matching lines in another list. Here is some toy data and the expected output: $ echo -e "a\nb\nc\nd" > list1 $ echo -e "c\nd\ne\nf" > list2 $ antijoincommand list1 list2 a b
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: $ awk 'NR==FNR{++a[$0]} !a[$0]' list2 list1 a b If the files are already sorted, an alternative to join -v 1 would be com...
How to do anti-join or inverse join in bash
1,560,507,233,000
I have a tab separated file that looks like this: 123 some text 123 some different text 334 some other text 341 more text and I want to do two things. One is to order everything numerically (this is easy to do) and the other is to remove a line if it's number is already present. I.e. the output would look lik...
If you want to sort/test for uniqueness the first field specifically, and your system has the GNU coreutils version of sort, then I think you could just use sort -nu file viz. $ sort -nu file 123 some text 334 some other text 341 more text From info coreutils 'sort invocation' The commands sort -u and sort |...
Removing lines with a single common field
1,560,507,233,000
I have two files. file_1.txt looks like this: R1 C1 C2 C3 C4 C5 R2 C1 C2 C3 C4 C5 R3 C1 C2 C3 C4 C5 R4 C1 C2 C3 C4 C5 R5 C1 C2 C3 C4 C5 R6 C1 C2 C3 C4 C5 R7 C1 C2 C3 C4 C5 R8 ...
This problem makes for a typical application of awk awk 'NR == FNR{a1[$1]=$2; a2[$1]=$3; next}; $1 in a1{$5=a1[$1]; $6=a2[$1]};{print}' file_2.txt file_1.txt You may have to set the output field separator explicitly to tab, in which case awk -v OFS='\t' 'NR == FNR{a1[$1]=$2; a2[$1]=$3; next}; $1 in a1{$5...
Join two files with different number of columns and rows
1,560,507,233,000
Table 1 (tab separated): NC_000001.11 1243 A T 0.14 NC_000005.11 1432 G C 0.0006 NC_000012.12 1284 A T 0.93428 NC_000021.9 9824 T C 0.9 Lookup table (tab separated) - this is in fact huge, around 6G gzipped: NC_000001.11 1243 rs73647921 A T NC_000005.11 1432 rs75444 G C NC_000012.12 1284 rs754723 A T ...
With awk (and bash) you can write awk ' BEGIN {FS = OFS = "\t"} NR == FNR {pvalue[$1,$2,$3,$4] = $5; next} FNR == 1 {print "MarkerName", "P-Value"} { key = $1 SUBSEP $2 SUBSEP $4 SUBSEP $5 sub(/\r$/, "", key) } key in pvalue {print $3, pvalue[key]} ' table1.tsv <(zcat lookup.tsv.gz) awk ...
Looking up values in one table and outputting it into another using join/awk
1,560,507,233,000
I have seven (or eight and so on) files with same number of lines. file1 1.001 1.002 1.003 1.004 file2 2.001 2.002 2.003 2.004 file3 3.001 3.002 3.003 3.004 etc. Desired output: 1.001;2.001;3.001;4.001;5.001;6.001;7.001 1.002;2.002;3.002;4.002;5.002;6.002;7.002 1.003;2.003;3.003;4.003;5.003;6.003;7.003 1.004;2.004;...
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;4.003;5.003;6.003;7.003;8.003 1.004;2.004;3.004;4.004;5.004;6.004;7.004;8.004 But, if you must use awk: $ awk '{a[FNR]...
Join seven files with awk line-by-line
1,560,507,233,000
I have two different files and I'd like to do a merge of their information using the first column. File1.txt A,info1,info2 234,info3,info4 CD,info5,info6 File2.txt 234,ccc,bb CD,aaa,dd Expected output.csv A,info1,info2,, 234,info3,info4,ccc,bb CD,info5,info6,aaa,dd I tried with awk (not my script) join and grep but...
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 with the value of -e option which by default it's space character but you can change it to any string you want); $ join -...
Merge two files using first column
1,560,507,233,000
I have multiple files which need to be merged based on first column from each file File1: foo 12 jhdfeg 25 kjfdgkl 37 File 2: foo 23 jhdfeg 45 File 3: foo 35 djhf 37 The output should be like this file1 file2 file3 foo 12 23 35 jhdfeg 25 45 0 kjfdgkl 37 0 0 d...
perl -F'\s+' -lane ' $. == 1 and @ARGC = ($ARGV, @ARGV); # initialize the @ARGC array exists $h{$F[0]} or $h[keys %h] = $F[0]; # needed to remember order $h{$F[0]}->[@ARGC-@ARGV-1] = $F[1]; # populate hash END { $, = "\t"; # set the OFS to TAB print q//, @ARGC; # ...
Paste multiple files based on the first column into one single file
1,560,507,233,000
I need to combine two files into a single file with all columns from both files. I am providing my example files. File 1 chr loc T1 C1 chr1 100 2 3 chr1 200 3 4 chr2 100 1 4 chr2 400 3 1 File 2 chr loc T2 C2 chr1 100 1 2 chr1 300 4 1 chr2 100 7 5 chr2 500 1 9 and output file...
join -a1 -a2 -e 0 -o 0,1.2,1.3,2.2,2.3 \ <(sed 's/ \+/_/' file1 | sort) \ <(sed 's/ \+/_/' file2 | sort) | sed 's/_/ /' | column -t | sort chr loc T1 C1 T2 C2 chr1 100 2 3 1 2 chr1 200 3 4 0 0 chr1 300 0 0 4 1 chr2 100 1 4 7 5 chr2 400 3 1 0 0 chr2 500 0 0...
combine two files to single file with combined columns
1,560,507,233,000
I have four different files: file1, file2, file3, file4. Each file has 2 different columns separated by tab. I want to match first column of file1 (as reference) with the first column of second file, third and fourth file and print first column which is matching and second column of each file which has matching first ...
In perl, the tool for this job is a hash. A hash is a set of key-value pairs which makes this sort of cross referencing quite easy. Note - this will ONLY work if the first field is unique: #!/usr/bin/env perl use strict; use warnings; my %data; while (<>) { my ( $key, $value ) = split; push( @{ $data{$key} },...
compare multiple files(more than two) with two different columns
1,560,507,233,000
I have 2 CSV files for each date (csv_2014_4_15 and csv_2014_4_16) , with basic structure and couple of unique columns as below. id,name,created_at,updated_at,other columns 12, joe, 2013-1-1 18:30, 2014-2-1 12:00 56, bob, datetime, datetime I want to merge the 2 csv files based on these these conditions. My code so...
Try this: $ awk -F',' -v t="$(date +"%Y-%-m-%-d")" ' FNR == NR { u[$1] = $4; next; } $4 > u[$1] { print; next; } t ~ $3 ' file_1 file_2 Explanation We get today date, save it in variable t While reading file_1 FNR == NR, we save each updated time of each id in asso...
Merge csv files with conditions
1,560,507,233,000
I have a script equijoin2: #! /bin/bash # default args delim="," # CSV by default ...
"$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 argument instead of the two arguments -a and 2). Your join is probably GNU join in that it accepts options after non-op...
Why do I have "join: extra operand '/dev/fd/62'" error?
1,560,507,233,000
I have two files with columns separated by tab and I want to merge them file a01 a= b= c= d= e= f= g= h= i= j= k= l= m= n= 0= file b01 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 desired output a=1 b=2 c=3 d=4 e=5 f=6 g=7 h=8 i=9 j=10 k=11 l=12 m=13 n=14 0=15 but when I run the command ...
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 b01 | awk '{n=NF; for (i=n/2;i>0;i--) {$i = $i""$(i+n/2); NF--}} 1' a=1 b=2 c=3 d=4 e=5 f=6 g=7 h=8 i=9 j=10 k=11 l=12 m=1...
join returns nothing
1,560,507,233,000
I asked a similar question yesterday (Merging two tables including multiple ocurrence of column identifiers) but ran into a problem with unique lines. I would like to merge two tables based on column 1: File 1: 1 today 1 green 2 tomorrow 3 red File 2: 1 a lot 1 sometimes 2 at work 2 at home 2 sometime...
I would do this in Perl: #!/usr/bin/env perl use strict; my (%file1,%file2); ## Open the 1st file open(A,"file1"); while(<A>){ ## Remove trailing newlines chomp; ## Split the current line on tabs into the @F array. my @F=split(/\t/); ## This is the tricky part. It adds fields 2-last ## to ...
Merging two tables including multiple ocurrence of column identifiers and unique lines
1,560,507,233,000
I have 2 set of files. File one contains ID's ex: 1111 2222 6666 3333 4444 File two contains ID and username: 1873 Neil 1111 Roger 7632 Tim 3333 Oscar 8723 Greg 4444 Roy 6666 Patrick I want to extract the ID and username, but only the ones that has the same ID as in file 1. I did the normal grep -f file1 file2, on t...
Try doing this using join instead of grep, this will be more suitable : $ join <(sort file1) <(sort file2) 1111 Roger 3333 Oscar 4444 Roy 6666 Patrick If your shell lack process substitutions <( ), you can do : sort file1 > new_file1 sort file2 > new_file2 join new_file1 new_file2 Doc said : join writes to standa...
Matching two files for similar first line
1,560,507,233,000
I want to compare two CSV files with the following format. They not have headers. I want to compare them by a specific column (in this case, the 2nd one). The source CSV files around 4-5GB, so loading them into memory is won't work. If there's no matching column in old.csv, than every new line written into out.csv. Th...
Super simple with awk: $ awk -F'|' 'NR == FNR {old[$2]; next} !($2 in old)' old.csv new.csv "glider"|"person"|"airport" "glider"|"person2"|"airport" That stores the 2nd field of the old.csv file in the array named "old", and then for the new.csv file, it will print records where the 2nd field is not in the "old" arra...
Merging two CSV compared by a specific column only
1,560,507,233,000
I've been trying to use awk to merge two files when they have the same first column. Here are my example files: FileA.txt A2M 1 A4GALT 11 AAAS 35 AAGAB 7 FileB.txt A4GALT 2 AAAS 17 AAGAB 7 As you can see, the second file is missing the entry for A2M. If I am missing an entry, then I want the ent...
If you join two files, it's a job for join: join -1 1 -2 1 -a 1 -o 1.1 -o 1.2 -o 2.2 -e "0" FileA.txt FileB.txt Where: -1 1 -2 1 defines the fields to join (in both files the 1st) -a 1 to force join to print the unpairable lines from FileA.txt -o 1.1 1.2 2.2 is the output format and -e "0" defines the value to store...
Merge lines with matching first column
1,560,507,233,000
I have a requirement of comparing the first column between 2 pipe delimited files and if they match, I need to replace the 3rd Column in File 1 with 4th Column in File 2. File 1: 111|xyz|23345 222|abc|123 333|xyz|45667 444|xyz|5432 555|xyz|8976 File 2: 111|xyz|344|rtms 222|abc|222|xyzw 666|xyz|ggg|abde 888|xyz|ff|nn...
One-liner without the need for awk and a temporary file: join -t '|' -j1 -o 1.1 1.2 2.4 <(sort -t'|' -k1,1 file1) <(sort -t '|' -k1,1 file2) Using both join and awk: First, sort file2 based on 1st field and save it it file2.sort sort -k 1 file2 > file2.sort Now, using "|" as the delimiter, join file1 and file2.sort....
Compare 1st Column in 2 Files and Replace 3rd Column of File 1 with 4th Column of File 2
1,350,877,526,000
I am having some issues with a script that is using join to join two files. Eaxmple input files contains lines like this: Here are the input file and the output of the join command: D:\work\BuildScripts\3C>cat D:\temp\aaa.txt hzapplications\adn\adn4\adn4density\adn4_idd_module.cpp,83 hzapplications\adn\adn4\adn4densit...
It turns out that --ignore-case is the problem. It has an effect even when there are no uppercase letters because it treats all lowercase letters as uppercase, causing them to jump to the other side of the characters that are between the uppercase and lowercase in ASCII order: [\]^_ In normal sorted order, iolrt comes...
Using join with two files fails on larger file sizes
1,350,877,526,000
My files: file1.txt ========= key1 key1 key1 key1 key2 key2 key3 key3 key3 key4 key4 file2.txt ========= key1 22 key2 23 key3 24 Expected Output : ================== key1 22 key1 22 key1 22 key1 22 key2 23 key2 23 key3 24 key3 24 key3 24 All solu...
Awk solution: awk 'NR==FNR{ a[$1]=$2; next }$1 in a{ $2=a[$1]; print }' file2.txt file1.txt The output: key1 22 key1 22 key1 22 key1 22 key2 23 key2 23 key3 24 key3 24 key3 24 Or simply with join command: join -o1.1,2.2 file1.txt file2.txt
LEFT (OUTER) JOIN [duplicate]
1,350,877,526,000
I have many files like following in a directory "results" 58052 results/TB1.genes.results 198003 results/TB1.isoforms.results 58052 results/TB2.genes.results 198003 results/TB2.isoforms.results 58052 results/TB3.genes.results 198003 results/TB3.isoforms.results 58052 results/TB4.genes.results 198003 results/TB4.isofor...
GNU awk is used. I put this command in the bash script. It will be more convenient. Usage: ./join_files.sh or, for pretty printing, do: ./join_files.sh | column -t. #!/bin/bash gawk ' NR == 1 { PROCINFO["sorted_in"] = "@ind_num_asc"; header = $1; } FNR == 1 { file = gensub(/.*\/([^.]*)\..*/, "\\1", "g", ...
How to join files with required columns in linux?
1,350,877,526,000
I have two very large text files with space-delimited fields: File1 527858 51 2 27.92464882 8.63E-07 570289 82 2 30.12532071 2.87E-07 571034 90 2 29.26089611 4.43E-07 571033 90 2 28.56723908 6.26E-07 452403 104 2 28.27577506 7.24E-07 351390 100 2 28.16226794 7.67E-07 527858 50 2 ...
Here's the same basic idea as the awk in Archemar's answer, implemented in Perl: $ perl -lane '$#F>1?print"$l{$F[0]} @F[1..$#F]":($l{$F[0]}=$F[1])' file2 file1 rs435 51 2 27.92464882 8.63E-07 rs564 82 2 30.12532071 2.87E-07 rs654 90 2 29.26089611 4.43E-07 rs345 90 2 28.56723908 6.26E-07 rs665 104 2 28.27577506 7.24E-0...
match columns and replace
1,350,877,526,000
I have an invnentory file with a group of servers defined [prod] prod1 ansible_host=10.10.2.30 prod2 ansible_host=10.10.2.40 prod3 ansible_host=10.10.2.50 prod4 ansible_host=10.10.2.60 I want to define a variable that concatenates all servers in prod group delimited by comma. lets say variable is list, variable shoul...
The playbook below does the job - hosts: prod gather_facts: false tasks: - block: - set_fact: list1: "{{ ansible_play_hosts_all|product(['_IP'])|map('join')| join(',') }}" - debug: var: list1 - set_fact: list2: "{{ ansible_play_...
how to fetch ansible inventory & populate in an ansible variable?
1,350,877,526,000
I've been trying to merge two big files based on two key columns (Chromosome and Position) and I found out that the most efficient way seems to be awk. A sample of how my files look like is: file1.txt Gene_ID Chromosome Position Fst ENSG00000141424 18 33688658 0 ENSG00000141424 18 33688669 0 ENSG00000141424 18 33688...
A few correction of your code should fix it awk 'NR==FNR{A[$1,$2]=$6;next}{$5=A[$2,$3];if($5!="")print}' file2.txt file1.txt NR==FNR instead of NR=FNR is the condition when awk runs through the first file file2.txt On the second run when NR!=FNR we print out the line if there exists a join key in A The default field...
Joining two files based on two key columns awk
1,350,877,526,000
I want to join two files according to number in the file, two number same file Toyota model1 BMW model2 Benz model3 BMW model4 BMW model5 Benz model6 Benz model7 and second file class C model1 class E model2 class A model3 class W model4 class W model5 class C model6...
Use join. It looks like changing the first space character in the file to something other than space will be sufficient to convert the file whitespace-delimited records. Here's an implementation that replaces the first space with % and then joins on the second column of each file. $ cat file2 | sed -e 's/ /%/' | join ...
How to Sort and join according to Number/Counter inside the file?
1,350,877,526,000
I'm trying to be a bit effective and need a script or command solution. Say I make a file with 2 columns, or two files with one column, whichever is easier: AA1 B2 ZZ1 YYY XX1 AA2 B2 ZZ2 YYY XX2 AA3 B3 ZZ3 YYY XX3 AA4 B4 ZZ4 YYY XX4 ZZ5 YYY XX5 ZZ6 YYY XX6 ...
Assuming you have the following files: $ cat file1 ZZ1 YYY XX1 ZZ2 YYY XX2 ZZ3 YYY XX3 ZZ4 YYY XX4 ZZ5 YYY XX5 ZZ6 YYY XX6 ZZ7 YYY XX7 $ cat file2 AA1 B2 AA2 B2 AA3 B3 AA4 B4 Then use this awk: awk 'FNR==NR{a[c++]=$0} FNR!=NR{for(i in a){print $0,a[i]}}' file1 file2 FNR==NR applies only to the first file file1 a[...
Combine columns using awk? (Or other suggestions)
1,350,877,526,000
I have 'FileA': 10 10011300 10011301 T C 10 10012494 10012495 G A 10 10028691 10028692 A T 10 10093496 10093497 G A 10 10102457 10102458 C T 10 10103252 10103253 G C 10 ...
Okay, I actually forgot I asked this question but someone just upvoted this post and I have since figured it out so I'll go ahead and post the answer. For the first snippet of output I listed above, 10 10011300 10011301 T C 10 10012494 10012495 G A 10 10028691 ...
How do I compare one text file against about two dozen other text files and print out certain columns of each line whenever there is a match?
1,350,877,526,000
I am trying join two files by one common column (1ª) between two file (File1 and file2), resulting in file 3, but the resultant file is not working. Can someone help me? I used this command: awk 'NR==FNR {h[$1] = $0; next} {print h[$1],$0}' file1 file2 > file3 File 1. 1 1767 0 1986 28061997 1 1 1 0 29031998 972 34176...
Try this one liner!. awk 'FNR==NR{A[$1]=$0;next}{line="";for(i=2;i<=NF;i++)line=line $i" ";sub(" $","",line);if ($1 in A)print A[$1]" "line;}' file1.txt file2.txt > file3 Out 1 1767 0 1986 28061997 1 1 1 0 29031998 972 34176 9 1 9 9 55 97 42 1 0 0 5 8 25031998 2 11 1071997 943 11 1101997 944 11 1011998 951 18 1011998...
How join two files by one common column (1ª) between two file (File1 and file2)?
1,350,877,526,000
I have two unsorted files, each with two columns. For any line in file1 whose column1 value matches that of any line in file2, but whose column 2 values differ, I want to print the column 1 value and each column 2 value. If data from column1 in file 1 does not exist in file2 it can be discarded. I do not need to prese...
Read the 2nd file, save the content into an array (key = 1st field, value = 2nd field) then read the 1st file and check if 1st field is a common key and if the corresponding 2nd field is different. If the result is positive, print the key and the two values: awk 'BEGIN{FS=OFS=","}NR==FNR{z[$1]=$2;next} {if (z[$1] && (...
If column 2 of file2 is diferent than column2 of file1 print column1 from file1 and column2 from both files
1,350,877,526,000
file1.txt (50 lines) TERYUFV00000010753 TERYUFV00000009526 file2.txt (500 lines) TERYUFV00000009526 refids_739_known_8/10_target TERYUFV00000018907 refids_12023_known_21/22_target TERYUFV00000010753 refids_11775_known_1/1_target Output.txt TERYUFV00000010753 refids_11775_known_1/1_target TERYUFV00000009526 refids_7...
fgrep -f file1.txt file2.txt Here we are obtaining search pattern from file1.txt and searching it in file2.txt. As the text is fixed we are using fgrep for faster search operation.
compare two files get identical list
1,350,877,526,000
I have a CSV file that is like this name;address;phone;email John;123 La Sierra;555-121212;[email protected] Nick;456 La Bongaa;555-121232;[email protected] Carl;789 La Fountain;553-121212;[email protected] and I want to remove the last entry making it be like name;address;phone; John;123 La Sierra;555-121212; Nick;4...
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 (-F\;) and output fields (OFS=';') are said to be separated with ";". The same with sed: sed 's/[^;]*$//' data.csv => ...
Removing a field from a comma delimited text with accented chars
1,350,877,526,000
I have two files--file1 and file2 that I want to join but some fields are missing in second file for which I want to insert string 'null'. One requirement is that the keys must be in the same order as in file1. The input files and expected output result are as below: file1.txt file2.txt a 7 nah a anau b 0 blah...
join + sort solution: join -o1.1,1.2,1.3,2.2 -a1 -e"null" <(sort file1.txt) <(sort file2.txt) The output: a 7 nah anau b 0 blah null c 5 bah bau d 1 gah cau e 0 hah null
Join two files, keep key order of first file and fill missing values with string 'null'
1,350,877,526,000
I used the sort command to numerically sort file1.txt. ~]# sort -n -o file1.txt file1.txt 0 Barack 50 George 60 Ronald 100 Bill The sort check command states the file is not sorted. ~]# sort -c file1.txt sort: file1.txt:4: disorder: 100 Bill I manually modify the file and place "100 Bill" second. ~]# cat file1.txt 0...
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 operation again numerically. When you run sort -c, you need to specify the same sorting parameters as you used to sort the fil...
Numeric sort fails to properly sort file
1,350,877,526,000
I have two CSV files and am trying to merge them based on the first column in the first file matching the third column in the second file. They lines are not sorted. file1.csv: android,1,2 osx,2,5 file2.csv: Converting,:Developer::|[E],android,Exact,,,,8,31 Converting,:Developer::|[E],osx,Exact,,,,8,31 Converting,:De...
You could use join for this join -1 1 -2 3 -t ',' -a 2 -o 2.{1..9} 1.{2..3} <(sort file1.csv) <(sort file2.csv) -1 and -2 specifies which field from the files to compare -t specifies the seperator to use for the fields -a 2 says to print lines in <file2> that do not match -o configures the output based on <file>.<fi...
Compare columns from two CSVs and merge on matches
1,350,877,526,000
I have 2 files and want to inner join them using awk. This is written using sql server : SELECT [file1.column1],[file2.column2] FROM file1 INNER JOIN file2 on file2.column1 = file1.column5; This is the file i want to join: file1 : file2: so the key is column5 file1 and column1 file2. How to write them in awk langua...
One way: join -t"|" -1 5 -2 1 -o 1.1 2.2 file1 file2 -1 5 - Use the 5th column of file1 -2 1 - Use the 1st column of file2 -o 1.1 2.2 - Print as output 1st column of 1st file, 2nd column of 2nd file
How to join 2 files based one key and selected some specific column?
1,350,877,526,000
I have several lists with two fields - first field contain an URL, 2nd field an email-address (an account). The 2nd field is the same for all entries in a list. I concatenate the lists to one list, and sort it by the 1st field. Most entries are unique, but some are duplicates or triplicates (ie. the URL was in the l...
With sort + awk pipeline: sort -k1,1 file \ | awk 'url && $1 != url{ print url, acc } { acc = ($1 == url? acc FS:"") $2; url = $1 }END{ print url, acc }' OFS='\t' Sample output: url1 acct2 url2 acct1 url3 acct1 acct2 url4 acct2 acct3 acct5
List sorted on 1st field, how can I join 2nd field on lines where 1st field is the same?
1,350,877,526,000
I am a novice programmer. I am using unix's join command to self-join couple long files together. join -j30 test test2 col1 col2 ... col30 col1 col2 ... col30 A B ZZZ ^M A B ZZZ I am getting this ^M character in my output. Why is it there? and How would I remove it? EDIT: Below is a screensho...
The ^M means you are bringing over/editing file in Windows. Use the dos2unix command over the files to convert them to Unix text mode. DOS uses carriage return and line feed "\r\n" as a line ending, while Unix uses just line feed "\n". The ^M are a visual representation of the "extra" \r characters. To install the dos...
How to fix unix `join` command inserting ^M between join columns? -Unix
1,350,877,526,000
I'm struggling with this task: I have two files: file1 looks like: 102 13.342 103 7.456 105 6.453 107 3.567 108 4.210 file2 looks like: 0 098 0 0 0 -9 x 0 099 0 0 0 -9 x 0 100 0 0 0 -9 x 0 101 0 0 0 -9 x 0 102 0 0 0 -9 x 0 103 0 0 0 -9 x 0 104 0 0 0 -9 x 0 105 0 0 0 -9 x 0 106 0 0 0 -9 x 0 106 0 0 0 -9 x 0 107 0 0 0 ...
When there are so many fields involved I tend to prefer awk: $ awk 'NR==FNR{a[$1]=$2; next}{if($2 in a){$6=a[$2]}}1;' file1 file2 0 098 0 0 0 -9 x 0 099 0 0 0 -9 x 0 100 0 0 0 -9 x 0 101 0 0 0 -9 x 0 102 0 0 0 13.342 x 0 103 0 0 0 7.456 x 0 104 0 0 0 -9 x 0 105 0 0 0 6.453 x 0 106 0 0 0 -9 x 0 106 0 0 0 -9 x 0 107 0 ...
Joining two files matching two columns with mismatches and in each matching line, substitute second column from file 1 into 6th column in file 2
1,350,877,526,000
Imagine that we have two for example files. The first file is filled with unique names of employees created by combining the first two characters of the first name and the last 2 characters of the last name. Example : Peter Smith - Peht First file contains : Peht Mawo Stso Makr Bavo The second file contains recording...
Something simple like: mapfile -t names < file1 for name in "${names[@]}" do echo "${name}" $(grep -c "^$name " file2) done Will provide output like: Peht 2 Mawo 3 Stso 1 Makr 0 Bavo 2 The grep string says to anchor the username at the beginning (^) of the line, and enforce a trailing space after the line.
Elegant way of counting how many times patterns from a file occur in another file
1,350,877,526,000
I would like to create a simple bash function to use for my convenience. Following the answer given at: Joining bash arguments into single string with spaces I've been able to mash up this small piece of code: function gcm { msg="'$*'" eval "git commit -m ${msg}" } Now, this example is very convenient for commit ...
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-quoting constructs. % (){ print -l $* } a b c a b c % (){ print -l "$*" } a b c a b c % (){ local msg; msg="$*"; print ...
ZSH, concatenate passed in arguments into a single string
1,350,877,526,000
I'm trying to join two files removing duplicate head row and take only 1 last row for example: File1.txt head1 data1 data2 tail8 File2.txt head1 data3 data4 tail9 results desired in file3.txt: head1 data1 data2 data3 data4 tail8 or rail9 doesn't matter I try first this to remove duplicate head: awk '!seen[$0]++' fi...
$ awk 'NR==FNR{ if (NR>1) print prev; prev=$0; next } FNR>1' file1 file2 head1 data1 data2 data3 data4 tail9
Merge two file skip last row of 1 file with awk
1,350,877,526,000
I have a text file with format word @@@ type @@@ sentence on every line, sorted by 'word' in ascending order. Some lines however are not unique and they begin with the same word as the previous line, ie see word1 below: ... word0 @@@ type2 @@@ sentence0 word1 @@@ type1 @@@ sentence1 word1 @@@ type1 @@@ sentence2 word...
Assuming your input is sorted on both word and type fields as it appears from your posted sample input: $ cat tst.awk BEGIN { FS=" @@@ "; ORS="" } { curr = $1 FS $2 } curr != prev { printf "%s%s", ORS, $0 prev = curr ORS = RS next } { printf " ;;; %s", $NF } END { print "" } $ awk -f tst.awk file word...
Merge following portions of lines into current line in a 3 column file
1,350,877,526,000
I have a table A: 1 n m n ... 2 m n m ... 3 n m n ... 4 m n m ... 5 n m n ... I have a table B: 1 A 3 B 5 C I want to join the column 2 of table B with table A by matching column 1 of both tables, without removing the unique lines in table A to get the following (for no matches write a "NA"): 1 A n m n ... 2 NA m n...
From man join -a FILENUM also print unpairable lines from file FILENUM, where FILENUM is 1 or 2, corresponding to FILE1 or FILE2 -e EMPTY replace missing input fields with EMPTY so join -a1 -e 'NA' -o 0,2.2,1.2,1.3,1.4 A B 1 A n m n 2 NA m n m 3 B n m n 4 NA m n m 5 C n m n
Join without removing unique lines in Linux
1,350,877,526,000
I have two files: File1.txt 30 40 A T match1 string1 45 65 G R match2 string2 50 78 C Y match3 string3 File2.txt match1 60 add1 50 add2 match2 15 add1 60 add2 match3 20 add1 45 add2 and I want to obtain an output that looks like so: 30...
I found a solution: awk -F "\t" 'FNR==NR {a[$1] = $2 "\t" $3;next} $5 in a{print $0 "\t" a[$5]}' file2.txt file1.txt > outing.txt
Joining columns from files if they contain a match in another column
1,350,877,526,000
I have a file (file-1) that looks like this, DIP-10097N|refseq:NP_416170|uniprotkb:P30015 DIP-10117N|refseq:NP_414973|uniprotkb:P08177 DIP-10168N|refseq:NP_418766|uniprotkb:P15005 DIP-10199N|refseq:NP_415632|uniprotkb:P30958 DIP-10358N|refseq:NP_418659|uniprotkb:P28903 DIP-10440N|refseq:NP_289596|uniprotkb:P20082 DIP-...
This is wide known operation in awk — collect array from key-file then use the array to operate with second file values awk ' FNR==NR{ A[$2] = A[$2] " " $1 next } $1 in A{ for(i=1;i<=split(A[$1], B);i++) print $1 B[i] } ' file2 file1 Or a little ...
Comapring columns in 2 files and printing the values that differ
1,350,877,526,000
I'm trying to join two simple files in Solaris 5.8 Version as below: ~/temp/s: cat 1 work1 a 8058 51 work2 b 15336 51 ~/temp/s: cat 2 8058 77-11:29:32 /apps/sas 15336 100-12:23:49 /local/hotfix ~/temp/s: join -1 3 -2 1 1 2 8058 work1 a 51 77-11:29:32 /apps/sas (The other line is missing from the output) The output ...
I think this might be a bug with join. I just tried it on Fedora 14 using this version of join: $ join --version join (GNU coreutils) 8.5 Example $ join -1 3 -2 1 1 2 8058 work1 a 51 77-11:29:32 /apps/sas 15336 work2 b 51 100-12:23:49 /local/hotfix Alternative You could use awk to do this: $ awk 'NR==FNR{_[$3]=$3;ne...
Join command gives incorrect output?
1,350,877,526,000
I have two files file 1 contains 2*J=0 EXP= 0.00000 2*J=4 EXP= 1.27911 2*J=8 EXP= 1.57613 2*J=12 EXP= 1.69134 2*J=10 EXP= 2.72705 2*J=16 EXP= 4.55689 2*J=20 EXP= 5.62138 file 2 contains 2*J=0 EXC= 0.00000 2*J=8 EXC= 1.21836 2*J=4 EXC= 1.59642 2*J=12 EXC= 1.78359 2*J=10 EXC= 2.69484 2*J=16 EXC= 7.24518 2*J=20 EXC= 7.3...
with GNU awk, we can control how the output is sorted: joiner.awk #!/usr/bin/env -S gawk -f FILENAME == ARGV[1] { f1[$1] = $2 OFS $3 sort_key[$1] = $3 next } { f2[$1] = $2 OFS $3 } function sorter(idx1, val1, idx2, val2) { return sort_key[idx1] - sort_key[idx2] } END { PROCINFO["sorted_in"]...
Join two files ordered numerically according to second column
1,350,877,526,000
I am trying to compare second columns of two text files and print the first columns of both files if match. I have tried the below awk codes but it was no use 1) awk 'NR==FNR {a[$2]=$2; next} {print $1,a[$1]}' nid8.txt nid9.txt 2) awk 'NR==FNR {a[$2]=$2; next} {print $1, $1 in a}' nid8.txt nid9.txt Example files: ni...
You can use join here: join -j 2 -o 1.1 2.1 <(sort -nk2,2 nid8.txt) <(sort -nk2,2 nid9.txt) Use the second field -j 2 on both files as the keys. and -output these fields: first field from the first file 1.1 first field from the second file 2.1 join requires input files to be sorted, so we sort them on the second fiel...
Compare second column of two text files and print first columns of both files if match
1,350,877,526,000
I would like to perform script below but without creation of intermediate files (lsfs.out, df.out) on IBM AIX with ksh lsfs_out=`lsfs | sed -n '1d;p' | sort -b -k 3` df_out=`df -k | sed -n '1d;p' | sort -b -k 7` echo "$lsfs_out" > lsfs.out echo "$df_out" > df.out join -1 7 -2 3 df.out lsfs.out The output of the first...
an awk solution (can be onelined of course) (df -k ; lsfs ) | awk 'FNR==1 {next; } NF==7 { L[$7]=$0 ; next ; } { printf "%s %s\n",$0,L[$3];}' where FNR==1 {next; } filter header NF==7 { L[$7]=$0 ; next ; } store df line indexed on filesystem { printf "%s %s\n",$0,L[$3];} print lsfs line and join with df's ...
How to join results of two commands in IBM AIX ksh
1,350,877,526,000
I can't seem to get either grep or awk to do a relatively simple index pull of a list. I suspect it's because of adjacent duplicates in the index file, something I wouldn't have thought would cause an issue. Oddly looking for a solution online wasn't successful as all the queries I found are people who want to remove ...
I don't think you can do this with grep, no, but you can in awk. The simplest approach I can think of is to store the contents of searchfile.txt in memory and then print its lines each time you see an index: $ awk -F'\t' 'NR==FNR{c[$1]=$0;next}{if(c[$1]){print c[$1]}}' searchfile.txt index.txt n0000003 216 -0.334 ...
Is it possible to use grep or awk to report duplicate output lines corresponding to repeating entries in an index file?
1,350,877,526,000
I would like to join data from two CSV files based on matching column information. The data to match is from File1.csv column 5, and File2 column 1, and i want to append the information from File2 column 2 upon match, and if no match leave empty double quotes. File1.csv "Z","P","W","K","1","1.18.24.59" "S","K","D","X"...
Here's one solution, using awk. Tested on GNU awk 4.1.3. $ awk -F, 'NR==FNR{a[$1]=$2}NR!=FNR{print $0","(a[$6]?a[$6]:"\"\"")}' file2.csv file1.csv "Z","P","W","K","1","1.18.24.59","23 25 41" "S","K","D","X","9","1.14.19.238","8827 145 8291" "R","M","P","Y","8","1.15.11.21","98 77 8291" "B","D","0","U","5","1.9.20.159...
Join two CSV files based on matching column data
1,542,262,746,000
I have two csv files: file1: C1, 1, 0, 1, 0, 1 C2, 1, 0, 1, 1, 0 C3, 0, 0, 1, 1, 0 file2: C3, 1.2 C1, 2.3 C2, 1.8 I want to merge these two files based on C column (which produces): C1, 1, 0, 1, 0, 1, 2.3 C2, 1, 0, 1, 1, 0, 1.8 C3, 0, 0, 1, 1, 0, 1.2 And then remove the second last column (to produce): C1, 1, 0, 1...
You just have create a hash-map on the second file on the C column and use that on the first file as below. The actions right next FNR==NR applies to the first file specified at the end and the subsequent action happens on the last file. This is because of the special variables in awk, FNR and NR which track line numb...
Joining two csv files on common column and removing the second last column
1,542,262,746,000
From coreutils' manual about join -e string Replace those output fields that are missing in the input with string. I.e., missing fields specified with the -12jo options. I don't understand the option at all. What do the following mean "those output fields that are missing in the input" "missing fields specified ...
The slightly cryptic string -12jo refers to the four separate options -1, -2, -j and -o, of which the first three has to do with selecting what field in each file to join on and the last has to do with what fields from each file should be outputted. The -j option is an extension in GNU join and and -j n is the same as...
What does `join -e` mean?
1,542,262,746,000
I'm trying to identify all lines in common based on the first column of one file. I'm using the following command: awk '{print $1}' File1 | fgrep -wf - File2 >Out File1: M01605:153:000000000-B55NK:1:1101:10003:14536 chr1 150129998 A Rev 18 M01605:153:000000000-B55NK:1:1101:10007:14573 chr17 44166311 C...
That's exactly what the join command is made for: it joins two files based on a common field: $ awk '{print $1}' File1 | join - File2 M01605:153:000000000-B55NK:1:1101:10003:14536 2:N:0:1 GTTTGCGCCGATGTA M01605:153:000000000-B55NK:1:1101:10007:14573 2:N:0:1 GGGGATAAGCGTTGC M01605:153:000000000-B55NK:1:1101:10007:14...
Grep not returning identical matches from awk pipe
1,542,262,746,000
I have two files each containing a timestamp and a count as follows File1.txt 9 2016-06-22 3 2016-06-23 2 2016-06-24 1 2016-06-25 2 2016-06-26 2 2016-06-27 File2.txt 3 2016-06-23 2 2016-06-25 5 2016-06-27 I would like to created an output where it uses the date column (col-3) in both the f...
The best solution is to use the join command: join -j 2 -a 1 -e " " -o 1.1 2.1 1.2 File1.txt File2.txt Not the more elegant solution, but if you want to learn shell-script this should do the job: while read line1; do file1_number=$(echo ${line1} | cut -d ' ' -f 1) file1_date=$(echo ${line1} | cut -d ' ' -f 2)...
Joining two file data based on column comparision
1,542,262,746,000
file1: 0000002|SLM DEV CORP |PO 857 0000003|S TOPPING |APT 19 0000004|JD BROS LTD |PO 118 0000005|ZKZ SERVICES |14699 CREDITV...
Simple with awk, if the join field is unique: awk -F"|" 'a[$1]++' file1 file2 -F"|" sets pipe as delimiter a[$1]++ is a condition. When the condition is true, the line is printed. The condition becomes true, when the first field $1 appears more than one times. If the join field is not unique: awk -F"|" 'a[$1]++&&F...
How to join two files by matching a column with an irregular number of columns?
1,542,262,746,000
Is there an easy way to print all lines of a file1 (so that the output has the same number of lines as the input file1) but print a message such as NoMatch where the first entries of file1 does not match the first entries of file2? file1: Entry1 Entry2 a 2 b 3 c 4 d 5 file2: a b b a d d Desired output: Entry1 a 2 ...
With awk read file2 first and save $1 in seen[$1] then read file1 and if $1 wasn't "seen" replace it with NoMatch: awk 'FNR==NR{seen[$1]++; next} {(FNR==1 || ($1 in seen)) || $1="NoMatch"};1' file2 file1 If you prefer join, you need sorted input. You'll have to extract the header from file1 first, sort the remaining ...
How do I print all records in file 1 but note the records that do not match file2?
1,542,262,746,000
I have a tabular file like this which is my index: a X001 a X002 a X003 b X002 c X006 z X007 z X008 z X001 I want to search the following single column file against that index and return each match from the second column of the index. a b z So the output from that search would be this: X001 X002 X003...
With join: join -o1.2 <(sort -k1n index) <(sort -k1n file) This will merge the two files at field number 1 and if they match, print the second field of the first file 1.2. Or with awk: awk 'FNR==NR{a[$1]=1} FNR!=NR&&a[$1]{print $2}' file index The file file is loaded into an array a. When the second file index in...
Find multiple matches in a tabular file and print second column?
1,542,262,746,000
I have 3 csv files I want to join by first column (id column) Each file has the same 3 columns. Row example : id | timestamp | Name 3792318, 2014-07-15 00:00:00, "A, B" When I join the 3 csv files with join -t, <(join -t, csv1 csv2) csv3 > out.csv The out.csv file doesn't have the same number of col...
Obviously, using a csv parser would be better but if we can safely assume that The 1st field will never contain a comma; You only want the ids that are present in the 1st file (if an id is in file2 or file3 and not in file1 of you ignore it); The files are small enough to fit in your RAM. Then this Perl approach sho...
Merge CSV files with field delimiters also occuring inside quotes