text-processing
How to strip trailing whitespace in CMake variable?
execute_process has a flag for stripping trailing whitespace on either standard out or standard error (or both) [OUTPUT_STRIP_TRAILING_WHITESPACE] [ERROR_STRIP_TRAILING_WHITESPACE] https://cmake.org/cmake/help/v3.0/command/execute_process.html
How to count the number of words in a sentence, ignoring numbers, punctuation and whitespace?
str.split() without any arguments splits on runs of whitespace characters: >>> s=”I am having a very nice day.” >>> >>> len(s.split()) 7 From the linked documentation: If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain … Read more
How can I loop through blocks of lines in a file?
Here’s another way, using itertools.groupby. The function groupy iterates through lines of the file and calls isa_group_separator(line) for each line. isa_group_separator returns either True or False (called the key), and itertools.groupby then groups all the consecutive lines that yielded the same True or False result. This is a very convenient way to collect lines into … Read more
How can I sum values in column based on the value in another column?
$ awk ‘{a[$1]+=$2}END{for(i in a) print i,a[i]}’ file ABC 80 XYZ 20 MNP 60 DEF 170
Select random lines from a file
Use shuf with the -n option as shown below, to get N random lines: shuf -n N input > output
Using multiple delimiters in awk
The delimiter can be a regular expression. awk -F'[/=]’ ‘{print $3 “\t” $5 “\t” $8}’ file Produces: tc0001 tomcat7.1 demo.example.com tc0001 tomcat7.2 quest.example.com tc0001 tomcat7.5 www.example.com
Using SQL to determine word count stats of a text field
The text handling capabilities of MySQL aren’t good enough for what you want. A stored function is an option, but will probably be slow. Your best bet to process the data within MySQL is to add a user defined function. If you’re going to build a newer version of MySQL anyway, you could also add … Read more
How to replace ${} placeholders in a text file?
Update Here is a solution from yottatsa on a similar question that only does replacement for variables like $VAR or ${VAR}, and is a brief one-liner i=32 word=foo envsubst < template.txt Of course if i and word are in your environment, then it is just envsubst < template.txt On my Mac it looks like it … Read more
How to add a new line of text to an existing file in Java? [duplicate]
you have to open the file in append mode, which can be achieved by using the FileWriter(String fileName, boolean append) constructor. output = new BufferedWriter(new FileWriter(my_file_name, true)); should do the trick