How do I merge the contents of two files ...

  • Follow


Hi,

I'm using bash on Mac 10.6.3.  I have two files, each having the same
number of lines.  How do I merge the files such that the resulting
file has a line format of

line_x_of_file_1,line_x_of_file2

?  Basically, I want to concatenate the lines of each file into a new
file.  Thanks, - Dave
0
Reply Dave 2/24/2011 5:45:15 PM

On Thu, 24 Feb 2011 09:45:15 -0800 (PST)
Dave <laredotornado@zipmail.com> wrote:

> Hi,
> 
> I'm using bash on Mac 10.6.3.  I have two files, each having the same
> number of lines.  How do I merge the files such that the resulting
> file has a line format of
> 
> line_x_of_file_1,line_x_of_file2
> 
> ?  Basically, I want to concatenate the lines of each file into a new
> file.  Thanks, - Dave

man paste
0
Reply pk 2/24/2011 5:32:22 PM


Dave wrote:

> I'm using bash on Mac 10.6.3.  I have two files, each having the same
> number of lines.  How do I merge the files such that the resulting
> file has a line format of
> 
> line_x_of_file_1,line_x_of_file2
> 
> ?  Basically, I want to concatenate the lines of each file into a new
> file.  Thanks, - Dave

$ apropos merge
…
paste (1)            - merge lines of files
…

-- 
PointedEars
0
Reply Thomas 2/24/2011 7:17:45 PM

On Feb 24, 11:32=A0am, pk <p...@pk.invalid> wrote:
> On Thu, 24 Feb 2011 09:45:15 -0800 (PST)
>
> Dave <laredotorn...@zipmail.com> wrote:
> > Hi,
>
> > I'm using bash on Mac 10.6.3. =A0I have two files, each having the same
> > number of lines. =A0How do I merge the files such that the resulting
> > file has a line format of
>
> > line_x_of_file_1,line_x_of_file2
>
> > ? =A0Basically, I want to concatenate the lines of each file into a new
> > file. =A0Thanks, - Dave
>
> man paste

With regards to paste, how do I make the delimiter more than just a
single character?  This command only uses the first character ...

paste -d "&url=3D" file1 file2

Thanks, - Dave
0
Reply Dave 2/24/2011 8:04:41 PM

2011-02-24, 12:04(-08), Dave:
> On Feb 24, 11:32 am, pk <p...@pk.invalid> wrote:
>> On Thu, 24 Feb 2011 09:45:15 -0800 (PST)
>>
>> Dave <laredotorn...@zipmail.com> wrote:
>> > Hi,
>>
>> > I'm using bash on Mac 10.6.3.  I have two files, each having the same
>> > number of lines.  How do I merge the files such that the resulting
>> > file has a line format of
>>
>> > line_x_of_file_1,line_x_of_file2
>>
>> > ?  Basically, I want to concatenate the lines of each file into a new
>> > file.  Thanks, - Dave
>>
>> man paste
>
> With regards to paste, how do I make the delimiter more than just a
> single character?  This command only uses the first character ...
>
> paste -d "&url=" file1 file2
[...]

You'd need to write it:

paste -d '&url=' file1 /dev/null /dev/null /dev/null /dev/null file2

Or you could do:

awk '
  BEGIN{
    OFS="&url="
    while (1) {
      n=0
      for (i=1; i < ARGC; i++)
        if ((getline $i < ARGV[i]) > 0) n++
      if (!n) exit
      print
    }
  }' file1 file2

-- 
Stephane
0
Reply Stephane 2/24/2011 8:21:55 PM

2011-02-24, 20:21(+00), Stephane CHAZELAS:
[...]
> Or you could do:
>
> awk '
>   BEGIN{
>     OFS="&url="
>     while (1) {
>       n=0
>       for (i=1; i < ARGC; i++)
>         if ((getline $i < ARGV[i]) > 0) n++

Sorry that should be:

          if ((getline $i < ARGV[i]) > 0) n++; else $i=""

to get a similar behavior to paste(1) when files don't have the
same number of lines.

>       if (!n) exit
>       print
>     }
>   }' file1 file2
>

-- 
Stephane
0
Reply Stephane 2/24/2011 8:27:13 PM

Dave <laredotornado@zipmail.com> writes:
> On Feb 24, 11:32 am, pk <p...@pk.invalid> wrote:
>> On Thu, 24 Feb 2011 09:45:15 -0800 (PST)
>> Dave <laredotorn...@zipmail.com> wrote:
>> > I'm using bash on Mac 10.6.3.  I have two files, each having the same
>> > number of lines.  How do I merge the files such that the resulting
>> > file has a line format of
>>
>> > line_x_of_file_1,line_x_of_file2
>>
>> > ?  Basically, I want to concatenate the lines of each file into a new
>> > file.  Thanks, - Dave
>>
>> man paste
>
> With regards to paste, how do I make the delimiter more than just a
> single character?  This command only uses the first character ...
>
> paste -d "&url=" file1 file2

Use a character that doesn't appear in either file, then pipe the
result through sed to replace the character with "&url=".

For example:

paste -d ';' file1 file2 | sed 's/;/\&url=/'

(The '\' is needed because of sed's special treatment of '&'.)

If picking an appropriate character is too difficult (say, because
the input files can contain arbitary data), I'd probably write a
small Perl script to do the job.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply Keith 2/24/2011 8:56:45 PM

On 2/24/2011 2:04 PM, Dave wrote:
> On Feb 24, 11:32 am, pk<p...@pk.invalid>  wrote:
>> On Thu, 24 Feb 2011 09:45:15 -0800 (PST)
>>
>> Dave<laredotorn...@zipmail.com>  wrote:
>>> Hi,
>>
>>> I'm using bash on Mac 10.6.3.  I have two files, each having the same
>>> number of lines.  How do I merge the files such that the resulting
>>> file has a line format of
>>
>>> line_x_of_file_1,line_x_of_file2
>>
>>> ?  Basically, I want to concatenate the lines of each file into a new
>>> file.  Thanks, - Dave
>>
>> man paste
>
> With regards to paste, how do I make the delimiter more than just a
> single character?  This command only uses the first character ...
>
> paste -d "&url=" file1 file2
>
> Thanks, - Dave

awk 'NR==FNR{f1[FNR]=$0; next} {print f1[FNR] "&url=" $0}' file1 file2

Regards,

	Ed.
0
Reply Ed 2/24/2011 9:45:38 PM

On 24.02.2011 18:45, Dave wrote:
> Hi,
> 
> I'm using bash on Mac 10.6.3.  I have two files, each having the same
> number of lines.  How do I merge the files such that the resulting
> file has a line format of
> 
> line_x_of_file_1,line_x_of_file2
> 
> ?  Basically, I want to concatenate the lines of each file into a new
> file.  Thanks, - Dave

This is one possibility purely in shell (using ksh's read -u)...

    exec 3< file1
    exec 4< file2
    while read -u3 left && read -u4 right
    do
        printf "%s&url=%s\n" "$left" "$right"
    done

....but a quick test showed that bash seems to support  read -u  as well.


And as a bonus here's a variation of Keith's suggestion...

  paste -d $'\n' file1 file2 | awk 'ORS=NR%2?"&url=":RS'


Janis
0
Reply Janis 2/24/2011 9:57:55 PM

Janis Papanagnou wrote:

> This is one possibility purely in shell (using ksh's read -u)...
>
>     exec 3< file1
>     exec 4< file2
>     while read -u3 left && read -u4 right
>     do
>         printf "%s&url=%s\n" "$left" "$right"
>     done

To handle whitespace and backslash characters correctly you need:

     while IFS= read -r -u3 left && IFS= read -r -u4 right

> ...but a quick test showed that bash seems to support  read -u  as well.

In shells that don't support it you can use:

     while IFS= read -r left <&3 && IFS= read -r right <&4

-- 
Geoff Clare <netnews@gclare.org.uk>

0
Reply Geoff 2/25/2011 1:52:10 PM

Beside paste, Mac also comes with lam:

    lam file1 -s, file2
0
Reply Hai 3/26/2011 9:31:42 PM

10 Replies
1972 Views

(page loaded in 0.164 seconds)

Similiar Articles:













7/23/2012 5:50:48 AM


Reply: