number of tokens in bash variable

The $# expansion will tell you the number of elements in a variable / array. If you’re working with a bash version greater than 2.05 or so you can: VAR=’some string with words’ VAR=( $VAR ) echo ${#VAR[@]} This effectively splits the string into an array along whitespace (which is the default delimiter), and then … Read more

Splitting one csv into multiple files

In Python Use readlines() and writelines() to do that, here is an example: >>> csvfile = open(‘import_1458922827.csv’, ‘r’).readlines() >>> filename = 1 >>> for i in range(len(csvfile)): … if i % 1000 == 0: … open(str(filename) + ‘.csv’, ‘w+’).writelines(csvfile[i:i+1000]) … filename += 1 the output file names will be numbered 1.csv, 2.csv, … etc. From … Read more

How is Guava Splitter.onPattern(..).split() different from String.split(..)?

You found a bug! System.out.println(s.split(“abc82”)); // [abc, 8] System.out.println(s.split(“abc8”)); // [abc] This is the method that Splitter uses to actually split Strings (Splitter.SplittingIterator::computeNext): @Override protected String computeNext() { /* * The returned string will be from the end of the last match to the * beginning of the next one. nextStart is the start position … Read more

Splitting a string into an iterator

Not directly splitting strings as such, but the re module has re.finditer() (and corresponding finditer() method on any compiled regular expression). @Zero asked for an example: >>> import re >>> s = “The quick brown\nfox” >>> for m in re.finditer(‘\S+’, s): … print(m.span(), m.group(0)) … (0, 3) The (4, 9) quick (13, 18) brown (19, … Read more

Splitting strings through regular expressions by punctuation and whitespace etc in java

You have one small mistake in your regex. Try this: String[] Res = Text.split(“[\\p{Punct}\\s]+”); [\\p{Punct}\\s]+ move the + form inside the character class to the outside. Other wise you are splitting also on a + and do not combine split characters in a row. So I get for this code String Text = “But I … Read more

tech