Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6028519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:45:07+00:00 2026-05-23T04:45:07+00:00

Using any combination of Linux tools (without going into any full featured programming language)

  • 0

Using any combination of Linux tools (without going into any full featured programming language) how can I sort this list

A,C 1
C,B 2
B,A 3

into

A,B 3
A,C 1
B,C 2
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:45:07+00:00Added an answer on May 23, 2026 at 4:45 am

    In case somebody is interested. I was not realy satisfied with any suggestions. Probably because I hoped for view lines solution and such doesn’t exist as far as I know.
    Anyway I did wrote an utility, called ljoin (for left join like in databases) which does exactly what I was asking for (of course :D)

    #!/usr/bin/perl
    =head1 NAME
    
    ljoin.pl - Utility to left join files by specified key column(s)
    
    =head1 SYNOPSIS
    
    ljoin.pl [OPTIONS] <INFILE1>..<INFILEN> <OUTFILE>
    
    To successfully join rows one must suply at least one input file and exactly one output file. Input files can be real file names or a patern, like [ABC].txt or *.in etc.
    
    
    =head1 DESCRIPTION
    
    This utility merges multiple file into one using specified column as a key
    
    =head2 OPTIONS
    
    =item --field-separator=<separator>, -fs <separator>
    
    Specifies what string should be used to separate columns in plain file. Default value for this option is tab symbol.
    
    =item --no-sort-fields, -no-sf
    
    Do not sort columns when creating a key for merging files
    
    =item --complex-key-separator=<separator>, -ks <separator>
    
    Specifies what string should be used to separate multiple values in multikey column. For example "A B" in one file can be presented as "B A" meaning that this application should somehow understand that this is the same key. Default value for this option is space symbol.
    
    =item --no-sort-complex-keys, -no-sk
    
    Do not sort complex column values when creating a key for merging files
    
    =item --include-primary-field, -i
    
    Specifies whether key which is used to find matching lines in multiple files should be included in the output file. First column in output file will be the key in any case, but in case of complex column the value of first column will be sorted. Default value for this option is false.
    
    =item --primary-field-index=<index>, -f <index>
    
    Specifies index of the column which should be used for matching lines.  You can use multiple instances of this option to specify a multi-column key made of more than one column like this "-f 0 -f 1"
    
    =item --help, -?
    
    Get help and documentation
    
    =cut
    
    
    use strict;
    use warnings;
    use Getopt::Long;
    use Pod::Usage;
    
    my $fieldSeparator = "\t";
    my $complexKeySeparator = " ";
    my $includePrimaryField = 0;
    my $containsTitles = 0;
    my $sortFields = 1;
    my $sortComplexKeys = 1;
    my @primaryFieldIndexes;
    
    GetOptions(
        "field-separator|fs=s" => \$fieldSeparator,
        "sort-fields|sf!" => \$sortFields,
        "complex-key-separator|ks=s" => \$complexKeySeparator,
        "sort-complex-keys|sk!" => \$sortComplexKeys,
        "contains-titles|t!" => \$containsTitles,
        "include-primary-field|i!" => \$includePrimaryField,
        "primary-field-index|f=i@" => \@primaryFieldIndexes,
        "help|?!" => sub { pod2usage(0) }
    ) or pod2usage(2);
    
    pod2usage(0) if $#ARGV < 1;
    
    push @primaryFieldIndexes, 0 if $#primaryFieldIndexes < 0;
    
    my %primaryFieldIndexesHash;
    for(my $i = 0; $i <= $#primaryFieldIndexes; $i++)
    {
        $primaryFieldIndexesHash{$i} = 1;
    }
    
    print "fieldSeparator = $fieldSeparator\n";
    print "complexKeySeparator = $complexKeySeparator \n";
    print "includePrimaryField = $includePrimaryField\n";
    print "containsTitles = $containsTitles\n";
    print "primaryFieldIndexes = @primaryFieldIndexes\n";
    print "sortFields = $sortFields\n";
    print "sortComplexKeys = $sortComplexKeys\n";
    
    my $fieldsCount = 0;
    my %keys_hash = ();
    my %files = ();
    my %titles = ();
    
    
    # Read columns into a memory
    foreach my $argnum (0 .. ($#ARGV - 1)) 
    {
        # Find files with specified pattern
        my $filePattern = $ARGV[$argnum];
        my @matchedFiles = < $filePattern >;
        foreach my $inputPath (@matchedFiles) 
        {
            open INPUT_FILE, $inputPath or die $!;
    
            my %lines;
            my $lineNumber = -1;
            while (my $line = <INPUT_FILE>) 
            {
                next if $containsTitles && $lineNumber == 0;
    
                # Don't use chomp line. It doesn't handle unix input files on windows and vice versa
                $line =~ s/[\r\n]+$//g;
    
                # Skip lines that don't have columns
                next if $line !~ m/($fieldSeparator)/;
    
                # Split fields and count them (store maximum number of columns in files for later use)
                my @fields = split($fieldSeparator, $line);
                $fieldsCount = $#fields+1 if $#fields+1 > $fieldsCount;
    
                # Sort complex key
                my @multipleKey;
                for(my $i = 0; $i <= $#primaryFieldIndexes; $i++)
                {
                    my @complexKey = split ($complexKeySeparator, $fields[$primaryFieldIndexes[$i]]);
                    @complexKey = sort(@complexKey) if $sortFields;
                    push @multipleKey, join($complexKeySeparator, @complexKey)
                }
    
                # sort multiple keys and create key string
                @multipleKey = sort(@multipleKey) if $sortFields;
                my $fullKey = join $fieldSeparator, @multipleKey;
    
                $lines{$fullKey} = \@fields;
                $keys_hash{$fullKey} = 1;
            }
            close INPUT_FILE;
    
            $files{$inputPath} = \%lines;
        }
    }
    
    # Open output file
    my $outputPath = $ARGV[$#ARGV];
    open OUTPUT_FILE, ">" . $outputPath or die $!;
    my @keys = sort keys(%keys_hash); 
    
    # Leave blank places for key columns
    for(my $pf = 0; $pf <= $#primaryFieldIndexes; $pf++)
    {
        print OUTPUT_FILE $fieldSeparator;
    }
    
    # Print column headers
    foreach my $argnum (0 .. ($#ARGV - 1)) 
    {
        my $filePattern = $ARGV[$argnum];
        my @matchedFiles = < $filePattern >;
        foreach my $inputPath (@matchedFiles) 
        {
            print OUTPUT_FILE $inputPath;
    
            for(my $f = 0; $f < $fieldsCount - $#primaryFieldIndexes - 1; $f++)
            {
                print OUTPUT_FILE $fieldSeparator;
            }
        }
    }
    
    # Print merged columns
    print OUTPUT_FILE "\n";
    foreach my $key ( @keys )
    {
        print OUTPUT_FILE $key;
    
        foreach my $argnum (0 .. ($#ARGV - 1)) 
        {
            my $filePattern = $ARGV[$argnum];
            my @matchedFiles = < $filePattern >;
            foreach my $inputPath (@matchedFiles) 
            {
                my $lines = $files{$inputPath};
    
                for(my $i = 0; $i < $fieldsCount; $i++)
                {
                    next if exists $primaryFieldIndexesHash{$i} && !$includePrimaryField;
                    print OUTPUT_FILE $fieldSeparator;
                    print OUTPUT_FILE $lines->{$key}->[$i] if exists $lines->{$key}->[$i];
                }
            }
        }
    
        print OUTPUT_FILE "\n";
    }
    close OUTPUT_FILE;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Without using any third party program to do this (i.e. without VMware ThinApp, U3
Using any member of the Windows Server family, I can set up an active
Using any tools which you would expect to find on a nix system (in
Okay so I'm not using any session variables, rather my code looks like this:
I have compiled a .NET application using Any CPU option. This .NET application uses
how to swap two numbers inplace without using any additional space?
How to check visited link using jquery without using any plugin please help to
I would like to clone a tag using Javascript (without using any external frameworks
I'm using boost::any in combination with boost::any_cast<> to write some framework code which should
Can anyone recommend any tools for compile and runtime analysis of C++ code? I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.