Add new textline having double quotes using sed - sed

Trying to add new textline having double quotes using sed.
Original file contents:
case String of
"line1" -> 10;
"line2" -> 20;
_ -> 32
end.
After adding new text, should look like:
case String of
"line1" -> 10;
"line2" -> 20;
"newline" -> 30;
_ -> 32
end.
Tried with command:
sed -i '3i "newline" -> 30;' filename

I suggest with GNU sed:
sed -i '4i \ "newline" -> 30;' file

Tested with GNU sed (4.2.2):
sed -i '3a\ "newline" -> 30;' input.txt
Before:
case String of
"line1" -> 10;
"line2" -> 20;
_ -> 32
end.
After:
case String of
"line1" -> 10;
"line2" -> 20;
"newline" -> 30;
_ -> 32
end.

Related

How can i define such a macro in spin?

I wrote the following model:
#define inc(sn)if :: sn < 255 -> sn = sn + 1; ::else -> sn = 1; fi;
#define inc_twice(sn) if :: sn+2 >255 -> sn= sn-253; ::else -> sn=sn+2; fi;
active proctype monitor()
{
byte sn = 255;
assert (inc(sn) ==1);
}
But the compiler fails as follows:
spin: test2.pml:9, Error: syntax error saw 'keyword: if' near 'if'
spin: test2.pml:9, Error: syntax error saw 'token: ::'
spin: test2.pml:9, Error: syntax error saw 'keyword: fi' near 'fi'
spin: test2.pml:11, Error: aborting (ana_stmnt)
child process exited abnormally.
How can i solve it?
I have met the same problem . And you can solve it by change your code into this :
#define inc(sn)if \\
:: sn < 255 -> sn = sn + 1 \\
::else -> sn = 1 \\
fi;
#define inc_twice(sn) if \\
:: sn+2 >255 -> sn= sn-253 \\
::else -> sn=sn+2 \\
fi;
I don't know what causes this issue, but I solve it by the above method.

How to create a 16 bytes Array for key to ShipHash in F#?

I am working on code where I need to hash values. SipHash seems like a great option.
let getSipHashValue (buffer:byte []) (key:byte []) =
match key.GetLength(0) with
| 16 -> SipHash24.Hash64(buffer, key)
| _ -> uint64(0)
Is there a way to pad the key to 16 bytes and make sure that it works?
I can get the exact length word as key but I would like to be able to use any word (that is shorter than 16 bytes) and just use some padding.
open System
open System.Text
let testKey : byte [] =
Encoding.UTF8.GetBytes "accumulativeness"
Console.WriteLine("Length: {0}", testKey.GetLength(0))
Is there a way to do that in F#?
I think I got it:
open System
open System.Text
let rec getPaddedBytes (s:string) =
let b = Encoding.UTF8.GetBytes s
match b.GetLength(0) with
| 16 -> b
| x when x < 16 -> getPaddedBytes (s + "0")
| _ -> b[0..15]
Console.WriteLine("Length: {0}", testKey.GetLength(0))
let testBytes = getPaddedBytes "accum"
let testString = Encoding.UTF8.GetString testBytes
Console.WriteLine("X: {0}", testString)
I need to fix getting the first 16 bytes. Not sure about that syntax.
It's not clear from your question what you want the padding to look like but you can pad with zeros with:
let getPadding (bs: byte[]): byte[] =
let rem = bs.Length % 16
let padBytes = if rem = 0 then 0 else (16 - rem)
Array.zeroCreate padBytes
let pad (bs: byte[]): byte[] =
Array.append bs (getPadding bs)
which you can then use with:
let padded = pad testKey
printfn "Key length: %d" padded.Length

Type mismatch: cannot convert from Map<Object,Object> to Map<Character,Integer>

Trying to get mapping digit between 1-26 from input String using stream operation.
input -
XYZ or xyz
expected output is Map -
X - 24
Y - 25
Z - 26
Below logic is giving compilation error :
Scanner scr = new Scanner(System.in);
String testString = scr.next();
Map<Character, Integer> charDigitMap = testString.chars().mapToObj(i -> (char) i).collect( Collectors.toMap(c -> c ,c -> (c - 'A' + 1)));
charDigitMap.forEach((k,v) -> System.out.println("--"+k+"--"+v));
Error is :
Type mismatch: cannot convert from Map<Object,Object> to Map<Character,Integer>
P.S :
Looks its a eclipse issue. Working fine on intelliJ.
You are missing to uppercase the chars (if input is small case). Add the following and your code should run fine:
.map(Character::toUpperCase)
An another approach:
Map<Character, Integer> intArray = "xyz".chars()
.map(Character::toUpperCase)
.collect(HashMap::new,
(c,m) -> c.put((char)m,
m-64),HashMap<Character,Integer>::putAll
);
Output:
{X=24, Y=25, Z=26}

Does PureScript support “format strings” like C / Java etc.?

I need to output a number with leading zeros and as six digits. In C or Java I would use "%06d" as a format string to do this. Does PureScript support format strings? Or how would I achieve this?
I don't know of any module that would support a printf-style functionality in PureScript. It would be very nice to have a type-safe way to format numbers.
In the meantime, I would write something likes this:
import Data.String (length, fromCharArray)
import Data.Array (replicate)
-- | Pad a string with the given character up to a maximum length.
padLeft :: Char -> Int -> String -> String
padLeft c len str = prefix <> str
where prefix = fromCharArray (replicate (len - length str) c)
-- | Pad a number with leading zeros up to the given length.
padZeros :: Int -> Int -> String
padZeros len num | num >= 0 = padLeft '0' len (show num)
| otherwise = "-" <> padLeft '0' len (show (-num))
Which produces the following results:
> padZeros 6 8
"000008"
> padZeros 6 678
"000678"
> padZeros 6 345678
"345678"
> padZeros 6 12345678
"12345678"
> padZeros 6 (-678)
"-000678"
Edit: In the meantime, I've written a small module that can format numbers in this way:
https://github.com/sharkdp/purescript-format
For your particular example, you would need to do the following:
If you want to format Integers:
> format (width 6 <> zeroFill) 123
"000123"
If you want to format Numbers
> format (width 6 <> zeroFill <> precision 1) 12.345
"0012.3"

Convert timestamp to datetime in erlang

How can I convert a timestamp (number of milliseconds since 1 Jan 1970..., aka epoch) to Date or DateTime format in Erlang? Something like {Year,Month,Day}.
Roughly:
msToDate(Milliseconds) ->
BaseDate = calendar:datetime_to_gregorian_seconds({{1970,1,1},{0,0,0}}),
Seconds = BaseDate + (Milliseconds div 1000),
{ Date,_Time} = calendar:gregorian_seconds_to_datetime(Seconds),
Date.
It just so happens that I have a github gist with a bunch of datetime utilities for exactly this purpose: http://gist.github.com/104903. Calendar has most of the low level plumbing for this stuff.
-module(date_util).
-compile(export_all).
epoch() ->
now_to_seconds(now())
.
epoch_hires() ->
now_to_seconds_hires(now())
.
now_to_seconds({Mega, Sec, _}) ->
(Mega * 1000000) + Sec
.
now_to_milliseconds({Mega, Sec, Micro}) ->
now_to_seconds({Mega, Sec, Micro}) * 1000
.
now_to_seconds_hires({Mega, Sec, Micro}) ->
now_to_seconds({Mega, Sec, Micro}) + (Micro / 1000000)
.
now_to_milliseconds_hires({Mega, Sec, Micro}) ->
now_to_seconds_hires({Mega, Sec, Micro}) * 1000
.
epoch_gregorian_seconds() ->
calendar:datetime_to_gregorian_seconds({{1970,1,1}, {0,0,0}})
.
now_to_gregorian_seconds() ->
epoch_to_gregorian_seconds(now())
.
epoch_to_gregorian_seconds({Mega, Sec, Micro}) ->
epoch_to_gregorian_seconds(now_to_seconds({Mega, Sec, Micro}));
epoch_to_gregorian_seconds(Now) ->
EpochSecs = epoch_gregorian_seconds()
, Now + EpochSecs
.
gregorian_seconds_to_epoch(Secs) ->
EpochSecs = epoch_gregorian_seconds()
, Secs - EpochSecs
.
date_to_epoch(Date) ->
datetime_to_epoch({Date, {0,0,0} })
.
datetime_to_epoch({Date, Time}) ->
gregorian_seconds_to_epoch(
calendar:datetime_to_gregorian_seconds({Date, Time}))
.
is_older_by(T1, T2, {days, N}) ->
N1 = day_difference(T1, T2)
, case N1 of
N2 when (-N < N2) ->
true;
_ ->
false
end
.
is_sooner_by(T1, T2, {days, N}) ->
case day_difference(T1, T2) of
N1 when N > N1 ->
true;
_ ->
false
end
.
is_time_older_than({Date, Time}, Mark) ->
is_time_older_than(calendar:datetime_to_gregorian_seconds({Date, Time})
, Mark);
is_time_older_than(Time, {DateMark, TimeMark}) ->
is_time_older_than(Time
, calendar:datetime_to_gregorian_seconds({DateMark, TimeMark}));
is_time_older_than(Time, Mark) when is_integer(Time), is_integer(Mark) ->
Time < Mark
.
day_difference({D1, _}, D2) ->
day_difference(D1, D2);
day_difference(D1, {D2, _}) ->
day_difference(D1, D2);
day_difference(D1, D2) ->
Days1 = calendar:date_to_gregorian_days(D1)
, Days2 = calendar:date_to_gregorian_days(D2)
, Days1 - Days2
.
is_time_sooner_than({Date, Time}, Mark) ->
is_time_sooner_than(calendar:datetime_to_gregorian_seconds({Date, Time})
, Mark);
is_time_sooner_than(Time, {DateMark, TimeMark}) ->
is_time_sooner_than(Time
, calendar:datetime_to_gregorian_seconds({DateMark, TimeMark}));
is_time_sooner_than(Time, Mark) when is_integer(Time), is_integer(Mark) ->
Time > Mark
.
subtract(Date, {days, N}) ->
New = calendar:date_to_gregorian_days(Date) - N
, calendar:gregorian_days_to_date(New)
.
add(Date, {days, N}) ->
New = calendar:date_to_gregorian_days(Date) + N
, calendar:gregorian_days_to_date(New)
.
OTP 21.0 added this function
calendar:system_time_to_universal_time(Time, TimeUnit) -> datetime()
Types
Time = integer()
TimeUnit = erlang:time_unit()
Converts a specified system time into universal date and time.
Example:
> os:system_time(1000).
1598512151718
> calendar:system_time_to_universal_time(1598512151718, 1000).
{{2020,8,27},{7,9,11}}
Refrence: https://erlang.org/doc/man/calendar.html#system_time_to_universal_time-2