10-19-2013
This is a much harder problem that it appears at first glance.
The sort solution proposed by danmero should just give one line sort each set of lines with identical values in the 1st 2 fields, but the one printed depends on the sort order of the remaining fields. The order ginkrf requested was that the last line in the (unsorted) file be printed for each set of lines with identical values in the 1st two fields.
The awk solution proposed by mjf will print the 1st line of each matching set instead of the last line of each matching set. (And, if the 1st 2 fields when concatenated yield the same key even though the fields are different, some desired output lines may be skipped. For example if $1 is "ab" and $2 is "c" in one record and "a" and "bc" in another, they will both have key "abc".)
Since ginkrf didn't say whether the order of the lines in the output has to match the order in which they appeared in the input, I won't try to guess at an efficient way to do what has been requested. If the order is important, the input file could be reversed, fed through mfj's awk script (with !x[$1 $2]++ changed to !x[$1,$2]++), and then reverse the order of the output again. Depending on the output constraints this might or might not be grossly inefficient.
If the output order is not important, it could be done easily with an awk script, but could require almost 400mb of virtual address space to process 20 million 20 byte records.
With a better description of the input (is there anything in a record other than its position in the input that can be used to determine which of several lines with the 1st two fields matching should be printed) and the output constraints, we might be able to provide a better solution. Are there ever more than two lines with the same 1st two fields? If yes, out of the 20 million input records, how many output records do you expect to be produced? Are there likely to be lots of lines that only have one occurrence of the 1st two fields? What are the file sizes (input and output) in bytes (instead of records)? What is the longest input line in bytes?
What OS and hardware are you using? How much memory? How much swap space?
Last edited by Don Cragun; 10-20-2013 at 10:31 AM..
Reason: Fix explanation of the possible failure of mjf's awk proposal.
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
:)Hi Friends,
I have certain log files extracted. I want it to be converted in pipe ( | ) delimited file. How do i do it?
E.g.
Account Balance : 123456789 Rs O/P (Account Balance: | 123456789 Rs)
Account Balance (Last) > 987654321 Rs O/P (Account Balance (Last) | 987654321 Rs)
Last... (5 Replies)
Discussion started by: anushree.a
5 Replies
2. Shell Programming and Scripting
i have a file whose data is like this::
osr_pe_assign|-120|wg000d@att.com|4|
osr_evt|-21|wg000d@att.com|4|
pe_avail|-21|wg000d@att.com|4|
osr_svt|-11|wg000d@att.com|4|
pe_mop|-13|wg000d@att.com|4|
instar_ready|-35|wg000d@att.com|4|
nsdnet_ready|-90|wg000d@att.com|4|... (6 Replies)
Discussion started by: priyanka3006
6 Replies
3. Shell Programming and Scripting
This is my input file with extra information in the HEADER and leading & trailing SPACES between PIPE delimiter.
02/04/2010 Dynamic List Display 1
--------------------------------------------------------------------------------------... (6 Replies)
Discussion started by: srimitta
6 Replies
4. Shell Programming and Scripting
Hi All,
I have space delimited file similar to the one as shown below.. I need to convert it as a pipe delimited, the values inside the pipe delimited file should be as highlighted...
AA ATIU2345098809 009697 005374
BB ATIU2345097809 005445 006518
CC ATIU9685098809 003215 003571
DD... (7 Replies)
Discussion started by: nithins007
7 Replies
5. Shell Programming and Scripting
I have a file which was pipe delimited, I need to make it tab delimited. I tried with sed but no use
cat file | sed 's/|//t/g'
The above command substituted "/t" not tab in the place of pipe.
Sample file:
abc|123|2012-01-30|2012-04-28|xyz
have to convert to:
abc 123... (6 Replies)
Discussion started by: karumudi7
6 Replies
6. Shell Programming and Scripting
I have file as below
column1|column2|column3|column4|column5|
fill1|fill2|fill3|fill4|fill5|
abc1|abc2|abc3|abc4|abc5|
.
.
.
.
i need to remove column2,3, from that file
column1|column4|column5|
fill1|fill4|fill5|
abc1|abc4|abc5|
.
.
. (3 Replies)
Discussion started by: greenworld123
3 Replies
7. Shell Programming and Scripting
Hi guys,
I need to know how i can ignore Pipe '|' if Pipe is coming as a column in Pipe delimited file
for eg:
file 1:
xx|yy|"xyz|zzz"|zzz|12...
using below awk command
awk 'BEGIN {FS=OFS="|" } print $3
i would get xyz
But i want as :
xyz|zzz to consider as whole column... (13 Replies)
Discussion started by: rohit_shinez
13 Replies
8. Shell Programming and Scripting
Hi guys,Got a bit of a bind I'm in. I'm looking to remove duplicates from a pipe delimited file, but do so based on 2 columns. Sounds easy enough, but here's the kicker...
Column #1 is a simple ID, which is used to identify the duplicate.
Once dups are identified, I need to only keep the one... (2 Replies)
Discussion started by: kevinprood
2 Replies
9. UNIX for Dummies Questions & Answers
Hi, I have a rquirement in unix as below .
I have a text file with me seperated by | symbol and i need to generate a excel file through unix commands/script so that each value will go to each column.
ex:
Input Text file:
1|A|apple
2|B|bottle
excel file to be generated as output as... (9 Replies)
Discussion started by: raja kakitapall
9 Replies
10. Shell Programming and Scripting
I have a file as below
Emp1|FirstName|MiddleName|LastName|Address|Pincode|PhoneNumber
1234|FirstName1|MiddleName2|LastName3| Add1 || ADD2|123|000000000
2345|FirstName2|MiddleName3|LastName4|
Add1 || ADD2|
234|000000000
OUTPUT :
... (1 Reply)
Discussion started by: styris
1 Replies
join(1) General Commands Manual join(1)
Name
join - join files
Syntax
join [ -a n] [ -e string] [ -j n m] [ -o list] [ -t c] file1 file2
Description
The command compares a field in file1 to a field in file2. If the two fields match, the command combines the line in file1 that contains
the field with the line in file2 that contains the field. The command writes its output to standard output. If you specify a hyphen (-)
in the file1 argument, compares standard input to the contents of file2.
The command compares and combines the input files one line at a time. Each line in the input file contains one field that uses to determine
if two lines should be joined. This field is called the join field. By default, the command uses the first field in each line as the join
field. The command compares the join field in the first line of file1 to the join field in the first line of file2. If the two fields
match, the command joins the lines. The command then compares the join fields in the second line of both files, and so on.
In the input files, fields are separated by tab or space characters. The command reads data from the first field until it encounters a tab
or space character, which terminates the first field. By default, the command ignores tab and space characters, so the next character
that is not a tab or space begins the second field. The second field is terminated by the tab or space that follows it, and the third
field begins with the next character that is not a tab or space. The command reads fields in this way until it encounters a new line char-
acter. Any number of tabs or spaces can separate two fields, and any number of newline characters can separate two lines.
Both file1 and file2 must be ordered in the collating sequence of the command on the fields that the two files are to be joined. By
default, uses the first field in each line and collates the same as
To create output, the command writes the join field, followed by the remaining fields in the line from file1, followed by the remaining
fields in the line from file2 to the output file. The following demonstrates how lines in the output appear by default:
join_field file1.field2 file1.field3 file1.field4 file2.field2 file2.field3
By default, the command ignores lines that do not contain identical join fields. The command writes no output for these lines.
You can change how creates output using command options. For example, you can cause the command to write output for lines that do not con-
tain identical join fields. You can also specify a list using the option. In list, you supply a list of specifiers in the form
file.field, where file is either 1 or 2 and field is the number of the field. For example, 1.2 specifies the second field in the first
file and 2.4 specifies the fourth field in the second file. The following demonstrates how lines in the output appear if you use these two
specifiers:
file1.field2 field2.field4
International Environment
LC_COLLATE If this environment variable is set and valid, uses the international language database named in the definition to determine
collation rules.
LC_CTYPE If this environment variable is set and valid, uses the international language database named in the definition to determine
character classification rules.
LANG If this environment variable is set and valid uses the international language database named in the definition to determine
collation and character classification rules. If LC_COLLATE or LC_CTYPE is defined their definition supercedes the defini-
tion of LANG.
Options
-a[n] Write lines that contain unmatched join fields to the output file. You can cause the command to write unmatched lines from
only one file using n. If you specify 1 in n, writes unmatched lines only from file 1. If you specify 2, writes unmatched
lines only from file 2.
If you omit the option, writes no output for unmatched lines.
-e s Writes the string you specify in s to the output if you specify a nonexistent field in the list for the option. For example,
if lines in file 2 contain only three fields, and you specify 2.4 in list, writes s in place of the nonexistent field.
-jn m Defines field m in file n to be the join field. The command compares the field you specify in the option to the default join
field in the other file. If you omit n, the command uses the mth field in both files.
-1 m Use the m th field in the first file as the join field. This option is equivalent to using m.
-2 m Use the m field in the second file as the join field. This option is equivalent to using m.
-o list Output the joined data according to list. The specifiers in list have the format file.field, where file is either 1 or 2 and
field is the number of the field.
-tc Recognize the tab character c. The presence of c in a line is significant, both for comparing join fields and creating output.
Restrictions
If you specify the option, the command collates the same as with no options.
Examples
Suppose that by issuing the following commands, you display the files shown in the example:
% cat file_1
apr 15
aug 20
dec 18
feb 05
% cat file_2
apr 06
aug 14
date
feb 15
Both files are sorted in ascending order.
If you issue the command without options, the output appears as follows:
% join file_1 file_2
apr 15 06
aug 20 14
feb 05 15
The third line in each input file is not joined in the output because the join fields (date and dec) do not match.
To join the lines in these files and format the output so that the second field from each file appears first and the first (join) field
appears second, issue the following command:
% join -o 1.2 1.1 2.2 2.1 file_1 file_2
15 apr 06 apr
20 aug 14 aug
05 feb 15 feb
To write lines that are unmatched to the output, issue the following command:
% join -a file_1 file_2
apr 15 06
aug 20 14
date
dec 18
feb 05 15
See Also
awk(1), comm(1), sort(1), sort5(1), environ(5int)
join(1)