swapping variable name and variable type - text-processing

I have a text file with a long list of variables like this:
a VARCHAR(32),
b INT,
c TINYINT,
.
.
.
I want to quickly swap the order of the name and type so I have:
VARCHAR(32) a,
INT b,
TINYINT c
.
.
.
Im happy to use a bash terminal or notepad ++ and I do have a basic knowledge of regex but Im not sure how to tackle this problem.
How can I go about doing this?

you can use this app I just wrote:
http://codepen.io/franzskuffka/pen/Ndxejz
Or run it through this function
function swap (text) {
let lines = text.split(',\n')
let parts = lines.map(function (line) {
var lineParts = line.split(' ')
lineParts[2] = lineParts[0]
delete lineParts[0]
return lineParts.join(' ')
})
return parts.join(',\n')
}
Generally I recommend the text editor Kakoune for this task which is awesome for text processing in general due to the multicursor support and incremental editing.

Related

How do I get a snippet to insert a character only if the user typed something?

I have this snippet.
SELECT 'SELECT * FROM ' + OBJECT_SCHEMA_NAME(o.object_id, DB_ID(${20:})) + '.' + name,
*
FROM ${20/$/./}sys.all_objects o
WHERE name LIKE '%${10:hadr}%'
ORDER BY o.name;
And this is how it works:
When the user types something in the function DB_ID(), I hope the content the user typed appears before sys.all_objects AND append an additional .. It already works like this as it shown in the above gif. However, I also hope if the user types nothing in the function DB_ID(), don't add . before sys.all_objects. Is this possible?
No need to add the : in field 2:
DB_ID(${2})
Use field 2
${2/(.*)/$1${1:+.}/}
capture all the typed text: (.*)
replace it with all the typed text: $1
followed by a . if the typed text is not empty: ${1:+.}
You can use lookbehind to assert that there's something in that field: (?<=.)$. For a minimal example, let's say this is the original snippet:
foo($1); ${1/$/./}bar()
Change it to:
foo($1); ${1/(?<=.)$/./}bar()
If I type something, e.g. x, then press Tab, I get:
foo(x); x.bar()
If I don't type anything then press Tab, I get:
foo(); bar()

Use Autohotkey variable in text snippet

After many times of seraching i could not find an answer regarding my use case on Google or Stack Overflow.
I'm heavily using Autohotkey snippets like this for email:
:*:emailsignature::
(
Email signature text
)
But sometimes i need to temporarily add some more text inside the existing snippet.
:*:emailsignature::
(
Email signature text
%datewithtext%
Email signature text
)
::datewithtext::*** Date with text ***
So what i was wishing for is to add a variable inside the snippet as shown above. And of course I've tried the variable syntax from AHK, but got all sorts of errors.
So hopefully someone can point my in the right direction or even better show me an example code.
Thanks in advance.
On top of all the other suggestions, I would suggest to prevent you having to write "emailsignature" to trigger the text expansion. Instead, I would use e.g. "es". This short string is unique enough with the \ as the closing character to never (seldom) collide. I use this trick for many long words.
You could try this as a solution
:*:emailsignature:: ; hotstrings expect a literal string on one line,
; but can be used like hotkeys as well.
FormatTime, Date
dateWithText =
( ; the continuation section below works when assigning to a variable
Email signature text
%Date%
Email signature text
)
Return
::datewithtext::
Send % dateWithText
Return
OR, you could get kind of fancy
; auto-execute section
V_EmailSignature := "myemail#yahoo.com"
V_MomEmail := "mom#yahoo.com"
V_StreetAddress := "123 Any Street`r`nAnytown, ID, 12345"
Return ; end auto-execute section
; HOTSTRINGS
:*:emailsignature::
:*:momemail::
:*:streetaddress::
FormatTime, Date
vName := regexReplace(A_ThisHotkey, "\W")
newText := V_%vName%
dateWithText := Join("`r`n", newText, Date, newText)
Return
::datewithtext::
Send % dateWithText
Return
Join(delim, Strs*) {
newStr := ""
for _, Str in Strs {
newStr .= (newStr ? Delim : "") . Str
}
Return newStr
}
Does that help you?

Converting numbers into timestamps (inserting colons at specific places)

I'm using AutoHotkey for this as the code is the most understandable to me. So I have a document with numbers and text, for example like this
120344 text text text
234000 text text
and the desired output is
12:03:44 text text text
23:40:00 text text
I'm sure StrReplace can be used to insert the colons in, but I'm not sure how to specify the position of the colons or ask AHK to 'find' specific strings of 6 digit numbers. Before, I would have highlighted the text I want to apply StrReplace to and then press a hotkey, but I was wondering if there is a more efficient way to do this that doesn't need my interaction. Even just pointing to the relevant functions I would need to look into to do this would be helpful! Thanks so much, I'm still very new to programming.
hfontanez's answer was very helpful in figuring out that for this problem, I had to use a loop and substring function. I'm sure there are much less messy ways to write this code, but this is the final version of what worked for my purposes:
Loop, read, C:\[location of input file]
{
{ If A_LoopReadLine = ;
Continue ; this part is to ignore the blank lines in the file
}
{
one := A_LoopReadLine
x := SubStr(one, 1, 2)
y := SubStr(one, 3, 2)
z := SubStr(one, 5)
two := x . ":" . y . ":" . z
FileAppend, %two%`r`n, C:\[location of output file]
}
}
return
Assuming that the "timestamp" component is always 6 characters long and always at the beginning of the string, this solution should work just fine.
String test = "012345 test test test";
test = test.substring(0, 2) + ":" + test.substring(2, 4) + ":" + test.substring(4, test.length());
This outputs 01:23:45 test test test
Why? Because you are temporarily creating a String object that it's two characters long and then you insert the colon before taking the next pair. Lastly, you append the rest of the String and assign it to whichever String variable you want. Remember, the substring method doesn't modify the String object you are calling the method on. This method returns a "new" String object. Therefore, the variable test is unmodified until the assignment operation kicks in at the end.
Alternatively, you can use a StringBuilder and append each component like this:
StringBuilder sbuff = new StringBuilder();
sbuff.append(test.substring(0,2));
sbuff.append(":");
sbuff.append(test.substring(2,4));
sbuff.append(":");
sbuff.append(test.substring(4,test.length()));
test = sbuff.toString();
You could also use a "fancy" loop to do this, but I think for something this simple, looping is just overkill. Oh, I almost forgot, this should work with both of your test strings because after the last colon insert, the code takes the substring from index position 4 all the way to the end of the string indiscriminately.

Xtext , Defining a Structure for a plot

i have to make a type curve that is a curve plot , the data is given by a CSV file (The interpreter is who read the data file). i have two type of values , x-axis, time (i think INT:INT) minutes:seconds format ; and y-axis voltage ( double ) . My grammar datatypes are the following :
Model:
elements+=Element*;
Element:
DataType /* | Operation | Control*/ ;
DataType :
Text | Real
;
Text:
("String" | "Text") name=ID (value=STRING)?
;
Double returns ecore::EDouble:
INT ('.' INT)?
;
Real:
("Double" | "Real") name=ID (value=Double)?
;
I was traying to make a map in my grammar with this format : Curve(Double voltage,time INT:INT) but i dont know why it doesnt work . what do you think guys ? should i define a new type for the type time ? or how make the map properly? Thanks for your time
CurveCollection :
'Curve['keys+=[Real]*','values+=Time*']'
;
Time:
INT':'INT
;
I tried to define the structure , but i dont know if is going to work like to arrays of (keys , values ) like a Map in java
you can enable a default unique name validation by uncommenting in and rerunning the language workflow
validator = {
composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
}
alternatively you can implement a own unique name validation in <YourDsl>Validator

EDIFACT macro (readable message structure)

I´m working within the EDI area and would like some help with a EDIFACT macro to make the EDIFACT files more readable.
The message looks like this:
data'data'data'data'
I would like to have the macro converting the structure to:
data'
data'
data'
data'
Pls let me know how to do this.
Thanks in advance!
BR
Jonas
If you merely want to view the files in a more readable format, try downloading the Softshare EDI Notepad. It's a fairly good tool just for that purpose, it supports X12, EDIFACT and TRADACOMS standards, and it's free.
Replacing in VIM (assuming that the standard EDIFACT separators/escape characters for UNOA character set are in use):
:s/\([^?]'\)\(.\)/\1\r\2/g
Breaking down the regex:
\([^?]'\) - search for ' which occurs after any character except ? (the standard escape character) and capture these two characters as the first atom. These are the last two characters of each segment.
\(.\) - Capture any single character following the segment terminator (ie. don't match if the segment terminator is already on the end of a line)
Then replace all matches on this line with a new line between the segment terminator and the beginning of the next segment.
Otherwise you could end up with this:
...
FTX+AAR+++FORWARDING?: Freight under Vendor?'
s care.'
NAD+BY+9312345123452'
CTA+PD+0001:Terence Trent D?'
Arby'
...
instead of this:
...
FTX+AAR+++FORWARDING?: Freight under Vendor?'s care .'
NAD+BY+9312345123452'
CTA+PD+0001:Terence Trent D?'Arby'
...
Is this what you are looking for?
Option Explicit
Dim stmOutput: Set stmOutput = CreateObject("ADODB.Stream")
stmOutput.Open
stmOutput.Type = 2 'adTypeText
stmOutput.Charset = "us-ascii"
Dim stm: Set stm = CreateObject("ADODB.Stream")
stm.Type = 1 'adTypeBinary
stm.Open
stm.LoadFromFile "EDIFACT.txt"
stm.Position = 0
stm.Type = 2 'adTypeText
stm.Charset = "us-ascii"
Dim c: c = ""
Do Until stm.EOS
c = stm.ReadText(1)
Select Case c
Case Chr(39)
stmOutput.WriteText c & vbCrLf
Case Else
stmOutput.WriteText c
End Select
Loop
stm.Close
Set stm = Nothing
stmOutput.SaveToFile "EDIFACT.with-CRLF.txt"
stmOutput.Close
Set stmOutput = Nothing
WScript.Echo "Done."