Unix Shell - replacing lines in a file with lines from another file

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2007 > replacing lines in a file with lines from another file





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author replacing lines in a file with lines from another file
Elliot

2007-09-27, 7:36 pm

This should be an easy one for you smart UNIX people.

I have two files. I need to replace every 4th line in File A with the
corresponding line in File B. So when I vi my output file I want to see:

1 [Line 1 from File A]
2 [Line 2 from File A]
3 [Line 3 from File A]
4 [Line 4 from File B]
5 [Line 5 from File A]
6 [Line 6 from File A]
7 [Line 7 from File A]
8 [Line 8 from File B]
.....etc....

I'm not that smart so I can't figure out how to do it. A script would be
fine, as would a single command line.

¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
¸.·´ ¸.·*¨¯¨*·. THANK YOU!!.·*¨¯¨*·.¸ ´·.¸
¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
Janis Papanagnou

2007-09-27, 7:36 pm

Elliot wrote:
> This should be an easy one for you smart UNIX people.
>
> I have two files. I need to replace every 4th line in File A with the
> corresponding line in File B. So when I vi my output file I want to see:
>
> 1 [Line 1 from File A]
> 2 [Line 2 from File A]
> 3 [Line 3 from File A]
> 4 [Line 4 from File B]
> 5 [Line 5 from File A]
> 6 [Line 6 from File A]
> 7 [Line 7 from File A]
> 8 [Line 8 from File B]
> ....etc....


One possibility...

paste -d $'\n' FileA FileB | awk 'NR%8==1||NR%8==3||NR%8==5||NR%8==0'


Janis

>
> I'm not that smart so I can't figure out how to do it. A script would be
> fine, as would a single command line.
>
> ¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
> ¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
> ¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
> ¸.·´ ¸.·*¨¯¨*·. THANK YOU!!.·*¨¯¨*·.¸ ´·.¸
> ¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
> ¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
> ¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸

John W. Krahn

2007-09-27, 7:36 pm

Elliot wrote:
> This should be an easy one for you smart UNIX people.
>
> I have two files. I need to replace every 4th line in File A with the
> corresponding line in File B. So when I vi my output file I want to see:
>
> 1 [Line 1 from File A]
> 2 [Line 2 from File A]
> 3 [Line 3 from File A]
> 4 [Line 4 from File B]
> 5 [Line 5 from File A]
> 6 [Line 6 from File A]
> 7 [Line 7 from File A]
> 8 [Line 8 from File B]


$ PERL -i -pe'
BEGIN { @fileB = `cat File_B.txt` }
$. % 4 or $_ = $fileB[ $. - 1 ]
' File_A.txt



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Ed Morton

2007-09-28, 1:30 am

Elliot wrote:
> This should be an easy one for you smart UNIX people.
>
> I have two files. I need to replace every 4th line in File A with the
> corresponding line in File B. So when I vi my output file I want to see:
>
> 1 [Line 1 from File A]
> 2 [Line 2 from File A]
> 3 [Line 3 from File A]
> 4 [Line 4 from File B]
> 5 [Line 5 from File A]
> 6 [Line 6 from File A]
> 7 [Line 7 from File A]
> 8 [Line 8 from File B]
> ....etc....
>


awk 'NR==FNR{a[NR]=$0; next} {print FNR%4 ? $0 : a[FNR]}' fileB fileA

or if you want to save some memory:

awk 'NR==FNR{if (NR%4) a[NR]=$0; next} {print FNR%4 ? $0 : a[FNR]}'
fileB fileA

Ed.
William James

2007-09-28, 1:30 am

On Sep 27, 4:16 pm, Elliot <ell...@home.net> wrote:
> This should be an easy one for you smart UNIX people.
>
> I have two files. I need to replace every 4th line in File A with the
> corresponding line in File B. So when I vi my output file I want to see:
>
> 1 [Line 1 from File A]
> 2 [Line 2 from File A]
> 3 [Line 3 from File A]
> 4 [Line 4 from File B]
> 5 [Line 5 from File A]
> 6 [Line 6 from File A]
> 7 [Line 7 from File A]
> 8 [Line 8 from File B]
> ....etc....
>
> I'm not that smart so I can't figure out how to do it. A script would be
> fine, as would a single command line.


awk 'NR==FNR{a[NR]=$0;next}
FNR%4{print a[FNR];next} 1' fileA fileB

Rob S

2007-09-28, 1:30 am

Elliot wrote:
> This should be an easy one for you smart UNIX people.
>
> I have two files. I need to replace every 4th line in File A with the
> corresponding line in File B. So when I vi my output file I want to see:
>

Hi Elliot. Others have provided good solutions. I'm just curious, when
you say you vi your output, I hope you're not using vi as a file reader.
Vi is an editor, and it would be very easy to change your file
accidentally if you use it to read files.

less output.file or more output.file or even cat output.file (shudder)
are better methods of reading ascii files.

--

Rob
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
http://www.aspir8or.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -


You know you've spent too much time on the computer when you spill milk
and the first thing you think is, 'edit, undo.'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
William James

2007-09-28, 1:30 am

On Sep 27, 4:16 pm, Elliot <ell...@home.net> wrote:
> This should be an easy one for you smart UNIX people.
>
> I have two files. I need to replace every 4th line in File A with the
> corresponding line in File B. So when I vi my output file I want to see:
>
> 1 [Line 1 from File A]
> 2 [Line 2 from File A]
> 3 [Line 3 from File A]
> 4 [Line 4 from File B]
> 5 [Line 5 from File A]
> 6 [Line 6 from File A]
> 7 [Line 7 from File A]
> 8 [Line 8 from File B]
> ....etc....
>
> I'm not that smart so I can't figure out how to do it. A script would be
> fine, as would a single command line.


ruby -e 'a,b=[0,0].map{gets(nil).split /\n/}
a.size.times{|i|
puts(((i+1)%4==0 ? b : a)[i])}' fileA fileB

Cyrus Kriticos

2007-09-28, 1:30 am

Rob S wrote:
> Elliot wrote:
> I'm just curious, when
> you say you vi your output, I hope you're not using vi as a file reader.
> Vi is an editor, and it would be very easy to change your file
> accidentally if you use it to read files.


vi -R FILENAME

--- man 1 vi ---
[...]
-R Read-only mode. The ’readonly’ option will be
set. You can still edit the buffer, but will be
prevented from accidently overwriting a file. If
you do want to overwrite a file, add an exclamaâ€
tion mark to the Ex command, as in ":w!". The -R
option also implies the -n option (see below).
The ’readonly’ option can be reset with ":set
noro". See ":help ’readonly’".
[...]
----------------

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Tiago Peczenyj

2007-09-28, 1:24 pm


my 2 cents

using GNU sed version 4.0.9
$ (cat -n fileA | sed '4~4d' ; cat -n fileB | sed -n '4~4p') | sort -n
| cut -f2-
line 1 from file A
line 2 from file A
line 3 from file A
line 4 from file B
line 5 from file A
line 6 from file A
line 7 from file A
line 8 from file B
line 9 from file A
line 10 from file A

# you can use
# sed '4~4!d' instead of sed -n '4~4p'
# from man sed
# Addresses ...
# first~step
# Match every step'th line starting with line first. For
example, ``sed -n 1~2p'' will print all the
# odd-numbered lines in the input stream, and the address 2~5 will
match every fifth line, starting with
# the second. (This is an extension.)
#
# or using awk ...

$ ( cat -n fileA | awk 'NR%4!=3D0' ; cat -n fileB | awk 'NR%4 =3D=3D 0') |
sort -n | cut -f2-

But, Mr. Ed Morton shows a better way to use awk (and much more
simple!)

Best Regards

Tiago

On Sep 27, 6:16 pm, Elliot <ell...@home.net> wrote:
> This should be an easy one for you smart UNIX people.
>
> I have two files. I need to replace every 4th line in File A with the
> corresponding line in File B. So when I vi my output file I want to see:
>
> 1 [Line 1 from File A]
> 2 [Line 2 from File A]
> 3 [Line 3 from File A]
> 4 [Line 4 from File B]
> 5 [Line 5 from File A]
> 6 [Line 6 from File A]
> 7 [Line 7 from File A]
> 8 [Line 8 from File B]
> ....etc....
>
> I'm not that smart so I can't figure out how to do it. A script would be
> fine, as would a single command line.
>
> =B8.=B7=B4 =B8.=B7*=A8=AF=A8*=B7.=B8 =B4=B7.=B8=B8.=B7=B4 =B8.=B7*=A8=AF=

=A8*=B7.=B8 =B4=B7.=B8
> =B8.=B7=B4 =B8.=B7*=A8=AF=A8*=B7.=B8 =B4=B7.=B8=B8.=B7=B4 =B8.=B7*=A8=AF=

=A8*=B7.=B8 =B4=B7.=B8
> =B8.=B7=B4 =B8.=B7*=A8=AF=A8*=B7.=B8 =B4=B7.=B8=B8.=B7=B4 =B8.=B7*=A8=AF=

=A8*=B7.=B8 =B4=B7.=B8
> =B8.=B7=B4 =B8.=B7*=A8=AF=A8*=B7. THANK YOU!!.=B7*=A8=AF=A8*=B7.=B8 =B4=

=B7.=B8
> =B8.=B7=B4 =B8.=B7*=A8=AF=A8*=B7.=B8 =B4=B7.=B8=B8.=B7=B4 =B8.=B7*=A8=AF=

=A8*=B7.=B8 =B4=B7.=B8
> =B8.=B7=B4 =B8.=B7*=A8=AF=A8*=B7.=B8 =B4=B7.=B8=B8.=B7=B4 =B8.=B7*=A8=AF=

=A8*=B7.=B8 =B4=B7.=B8
> =B8.=B7=B4 =B8.=B7*=A8=AF=A8*=B7.=B8 =B4=B7.=B8=B8.=B7=B4 =B8.=B7*=A8=AF=

=A8*=B7.=B8 =B4=B7.=B8


Rob S

2007-09-29, 7:25 pm

Elliot wrote:
> This should be an easy one for you smart UNIX people.
>
> I have two files. I need to replace every 4th line in File A with the
> corresponding line in File B. So when I vi my output file I want to see:
>

Hi Elliot. Others have provided good solutions. I'm just curious, when
you say you vi your output, I hope you're not using vi as a file reader.
Vi is an editor, and it would be very easy to change your file
accidentally if you use it to read files.

less output.file or more output.file or even cat output.file (shudder)
are better methods of reading ascii files.

--

Rob
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
http://www.aspir8or.com
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -


You know you've spent too much time on the computer when you spill milk
and the first thing you think is, 'edit, undo.'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com