merge two files line by line...

I have 2 files:

file1:

x y z
a b c
--
x y z
d c f
--
x y z
t h y


file 2:

12:00
12:01
12:02


I want to merge these files such that the output looks like:

x y z
a b c
12:00
x y z
d c f
12:01
x y z
t h y
12:02

Is this easily possible?

Thanks in advance
CSGill [ Do, 08 November 2007 19:33 ] [ ID #1865324 ]

Re: merge two files line by line...

CSGill [at] gmail.com wrote:
> I have 2 files:
>
> file1:
>
> x y z
> a b c
> --
> x y z
> d c f
> --
> x y z
> t h y
>
>
> file 2:
>
> 12:00
> 12:01
> 12:02
>
>
> I want to merge these files such that the output looks like:
>
> x y z
> a b c
> 12:00
> x y z
> d c f
> 12:01
> x y z
> t h y
> 12:02
>
> Is this easily possible?

One possibility...

awk 'NR==FNR{a[NR]=$0;next}/^--/{$0=a[++nr]}1' file2 file1

If there's no '--' in the last line of file1 you may have to change it to...

awk 'NR==FNR{a[NR]=$0;next}/^--/{$0=a[++nr]}1
END{print a[++nr]}' file2 file1

(Or add a final '--' to file1 instead, to keep the awk program terse.)

Janis

>
> Thanks in advance
>
Janis Papanagnou [ Do, 08 November 2007 19:45 ] [ ID #1865325 ]

Re: merge two files line by line...

CSGill [at] gmail.com wrote:
> I have 2 files:
>
> file1:
>
> x y z
> a b c
> --
> x y z
> d c f
> --
> x y z
> t h y
here "--" too?

> file 2:
>
> 12:00
> 12:01
> 12:02
>
>
> I want to merge these files such that the output looks like:
>
> x y z
> a b c
> 12:00
> x y z
> d c f
> 12:01
> x y z
> t h y
> 12:02
>
> Is this easily possible?

[GNU sed]

$ sed '/--/R file2
/--/d' file1

--
Best regards | Be nice to America or they'll bring democracy to
Cyrus | your country.
Cyrus Kriticos [ Do, 08 November 2007 19:56 ] [ ID #1865327 ]

Re: merge two files line by line...

2007-11-08, 10:33(-08), CSGill [at] gmail.com:
> I have 2 files:
>
> file1:
>
> x y z
> a b c
> --
> x y z
> d c f
> --
> x y z
> t h y
>
>
> file 2:
>
> 12:00
> 12:01
> 12:02
>
>
> I want to merge these files such that the output looks like:
>
> x y z
> a b c
> 12:00
> x y z
> d c f
> 12:01
> x y z
> t h y
> 12:02
>
> Is this easily possible?
[...]

awk '$0 == "--" {getline < "file2"}
{print}
END {while ((getline < "file2") > 0) print}' file1

--
Stéphane
Stephane CHAZELAS [ Do, 08 November 2007 20:14 ] [ ID #1865328 ]

Re: merge two files line by line...

On Nov 8, 12:45 pm, Janis Papanagnou <Janis_Papanag... [at] hotmail.com>
wrote:
> CSG... [at] gmail.com wrote:
> > I have 2 files:
>
> > file1:
>
> > x y z
> > a b c
> > --
> > x y z
> > d c f
> > --
> > x y z
> > t h y
>
> > file 2:
>
> > 12:00
> > 12:01
> > 12:02
>
> > I want to merge these files such that the output looks like:
>
> > x y z
> > a b c
> > 12:00
> > x y z
> > d c f
> > 12:01
> > x y z
> > t h y
> > 12:02
>
> > Is this easily possible?
>
> One possibility...
>
> awk 'NR==FNR{a[NR]=$0;next}/^--/{$0=a[++nr]}1' file2 file1
>
> If there's no '--' in the last line of file1 you may have to change it to...
>
> awk 'NR==FNR{a[NR]=$0;next}/^--/{$0=a[++nr]}1
> END{print a[++nr]}' file2 file1
>
> (Or add a final '--' to file1 instead, to keep the awk program terse.)
>
> Janis
>
>
>
> > Thanks in advance

Thanks so much... works like a charm!
CSGill [ Do, 08 November 2007 20:41 ] [ ID #1865329 ]

Re: merge two files line by line...

CSGill [at] gmail.com wrote:
>
> I have 2 files:
>
> file1:
>
> x y z
> a b c
> --
> x y z
> d c f
> --
> x y z
> t h y
>
> file 2:
>
> 12:00
> 12:01
> 12:02
>
> I want to merge these files such that the output looks like:
>
> x y z
> a b c
> 12:00
> x y z
> d c f
> 12:01
> x y z
> t h y
> 12:02


perl -lne'BEGIN { chomp( [at] x = `cat file2` ); $/ = "--\n" } print $_,
shift [at] x' file1



John
--
use Perl;
program
fulfillment
krahnj [ Do, 08 November 2007 22:27 ] [ ID #1865331 ]

Re: merge two files line by line...

On 8 nov, 16:33, CSG... [at] gmail.com wrote:
> I have 2 files:
>
> file1:
>
> x y z
> a b c
> --
> x y z
> d c f
> --
> x y z
> t h y
>
> file 2:
>
> 12:00
> 12:01
> 12:02
>
> I want to merge these files such that the output looks like:
>
> x y z
> a b c
> 12:00
> x y z
> d c f
> 12:01
> x y z
> t h y
> 12:02
>
> Is this easily possible?
>
> Thanks in advance

Yeah... can be exotic this solution :

$ awk '/--/{print NR "s/" $0}' file1
3s/--
6s/--
9s/--

$ paste -d "/" <( awk '/--/{print NR "s/" $0}' file1 ) file2 /dev/null
3s/--/12:00/
6s/--/12:01/
9s/--/12:02/

$ paste -d "/" <( awk '/--/{print NR "s/" $0}' file1 ) file2 /dev/null
| sed -f - file1
x y z
a b c
12:00
x y z
d c f
12:01
x y z
t h y
12:02
Tiago Peczenyj [ Do, 08 November 2007 23:30 ] [ ID #1865334 ]
Linux » comp.unix.shell » merge two files line by line...

Vorheriges Thema: variable expansion
Nächstes Thema: Combine multiple line segment into one, when certain pattern is found - awk/sed/perl