help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] files


From: Bob Proulx
Subject: Re: [Help-bash] files
Date: Wed, 20 Apr 2016 22:50:59 -0600
User-agent: Mutt/1.5.24 (2015-08-30)

Val Krem wrote:
> I have two files the first file is pipe delimited  and the other
> file is space delimited. I want to combine the two files by the
> first column and the final result should be pipe delimited file
> 
> file 1
> 
> A123|24|315
> A125|63|450
> 
> file 2
> A123 009 163
> A125 091 112
> 
> i want the result
> A123|24|315|009|163
> A125|63|450|091|112 

This works.

  join -t'|' file1 <(sed 's/ /|/g' file2)
    A123|24|315|009|163
    A125|63|450|091|112

The <(...) is the process substitution syntax.  It creates a named
pipe on the fly and runs the sub-command with the output attached to
it.  This way it looks like a file but the file has been preprocessed.
Since you said one was space this converts it to a '|' on the fly and
then uses the result.

Note that the files must be sorted.  If they aren't then simply sort
in the sub processes.

  join -t'|'  <(sort file1) <(sed 's/ /|/g' file2 | sort)

Bob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]