Sorry if question is vague.
I have a string that I want to format in a certain way
Currently it gets outputted like this
Could I output this like this?
With a new line for after each deliminator?
The common deliminator is the pipe (|) for these.
You can do this with a combination of the split() function to turn the strings into arrays of elements, and UNNEST, to convert each element in the array into a separate row:
WITH t(column, text) AS (
VALUES
('column1', 'text1|text2|text3'),
('column2', 'text3|text4|text4')
)
SELECT t.column, u.item
FROM t, UNNEST(split(t.text, '|')) u(item)
I have a large file of around 130MB containing 10 A characters in each line and \t at the end of 10th "A" character, I want to extract this text file and then change all A's to B's. Can any one help with its code snippet?
this is what I have wrote till now
USE DATABASE imodelanalytics;
#searchlog =
EXTRACT characters string
FROM "/iModelAnalytics/Samples/Data/dummy.txt"
USING Extractors.Text(delimiter: '\t', skipFirstNRows: 1);
#modify =
SELECT characters AS line
FROM #searchlog;
OUTPUT #modify
TO "/iModelAnalytics/Samples/Data/B.txt"
USING Outputters.Text();
I'm new to this, so any suggestions will be helpful ! Thanks
Assuming all of the field would be AAAAAAAAAA then you could write:
#modify = SELECT "BBBBBBBBBB" AS characters FROM #searchlog;
If only some are all As, then you would do it in the SELECT clause:
#modify =
SELECT (characters == "AAAAAAAAAA" ? "BBBBBBBBBB" : characters) AS characters
FROM #searchlog;
If there are other characters around the AAAAAAAAAA then you would use more of the C# string functions to find them and replace them in a similar pattern.
I would like to split pathstr into separate parts how can I do this? See example below.
PS: I'm using octave 3.8.1
dpath='tmp/h1/cli/pls/03sox_a_Fs_1000/'
[pathstr,name,ext] = fileparts(dpath)
>>>pathstr = tmp/h1/cli/pls/03sox_a_Fs_1000
If all I want is 03sox_a_Fs_1000 or pls
How can I do this?
Please note the filenames will change and could be of different lengths.
You can use strsplit (here using Matlab) to split your string (believe it or not!) using the delimiter /:
pathstr = 'tmp/h1/cli/pls/03sox_a_Fs_1000'
[Name,~] = strsplit(pathstr,'/')
Now Name looks like this:
Name =
'tmp' 'h1' 'cli' 'pls' '03sox_a_Fs_1000'
So you can select the last element using the end keyword and curly braces since the output of strsplit is a cell array:
Name = Name{end}
or end-1 to retrieve pls.
This applies to names of any length or format, as long as they are separated by /.
This problem is bugging me and the solution is probably obvious but i cant find it.
I have a bunch of data files which i want to load:
ex_file-1.txt, ex_file-2.txt, ..., ex_file-10.txt
To get their filenames i use:
files = dir('ex_file-*.txt');
This returns a struct with fields name, type, etc. The field name returns:
ex_file-1.txt, ex_file-10.txt, ex_file-2.txt, ..., ex_file-9.txt
I would like to sort this such that ex_file-10.txt is the last file rather than the second.
I have attempted to concatenate, convert to cells and sort but none seem to give what i need. I know that the most obvious solution would be to rename all file names so all strings have the same length but i'd prefer not to do that.
This could be one approach -
%// Input cell array of filenames
names = {'ex_file-1.txt', 'ex_file-10.txt', 'ex_file-2.txt', 'ex_file-3.txt', ...
'ex_file-4.txt', 'ex_file-5.txt'}
%// Reomove the starting common "ex_file" string
stripped_names = strrep(names,'ex_file-','')
%// Remove the ending extension part
stripped_names = strrep(stripped_names,'.txt','')
%// Convert to doubles and then get the sorted indices
[~,idx] = sort(str2double(stripped_names))
%// Use sorted indices to rearrange names array, for the final output
names_out = names(idx)
Code run -
>> names
names =
'ex_file-1.txt' 'ex_file-10.txt' 'ex_file-2.txt' 'ex_file-3.txt' 'ex_file-4.txt' 'ex_file-5.txt'
>> names_out
names_out =
'ex_file-1.txt' 'ex_file-2.txt' 'ex_file-3.txt' 'ex_file-4.txt' 'ex_file-5.txt' 'ex_file-10.txt'
This can be done using regular expressions. The numeric part of file name is detected as a subsequence of numeric characters right before the .txt part.
files = dir('ex_file-*.txt'); %// get file struct array
names = {files.name}; %// get file names. Cell array of strings
numbers = regexp(names, '\d+(?=\.txt)', 'match'); %// strings with numeric part of name
numbers = str2double([numbers{:}]); %// convert from strings to numbers
[~, ind] = sort(numbers); %// sort those numbers
names_sorted = names(ind); %// apply that order to file names
Here is a alternative which does not require any details about the file name. Primary sorting rule shortest first, secondary lexicographic:
%secondary sorting
list=sort(list);
%primary sorting by length
[a,b]=sort(cellfun(#numel,list)):
list=list(b);
I don't know if Matlab can do this, but I want to store some strings in a 4×3 matrix, each element in the matrix is a string.
test_string_01 test_string_02 test_string_03
test_string_04 test_string_05 test_string_06
test_string_07 test_string_08 test_string_09
test_string_10 test_string_11 test_string_12
Then, I want to write this matrix into a plain text file, either comma or space delimited.
test_string_01,test_string_02,test_string_03
test_string_04,test_string_05,test_string_06
test_string_07,test_string_08,test_string_09
test_string_10,test_string_11,test_string_12
Seems like matrix data type is not capable of storing strings. I looked at cell. I tried to use dlmwrite() or csvwrite(), but both of them only accept matrices. I also tried cell2mat() first, but in that way all letters in the strings are comma seperated, like
t,e,s,t,_,s,t,r,i,n,g,_,0,1,t,e,s,t,_,s,t,r,i,n,g,_,0,2,t,e,s,t,_,s,t,r,i,n,g,_,0,3
So is there any way to achieve this?
It is possible to shorten yuk's solution a bit.
strings = {
'test_string_01','test_string_02','test_string_03'
'test_string_04','test_string_05','test_string_06'
'test_string_07','test_string_08','test_string_09'
'test_string_10','test_string_11','test_string_12'};
fid = fopen('output.txt','w');
fmtString = [repmat('%s\t',1,size(strings,2)-1),'%s\n'];
fprintf(fid,fmtString,strings{:});
fclose(fid);
Cell array is the way to store strings.
I agree it's a pain to save strings into a text file, but you can do it with this code:
strings = {
'test_string_01','test_string_02','test_string_03'
'test_string_04','test_string_05','test_string_06'
'test_string_07','test_string_08','test_string_09'
'test_string_10','test_string_11','test_string_12'};
fid = fopen('output.txt','w');
for row = 1:size(strings,1)
fprintf(fid, repmat('%s\t',1,size(strings,2)-1), strings{row,1:end-1});
fprintf(fid, '%s\n', strings{row,end});
end
fclose(fid);
Substitute \t with , to get csv file.
You can also store cell array of strings into Excel file with XLSWRITE (requires COM interface, so it's on Windows only):
xlswrite('output.xls',strings)
In most cases you can use the delimiter ' ' and get Matlab to save a string into file with dlmwrite.
For example,
output=('my_first_String');
dlmwrite('myfile.txt',output,'delimiter','')
will save a file named myfile.txt containing my_first_String.