Split a file into multiple files based on column value [closed] - powershell

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to split the below file into multiple files based on content;
Input file:
HC05361 3036690000002020072814093990 R
DC05361 00100001CD 03113 075992733625
HC05362 3036690000002020072814103991 R
DC05362 00100001CD 03113 075992733625
HC05615 3024173024172020091408223795 R
DC05615 00100001NDL 00016 856115004682
DC05615 002000011 295013 825646252404
DC05615 003000011 295617 825646248490
Output files:
File1:
HC05361 3036690000002020072814093990 R
DC05361 00100001CD 03113 075992733625
File2:
HC05362 3036690000002020072814103991 R
DC05362 00100001CD 03113 075992733625
File3:
HC05615 3024173024172020091408223795 R
DC05615 00100001NDL 00016 856115004682
DC05615 002000011 295013 825646252404
DC05615 003000011 295617 825646248490

This works for me, but I think you may need to provide more information.
Like...
What you want the filenames to be?
What you know about the data
For example, do you always want to split on lines that start with HC? Are the values in the first column always going to be in the form HC#####?
Making a few assumptions, this is a basic example of what you could do.
This will iterate through every line in the file. If the line starts with HC#####, then it outputs that line and every following line to a file with that name. When it reaches a new HC##### value, it changes that to the file name:
$data = Get-Content .\sample.txt
foreach ($line in $data) {
if ($line -match '^HC[0-9]+') {
$fileName = "$($Matches[0]).txt"
}
Add-Content -Value $line -Path $fileName
}
This is assuming the first line will always provide us a filename.
It's also assuming that you want to split on lines starting with HC
It's also not performing any sort of exception handling.

Related

How to concatenate two strings in dart [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 days ago.
Improve this question
I want to concatenate two strings into a single variable.
String datas1 = "demo1 String2" ;
String datas2 = "String1" ;
i want to get string be like:
String datas3 = "demo1String1 String2"
how to do that?
Don't miss to use " or ' to mention the start and end of String.
String datas1 = "ajnk muhammed";
Or
String datas1 = 'ajnk muhammed';

Need to split a file containing a list of files in to chunks A-G, H-N... using PowerShell

The text file constans a list of files in alphabetical order.
path\afilename1.nnn
path\bafilename1.xxx
path\cafilemane2.sdf
path\ccfilename3.fds
...
I need to divide the file to separate files containing list of files starting with letters A to G next file with list of files H to N and so on...
I feel like we could somehow make Group-Object work for us here, but the switch approach alluded to by Lieven is a really good start. I would add that Switch{} can interrogate a file directly, so something as simple as:
$a_g_Files = [Collections.ArrayList]#()
$h_n_Files = [Collections.ArrayList]#()
Switch -RegEx -File 'c:\temp\filelist.txt'
{
'^path\\[a-g]' { [void]$a_g_Files.Add( $_ ); Break }
'^path\\[h-n]' { [void]$h_n_Files.Add( $_ ); Break }
}
$a_g_Files | Add-Content "c:\temp\a-gfiles.txt"
$h_n_Files | Add-Content "c:\temp\h-nfiles.txt"
This partly depends on if "path" is literal or if it varies from line to line. But surely even variations can be handled with RegEx.

How to remove part of file between two line delimiter values in Scala Spark? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have text as below,
TIME STAMP1
A1200 EVENT START
EVENT NAME = DOS
EVENT_INS = 1
EVENT_ID = 100
BUFFER = 233355
FORMAT = ATC
LOC = C:/User/data
;
TIME STAMP2
A1201 EVENT START
EVENT NAME = DOS
EVENT_INS = 0
EVENT_ID = 87
BUFFER = 773355
FORMAT = ETC
LOC = C:/User/data
;
how can I remove TIME STAMP2 based on A1201,need to remove from A1201 to ; using scala.A1201 sensor part will repeat at different location in the file. Wherever it comes, I need to remove from A1201 to ;..
How can I do with Scala Spark ?.
You can use the following simple solution
val rdd = sparkContext.wholeTextFiles("path to the text file")
rdd.map(x => x._2.replace("\n", "|*|").split(";").filter(!_.contains("A1201")).mkString(";").replace("|*|", "\n")+";")
where, wholeTextFiles would read the file in Tuple2 format with filename as first argument and text data as the second argument
x._2.replace("\n", "|*|") replaces the line feeds with special character which is to be used later
.split(";") splits the text at ; and forms array
.filter(!_.contains("A1201")) filters out all text from A1201 to ;
.mkString(";").replace("|*|", "\n")+";" converts the array of string to original format.
I hope the answer is helpful

Transform 'abcd' to {'a' 'b' 'c' 'd'} in Matlab [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I want to transform a string to a categorical array in which the categories are the characters.
If you want to make a cell-array of strings (chars) then use the cellstr() function. This will turn each row of a char array into a separate string in a cell-array. Since your string variable is a single row, use the single-quote character to transpose it to a column and then use cellstr():
string ='abcd'
A = cellstr(string') % The single quote after the string variable transposes it to a column
The output A will be columnar, so to get a row cell-array stick another single quote after the A, for example in use with categorical() as you mention:
B = categorical(A')
You can use num2cell for this purpose as follows:
string ='abcd';
num2cell(string)
Output:-
ans =
'a' 'b' 'c' 'd'
strings in matlab are already really a vector of characters.
str = 'abcd';
length(str) %4
str(1) %a
str(2:3) %bc

Automated indentation cleaner for MATLAB or Octave? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 3 years ago.
Improve this question
Does anyone know of an existing method of automatically cleaning the indentation of a MATLAB/Octave script? I've got someone else's code (no, really!), and it's horrible - none of the loops or functions are indented, and half of the other lines are indented to apparently random depths.
The problem with MATLAB is that it doesn't use braces, so C++ style indenters aren't going to work. Python ones might, with a bit of modification, which I will try if I can't find a pre-existing solution.
Basically it'd just need to indent lines after lines starting with function, for, if, while... and un-indent lines starting with end*, I think...
Clarification: As pointed out by Jonas, MATLAB users can just select all, and ctrl+I to nicify the indentation. Unfortunately, I don't have access to the MATLAB editor, and it would also be nice to be able to auto-indent a batch of files all at once.
CTRL+A (to select all), followed by CTRL+I (to automatically indent) will do the trick in the Matlab editor.
Ah, I should have known emacs and vi would have answers. I really should learn one of them. Anyway, I got frustrated with the work I was doing, and wrote this as a displacement activity. Remove the + '.test.m' to replace files:
#!/usr/bin/env python
import re, sys
def startswith(line=""):
# these need some word-boundary condition, but \b isn't working
ctrlstart = '\s*(function|if|while|for|switch)'
ctrlcont = '\s*(elseif|else|case|catch|otherwise)'
ctrlend = '\s*(end|endfunction|endif|endwhile|endfor|endswitch)'
match = re.match(ctrlstart, line)
if ( match != None ) :
return ['start', match.group(0)]
match=re.match(ctrlcont, line)
if ( match!=None ) :
return ['cont', match.group(0)]
match=re.match(ctrlend, line)
if ( match!=None ) :
return ['end', match.group(0)]
else :
return [False, None]
def main( filelist = list() ) :
for filename in filelist:
nextindent = 0
indentmult = 2
file = open(filename, 'r')
filelines = file.readlines()
for ind in range(0, len(filelines)) :
indentlevel = nextindent
match = startswith(filelines[ind])
if match[0] == 'start' :
nextindent += 1
elif match[0] == 'cont' :
indentlevel -= 1
elif match[0] == 'end' :
indentlevel -= 1
nextindent -= 1
elif match[0] == False :
nextindent = indentlevel
filelines[ind] = ' '*indentlevel*indentmult + filelines[ind].lstrip().rstrip() +'\n'
outfile = open(filename + '.test.m', 'w')
outfile.writelines(filelines)
file.close()
outfile.close()
args = []
for arg in sys.argv[1:] :
args += [str(arg)]
main(args)
I tried the emacs way but it doesn't work, i am new to ubuntu and octave. So i took the easiest way :D, online site that indent the code for me and i can copy/paste the new clean code.
http://base-n.de/matlab/code_beautifier.html
Here is a vim plugin to automatically indent and syntax highlight for octave code
https://github.com/tranvansang/octave.vim