replacing lines in a file with lines from another file
replacing lines in a file with lines from another file
am 27.09.2007 23:16:01 von Elliot
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!!.·*¨¯¨*·.¸ ´·.¸
¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
Re: replacing lines in a file with lines from another file
am 27.09.2007 23:39:29 von Janis Papanagnou
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!!.·*¨¯¨*·.¸ ´·.¸
> ¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
> ¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
> ¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸¸.·´ ¸.·*¨¯¨*·.¸ ´·.¸
Re: replacing lines in a file with lines from another file
am 28.09.2007 01:27:19 von Dummy
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
Re: replacing lines in a file with lines from another file
am 28.09.2007 02:53:47 von Ed Morton
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.
Re: replacing lines in a file with lines from another file
am 28.09.2007 02:54:52 von William James
On Sep 27, 4:16 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....
>
> 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
Re: replacing lines in a file with lines from another file
am 28.09.2007 03:54:59 von Rob S
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.'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Re: replacing lines in a file with lines from another file
am 28.09.2007 03:54:59 von Rob S
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.'
- - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Re: replacing lines in a file with lines from another file
am 28.09.2007 04:05:14 von William James
On Sep 27, 4:16 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....
>
> 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
Re: replacing lines in a file with lines from another file
am 28.09.2007 06:08:38 von Cyrus Kriticos
Rob S wrote:
> Elliot wrote:
>>
>> So when I vi my output file I want to see:
>>
> 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
Re: replacing lines in a file with lines from another file
am 28.09.2007 19:43:49 von Tiago Peczenyj
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 == 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 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.·´ =B8.=B7*¨¯=A8*=B7.=B8 ´·.¸¸.·´ =B8.=B7*¨¯=
=A8*=B7.=B8 ´·.=B8
> =B8.·´ =B8.=B7*¨¯=A8*=B7.=B8 ´·.¸¸.·´ =B8.=B7*¨¯=
=A8*=B7.=B8 ´·.=B8
> =B8.·´ =B8.=B7*¨¯=A8*=B7.=B8 ´·.¸¸.·´ =B8.=B7*¨¯=
=A8*=B7.=B8 ´·.=B8
> =B8.·´ =B8.=B7*¨¯=A8*=B7. THANK YOU!!.=B7*¨¯=A8*=B7.=B8 =B4=
=B7.=B8
> =B8.·´ =B8.=B7*¨¯=A8*=B7.=B8 ´·.¸¸.·´ =B8.=B7*¨¯=
=A8*=B7.=B8 ´·.=B8
> =B8.·´ =B8.=B7*¨¯=A8*=B7.=B8 ´·.¸¸.·´ =B8.=B7*¨¯=
=A8*=B7.=B8 ´·.=B8
> =B8.·´ =B8.=B7*¨¯=A8*=B7.=B8 ´·.¸¸.·´ =B8.=B7*¨¯=
=A8*=B7.=B8 ´·.=B8