merge two files line by line...

merge two files line by line...

am 08.11.2007 19:33:43 von CSGill

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

Re: merge two files line by line...

am 08.11.2007 19:45:54 von Janis Papanagnou

CSGill@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
>

Re: merge two files line by line...

am 08.11.2007 19:56:14 von Cyrus Kriticos

CSGill@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.

Re: merge two files line by line...

am 08.11.2007 20:14:52 von Stephane CHAZELAS

2007-11-08, 10:33(-08), CSGill@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

Re: merge two files line by line...

am 08.11.2007 20:41:41 von CSGill

On Nov 8, 12:45 pm, Janis Papanagnou
wrote:
> CSG...@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!

Re: merge two files line by line...

am 08.11.2007 22:27:27 von krahnj

CSGill@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( @x = `cat file2` ); $/ = "--\n" } print $_,
shift @x' file1



John
--
use Perl;
program
fulfillment

Re: merge two files line by line...

am 08.11.2007 23:30:37 von Tiago Peczenyj

On 8 nov, 16:33, CSG...@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