Stumped on this one

19 views
Skip to first unread message

@lbutlr

unread,
Jun 15, 2021, 7:28:45 PM6/15/21
to BBEdit Talk
I have a file that has a few hundred lines, 77 of which are a HTML/XML style date element

<date>Tue, 15 Jun 2021 17:12:26 -0600</date>

I have another files that has 77 date lines in the same format. Everything is different dates, obvs.

I want to replace the first date in file 1 with the first date in file 2, and so on through the entire file.

I haven't come up with a goos idea, though I think I have several BAD ideas. The one that seems least bad (but really bad) is to format the files with all 'blocks" onto a single line, then line up all the date fields, and copy and paste a multiline paste, but I figure by the time I have lined up the dates I'm spent more time than doing it by hand.

I don't want to do it by hand, partly because I would rather waste hours figuring out how to do it than undertake the tediuous job, but nastily because I know going it by hand things will get borked.

I swear I had a way to do this a couple o months ago.

--
Steak and suspicious-organ pie

Neil Faiman

unread,
Jun 15, 2021, 7:48:18 PM6/15/21
to BBEdit Talk Mailing List
This can probably be done with half a dozen lines of Perl. I haven't coded in Perl for years, so I won't try to write the actual code, but it would be something like:

for every line in file 1
if line contains /<date>.*</date>/
read a date from file 2
replace /<date>.*</date>/ in line with the date from file 2
write line

Trying to be cleverer than that would probably be like trying to trim a hedge with a lawn mower.

Regards,

Neil Faiman

Jeffrey Jones

unread,
Jun 15, 2021, 8:02:32 PM6/15/21
to bbe...@googlegroups.com
On 2021 Jun 15, at 19:28, @lbutlr <kre...@kreme.com> wrote:

I want to replace the first date in file 1 with the first date in file 2, and so on through the entire file.

I'll bet there are several ways to do it with the Canonize command.

For example, extract all the date lines from the first file. Then paste the date lines from the second file as a second column. Now, use that as the Canonize file.

Christopher Stone

unread,
Jun 15, 2021, 10:36:04 PM6/15/21
to BBEdit-Talk
On 06/15/2021, at 18:28, @lbutlr <kre...@kreme.com> wrote:
I have a file that has a few hundred lines, 77 of which are a HTML/XML style date element

<date>Tue, 15 Jun 2021 17:12:26 -0600</date>

I have another files that has 77 date lines in the same format. Everything is different dates, obvs.

I want to replace the first date in file 1 with the first date in file 2, and so on through the entire file.


Hey Lewis,

I think what I'd do is:

1) Scrape the file 1 for the replacement date lines.

2) Paste them into this script in place of the dummy $input items:



#!/usr/bin/env perl -sw
use v5.010;

my $input = '
<date>Tue, 01 May 2021 17:12:26 -0600</date>
<date>Tue, 02 May 2021 17:12:26 -0600</date>
<date>Tue, 03 May 2021 17:12:26 -0600</date>
';

$input =~ s!\A\s+|\s+$!!g;

my @array = split(/\n/, $input);

my $counter = 0;

while (<>) {

   

   if (m!<date>.+?</date>!) {

      s!<date>.+?</date>!$array[$counter]!;
      $counter++;
      print;

      

   } else {

      print;

   }

   

}



3) Open the file where the replacements are to take place to be frontmost in BBEdit.

4) Run the script.

It works well on my test file (only lightly tested).



If I had more time and was feeling adventurous I'd do basically the same thing, except:

1) My user-input would be the source and destination file paths.

2) I'd read the source file date items directly into an array.

3) Then I'd open the destination file and do the processing in-place.

I haven't played with reading and writing files for a while, so that's more than I want to tackle at the moment.


--
Take Care,
Chris


@lbutlr

unread,
Jun 16, 2021, 8:57:18 AM6/16/21
to BBEdit Talk
On 15 Jun 2021, at 18:01, 'Jeffrey Jones' via BBEdit Talk <bbe...@googlegroups.com> wrote:
> On 2021 Jun 15, at 19:28, @lbutlr <kre...@kreme.com> wrote:
>>
>> I want to replace the first date in file 1 with the first date in file 2, and so on through the entire file.
>
> I'll bet there are several ways to do it with the Canonize command.
Yes, this is what I was trying to remember!

Brain like a steel sieve, lemme tell ya!

--
Stone circles were common enough everywhere in the mountains. Druids
built them as weather computers, and since it was always cheaper
to build a new 33-Megalith circle than to upgrade an old slow
one, there were generally plenty of ancient ones around --Lords
and Ladies

Reply all
Reply to author
Forward
0 new messages