summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAssaf Gordon <assafgordon@gmail.com>2009-11-20 15:24:07 +0000
committerPádraig Brady <P@draigBrady.com>2010-02-01 13:57:42 +0000
commit819aa9eba741c36bb522cbc2c7f10e24d190f945 (patch)
tree86bafe4a6dbe2dbc2ca813bbb349fe49caaf38cb /src
parent86914603b58b666e4c0f8442ba46e89190f381e2 (diff)
downloadcoreutils-819aa9eba741c36bb522cbc2c7f10e24d190f945.tar.xz
join: add --header option to always output the first line
This essentially allows one to use --check-order with headings. Note join without --check-order will already handle the common case where headings do match in each file, however using --check-order will fail often when the header sorts after the first line of data. Note also that this will join header lines from each file even if they don't match, with headings from the first file being used. * NEWS: Mention the new option. * doc/coreutils.texi (join invocation): Describe the new option. * src/join.c (usage): Likewise. (join): Join the header lines unconditionally. * tests/misc/join: Add 5 new tests.
Diffstat (limited to 'src')
-rw-r--r--src/join.c23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/join.c b/src/join.c
index d86e62c03..6030a01b3 100644
--- a/src/join.c
+++ b/src/join.c
@@ -137,7 +137,8 @@ static enum
enum
{
CHECK_ORDER_OPTION = CHAR_MAX + 1,
- NOCHECK_ORDER_OPTION
+ NOCHECK_ORDER_OPTION,
+ HEADER_LINE_OPTION
};
@@ -146,6 +147,7 @@ static struct option const longopts[] =
{"ignore-case", no_argument, NULL, 'i'},
{"check-order", no_argument, NULL, CHECK_ORDER_OPTION},
{"nocheck-order", no_argument, NULL, NOCHECK_ORDER_OPTION},
+ {"header", no_argument, NULL, HEADER_LINE_OPTION},
{GETOPT_HELP_OPTION_DECL},
{GETOPT_VERSION_OPTION_DECL},
{NULL, 0, NULL, 0}
@@ -157,6 +159,10 @@ static struct line uni_blank;
/* If nonzero, ignore case when comparing join fields. */
static bool ignore_case;
+/* If nonzero, treat the first line of each file as column headers -
+ join them without checking for ordering */
+static bool join_header_lines;
+
void
usage (int status)
{
@@ -191,6 +197,8 @@ by whitespace. When FILE1 or FILE2 (not both) is -, read standard input.\n\
--check-order check that the input is correctly sorted, even\n\
if all input lines are pairable\n\
--nocheck-order do not check that the input is correctly sorted\n\
+ --header treat first line in each file as field header line,\n\
+ print them without trying to pair them.\n\
"), stdout);
fputs (HELP_OPTION_DESCRIPTION, stdout);
fputs (VERSION_OPTION_DESCRIPTION, stdout);
@@ -614,6 +622,15 @@ join (FILE *fp1, FILE *fp2)
initseq (&seq2);
getseq (fp2, &seq2, 2);
+ if (join_header_lines && seq1.count && seq2.count)
+ {
+ prjoin(seq1.lines[0], seq2.lines[0]);
+ prevline[0] = NULL;
+ prevline[1] = NULL;
+ advance_seq (fp1, &seq1, true, 1);
+ advance_seq (fp2, &seq2, true, 2);
+ }
+
while (seq1.count && seq2.count)
{
size_t i;
@@ -1051,6 +1068,10 @@ main (int argc, char **argv)
&nfiles, &prev_optc_status, &optc_status);
break;
+ case HEADER_LINE_OPTION:
+ join_header_lines = true;
+ break;
+
case_GETOPT_HELP_CHAR;
case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);