Perl opening file handle in append mode to read - perl

I have a text file where I have to read the content and append to it. i.e. make a copy of the file and append to it. I wrote the following code
#!/usr/bin/perl
use strict;
use warnings;
my $line;
open FILL, ">> fred"
or warn "Cannot open : $!";
while(<FILL>){
# s/^Author:.*/Author: Randal L. Schwartz/;
# s/^Phone:.*\n//;
# s/^Date:.*/Date: $date/;
$line.=$_;
print $line;
}
select FILL;
print $line;
But it doesn't enter the while loop at all and when I open a filehandle for the same file in read mode and select the file handle in append mode to print it works.
Input :
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Department: R&D
Phone: +1 503 555-0095
Date: Tues March 9, 2004
Version: 2.1
Size: 21k
Status: Final beta
Output :
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Department: R&D
Phone: +1 503 555-0095
Date: Tues March 9, 2004
Version: 2.1
Size: 21k
Status: Final beta
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Department: R&D
Phone: +1 503 555-0095
Date: Tues March 9, 2004
Version: 2.1
Size: 21k
Status: Final beta
This program works correctly
#!/usr/bin/perl
use strict;
use warnings;
my $line;
open FIL, "fred"
or warn "Cannot open : $!";
open FILL, ">> fred"
or warn "Cannot open : $!";
while(<FIL>){
# s/^Author:.*/Author: Randal L. Schwartz/;
# s/^Phone:.*\n//;
# s/^Date:.*/Date: $date/;
$line.=$_;
print $line;
}
select FILL;
print $line;
It appends correctly in file but the terminal output is
Program name: granite
Program name: granite
Author: Gilbert Bates
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Department: R&D
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Department: R&D
Phone: +1 503 555-0095
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Department: R&D
Phone: +1 503 555-0095
Date: Tues March 9, 2004
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Department: R&D
Phone: +1 503 555-0095
Date: Tues March 9, 2004
Version: 2.1
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Department: R&D
Phone: +1 503 555-0095
Date: Tues March 9, 2004
Version: 2.1
Size: 21k
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Department: R&D
Phone: +1 503 555-0095
Date: Tues March 9, 2004
Version: 2.1
Size: 21k
Status: Final beta
Program name: granite
Author: Gilbert Bates
Company: RockSoft
Department: R&D
Phone: +1 503 555-0095
Date: Tues March 9, 2004
Version: 2.1
Size: 21k
Status: Final beta
Why does it print so many times?

$line is the variable in which you accumulate everything read so far. So within the loop, you probably wanted to do
print $_;
or just
print;
instead of
print $line;
To answer your original question... You will want to open the file in both read and write (append) mode.
open FILL, '>>+', 'fred';
(Please don't use the 2-argument version of open. It's unsafe.)
But this places the file pointer at the end of the file, so you'll have to do a
seek FILL, 0, 0;
before reading. Since you read before writing, it makes no difference whether or not you open the file in append mode.
You should even be able to add another seek after reading the file, but I am not sure whether this is going to be portable across Unix and Windows.

Related

Sale cannot be executed in Kadena Pact Marmalde

Errors occur in the following conditions.
Running on local server (ubuntu on windows).
I deployed these files.
Token is minted.
The yaml file is generated via Django.
The generated file is then used to run the API request formatter to generate Json.
Template:
code:
(marmalade.ledger.sale
(read-msg 'token-id)
(read-msg 'account)
(read-decimal 'amount)
(read-integer 'timeout)
)
data:
token-id: "${token_id}"
account: "${account}"
amount: ${amount}
timeout: ${timeout}
quote:
price: "${price}"
recipient-guard:
keys:
[ "${recipient_key}", ]
recipient: ${recipient}
keyPairs:
- public: ${marmalade_pub}
secret: ${marmalade_sec}
caps:
- name: "marmalade.ledger.SALE"
args: ["${token_id}", "${account}", ${amount}, ${timeout}]
Generated Fileļ¼š
code:
(marmalade.ledger.sale
(read-msg 'token-id)
(read-msg 'account)
(read-decimal 'amount)
(read-integer 'timeout)
)
data:
token-id: "92496e13-4414-4c58-aceb-0c7d77120af2"
account: "a0fc037522ae0202052775f773e3cf823e3bd7640a12e80694b2dd76"
amount: 1.0
timeout: 100
quote:
price: "1.0"
recipient-guard:
keys:
[ "52d9d996e6cc77f48a1ff1b734224a2ce6cee3af84c3f6e1720047645e9c2c31", ]
recipient: a0fc037522ae0202052775f773e3cf823e3bd7640a12e80694b2dd76
keyPairs:
- public: b63680e667576818b713d5d398ad395610b522fbc53e08afe46a719a0a128fb5
secret: 4242d66fac539660702ce4f9c01a39bb16f1d6b6c7110c6647010cb4dd041dd8
caps:
- name: "marmalade.ledger.SALE"
args: ["92496e13-4414-4c58-aceb-0c7d77120af2", "a0fc037522ae0202052775f773e3cf823e3bd7640a12e80694b2dd76", 1.0, 100]
JSON:
b'{"cmds":[{"hash":"cD5gqQVmRj08xqOdqfKwLLL2eb76_j_eZDdzV4Xdkjg","sigs":[{"sig":"2fb4bbb496271a138c33a4c8b94cf4511c8ad494b5c0cca94a2d1f98a37025e865b1414116fe77e841fbe71c6430e
5da72209123a63e958b992f4e8ba1770103"}],"cmd":"{\\"networkId\\":null,\\"payload\\":{\\"exec\\":{\\"data\\":{\\"amount\\":1.0,\\"token-id\\":\\"92496e13-4414-4c58-aceb-0c7d7712
0af2\\",\\"account\\":\\"a0fc037522ae0202052775f773e3cf823e3bd7640a12e80694b2dd76\\",\\"quote\\":{\\"recipient-guard\\":{\\"keys\\":[\\"52d9d996e6cc77f48a1ff1b734224a2ce6cee3
af84c3f6e1720047645e9c2c31\\"]},\\"price\\":\\"1.0\\",\\"recipient\\":\\"a0fc037522ae0202052775f773e3cf823e3bd7640a12e80694b2dd76\\"},\\"timeout\\":100},\\"code\\":\\"(marmal
ade.ledger.sale (read-msg \'token-id) (read-msg \'account) (read-decimal
\'amount) (read-integer \'timeout)
)\\"}},\\"signers\\":[{\\"pubKey\\":\\"b63680e667576818b713d5d398a
d395610b522fbc53e08afe46a719a0a128fb5\\",\\"clist\\":[{\\"args\\":[\\"92496e13-4414-4c58-aceb-0c7d77120af2\\",\\"a0fc037522ae0202052775f773e3cf823e3bd7640a12e80694b2dd76\\",1
,100],\\"name\\":\\"marmalade.ledger.SALE\\"}]}],\\"meta\\":{\\"creationTime\\":0,\\"ttl\\":0,\\"gasLimit\\":0,\\"chainId\\":\\"\\",\\"gasPrice\\":0,\\"sender\\":\\"\\"},\\"n
once\\":\\"2022-07-20 09:53:05.975872 UTC\\"}"}]}\n'
The following error occurred when I posted the Json data to request.post on 'localhost:8080/api/v1/poll'.
{'cD5gqQVmRj08xqOdqfKwLLL2eb76_j_eZDdzV4Xdkjg': {'gas': 0, 'result': {'status': 'failure', 'error': {'callStack': [], 'type': 'EvalError', 'message': 'Managed capability not installed: (marmalade.ledger.OFFER "92496e13-4414-4c58-aceb-0c7d77120af2" "a0fc037522ae0202052775f773e3cf823e3bd7640a12e80694b2dd76" 1.0 100)', 'info': ''}}, 'reqKey': 'cD5gqQVmRj08xqOdqfKwLLL2eb76_j_eZDdzV4Xdkjg', 'logs': None, 'metaData': None,
'continuation': None, 'txId': None}}
How do I get these to work properly?
P.S. I am Japanese and not an English speaker. I used a translation site to ask this question. Sorry if the description is missing.

How to print the date in .rmd?

I have been working on my webpage, created in a Jekyll template and hosted by Github.
When I try to include the date in the YAML of a .rmd document, the commit fails. There is no error message, but it does not update the webpage.
This YAML is failing.
---
title: "My Title"
author: "Author"
date: "`r format(Sys.time(), '%d %B, %Y')`"
layout: default
permalink: /pages/Folder/Name/
output: html_document
---
However, when I try to add the date as the author, it appears without any problem (i.e., the commit is successful, and the page is updated).
---
title: "My Title"
author:
- "Author"
- "`r format(Sys.time(), '%d %B, %Y')`"
layout: default
permalink: /pages/Folder/Name/
output: html_document
---
So, the last option works, but it is not cooler.
Could someone tell me what document should I modify? I have tried multiple formats for:
date: "April 20, 2017"
'r format(Sys.time(), '%d %B, %Y')'
Thanks in advance.

Perl script - tables created in real time in terminal output

My script prints data on separate lines in terminal output, it looks like this:
Name: Paul Surname: Doe Age:34
Name: Paulo Surname: Doeson Age:28
Name: Paulinho Surname: Docsone Age:5
Name: Paulmen Surname: Doee Age:12
Name: Paulchen Surname: Dooe Age:31
And I would like all data to be divided into columns and displayed equally in bars, as below:
Name: Paul Surname: Doe Age:34
Name: Paulo Surname: Doeson Age:28
Name: Paulinho Surname: Docsone Age:5
Name: Paulmen Surname: Doee Age:12
Name: Paulchen Surname: Dooe Age:31
I note that the output can not be buffered or stored. I would like to achieve this in real time while running the script. Are there any tricks to achieve this? Something like specifying the width of a cell in an HTML table?
EDIT
Really EXAMPLE code:
#arr = q(Name: Paul Surname: Doe Age:34, Name: Paulo Surname: Doeson Age:28, Name: Paulinho Surname: Docsone Age:5, Name: Paulmen Surname: Doee Age:12, Name: Paulchen Surname: Dooe Age:31);
foreach (#arr) {
# here should be our trick to creating columns
print $_ # should be print element in columns (without buffering). print now, not after loop end.
}
First thing that comes into my mind are Perl formats. It's built-in and give's you a whole bunch of means for formatting output. Have a look at it.

Whois lookup response gives error when calling its properties method

Using this gem: whois (3.6.5)
Doing this:
Whois::Client.new(timeout: 2).lookup('miaz.ca')
And then trying to call .properties on the result of the call (pasted below)
"Domain name: miaz.ca\nDomain status: registered\nCreation date: 2014/03/12\nExpiry date:
2018/03/12\nUpdated date: 2017/03/02\nDNSSEC:
Unsigned\n\nRegistrar:\n Name: Go Daddy Domains
Canada, Inc\n Number: 2316042\n\nName servers:\n
ns61.domaincontrol.com\n ns62.domaincontrol.com\n\n% WHOIS look-up
made at 2017-08-15 20:13:15 (GMT)\n%\n% Use of CIRA's WHOIS service is
governed by the Terms of Use in its Legal\n% Notice, available at
http://www.cira.ca/legal-notice/?lang=en \n%\n% (c) 2017 Canadian
Internet Registration Authority, (http://www.cira.ca/) \n"
But then calling Whois::Client.new(timeout: 2).lookup('childrenandco.qa')
gives this response and .properties gives a undefined method 'zip' for "shops.myshopify.com":String:
"Domain Name: childrenandco.qa\r\nLast Modified: 15-Aug-2017 14:06:49 UTC\r\nRegistrar
Name: W3INFOTECH W.L.L\r\nStatus:
inactive\r\n\r\nRegistrant Contact ID: W3R2736\r\nRegistrant
Contact Name: Ibrahim alobaidan\r\nRegistrant Contact Email:
Visit www.domains.qa\r\n\r\nTech Contact ID:
W3T2736\r\nTech Contact Name: Ibrahim alobaidan\r\nTech
Contact Email: Visit www.domains.qa\r\n\r\nName Server:
shops.myshopify.com\r\n"

wevtutil query to write an output on a single line

Am trying to import / read Windows server event logs to a text file, using a wevtutil command. I use the following command to write my logs to file.txt:
$ wevtutil qe Application \rd:true \f:text (reads application logs)
and the sample output of my command, is:
Event[1]:
Log Name: Application
Source: Microsoft-Windows-Security-SPP
Date: 2016-03-29T13:02:27.000
Event ID: 8196
Task: N/A
Level: Information
Opcode: N/A
Keyword: Classic
User: N/A
User Name: N/A
Computer: WIN-IONOGQTF9O5
Description: License Activation Scheduler (sppuinotify.dll)
Event[2]:
Log Name: Application
Source: Microsoft-Windows
Date: 2016-06-29T13:02:57.000
Event ID: 3444
Task: N/A
Level: Critical
Opcode: N/A
Keyword: Classic
User: N/A
User Name: N/A
Computer: WIN-IONOGDFFF9O5
Description: AIRO.Activation code(sppuinotify.dll)
(Actually,two sample logs).
but, i want to write my log as a single line to .txt file, rather than the above multi-line output for a single log. is there a wevtutil command utility to write a log to a single line, like below:
Event[1]:Log Name: Application Source: Microsoft-Windows-Security-SPP Date: 2016-03-29T13:02:27.000 Event ID: 8196 Task: N/A Level: Information Opcode: N/A Keyword: Classic User: N/A User Name: N/A Computer: WIN-IONOGQTF9O5 Description: License Activation Scheduler (sppuinotify.dll)
Event[2]:Log Name: Application Source: Microsoft-Windows Date: 2016-03-29T13:02:27.000 Event ID: 8196 Task: N/A Level: Information Opcode: N/A Keyword: Classic User: N/A User Name: N/A Computer: WIN-IONOGQTF9O5 Description: License Activation Scheduler (sppuinotify.dll)
Thanks!
$logname = "Application"
$events = Get-EventLog -LogName $logname
$arr = #()
$counter = 1
foreach($event in $events){
$arr += "Event[$counter]:Log Name: $logname Source: $($event.Source) Date: $($event.TimeWritten) Event ID: $($event.EventID) Task: $($event.Category) Level: $($event.EntryType) ..."
$counter++
}
$arr | out-file events.txt
If you need to have Opcode, Keyword etc. use Get-Winevent instead of Get-Eventlog