Replace Multiple Strings with Other Strings in Command Line

I have a command in a Windows batch script that outputs a lot of text, but I need to replace certain strings with others. How can I do that?

"script\path\script.exe" "file\path\document.ext"

Outputs:

In diam purus B.E., cursus a convallis vitae, semper sit amet velit. Nulla pellentesque suscipit erat ac placerat. Curabitur non porttitor diam, condimentum rutrum sapien. Maecenas hendrerit, nisl a viverra volutpat, magna mi finibus mi, quis ultrices diam dui id orci. Etiam pharetra velit at diam faucibus, id rhoncus magna aliquam. Sed vel felis sit amet lacus bibendum rhoncus eu vel tellus. Sed eget dictum libero. Duis malesuada eros neque, vitae consectetur ipsum commodo et. Maecenas non erat ac risus ultricies congue. Aenean erat mauris, cursus a erat at, tristique fringilla lectus. Morbi ac gravida ex, et accumsan magna. Aenean dictum ultrices tellus, quis hendrerit nibh accumsan id. Aenean et est suscipit, blandit tellus sit amet, convallis magna. Ut porta semper condimentum. Vivamus blandit massa hendrerit, blandit erat at, ultricies leo. Quisque ultricies orci vel arcu elementum, ac auctor tortor finibus.

But I’d like to change all “. ” => “.\r\n” and “, ” => “,\r\n”

In diam purus B.E.,\r\ncursus a convallis vitae,\r\nsemper sit amet velit.\r\nNulla pellentesque suscipit erat ac placerat.\r\nCurabitur non porttitor diam,\r\ncondimentum rutrum sapien.\r\nMaecenas hendrerit,\r\nnisl a viverra volutpat,\r\nmagna mi finibus mi,\r\nquis ultrices diam dui id orci.\r\nEtiam pharetra velit at diam faucibus,\r\nid rhoncus magna aliquam.\r\nSed vel felis sit amet lacus bibendum rhoncus eu vel tellus.\r\nSed eget dictum libero.\r\nDuis malesuada eros neque,\r\nvitae consectetur ipsum commodo et.\r\nMaecenas non erat ac risus ultricies congue.\r\nAenean erat mauris,\r\ncursus a erat at,\r\ntristique fringilla lectus.\r\nMorbi ac gravida ex,\r\net accumsan magna.\r\nAenean dictum ultrices tellus,\r\nquis hendrerit nibh accumsan id.\r\nAenean et est suscipit,\r\nblandit tellus sit amet,\r\nconvallis magna.\r\nUt porta semper condimentum.\r\nVivamus blandit massa hendrerit,\r\nblandit erat at,\r\nultricies leo.\r\nQuisque ultricies orci vel arcu elementum,\r\nac auctor tortor finibus.

Computability – The language of all strings of even length

Define a language $ L$ as follows: $ $ L = \{y \in \{0, 1\}^* | y = code(M) \text{ for some TM M and M halts on all strings of even length} \}$ $

I can prove that $ L$ is not decidable/recursive, but is it recognisable/recursively enumerable? My intuition is that $ L$ is unrecognisable because suppose there exists a TM $ N$ that recognises $ L$ , $ N$ would have to test all strings of even length which is countably infinite.

I have tried reducing $ HALT^-$ to $ L$ but not succeeded. Is it unrecognisable or not? How would you prove it?

Thanks.

Bubble sort in C (for strings)

What do you think of this bubble sorting function in C, for a string array? I would like to improve the readability, performance, and any good practices.

void         sort_entries(char **entries, int reverse) {     char     **next;     char     **previous;     char     *t;     int      value;      previous = tab;     while (*(next = previous + 1))     {         value = strcasecmp(*previous, *next);         if ((!reverse && value > 0)             || (reverse && value < 0))         {             t = *previous;             *previous = *next;             *next = t;             previous = entries;             continue ;         }         previous++;     } } 

Why do numpy string arrays cut strings when they see a forward slash?

I’m processing some text data with python, making charts, etc. I’m using numpy string arrays as it’s the framework i’m the most comfortable with.

I encountered a weird behaviour with forward slashes. When trying to get a string containing a forward slash inside a numpy array, it is cut at the slash and the rest disappears. See example below:

import numpy  a = numpy.array(["one","two","three"]) a[0] = "test/case" print(a) 

This gets me:

['test/','two','three']

What’s happening here, can I fix it ? Thank you !

How to escape regexes and paths in double quote strings without going crazy?

It often happens that after designing my regexp (on regex101.com) I want to paste it in my program. Consider this regexp that matches numbers and string (but keep in mind this is general question!):

^(\"(?:[^\"]|\\")*\"|\-?[0-9]+(?:\.[0-9]+)?)$  

I overlined all characters that need to be escaped before pasting them into languages that use " for strings.

Needless to say, doing this manually drives me crazy. I face this problem both at work with C++ project and at home with Java and JavaScript projects.

How can I deal with this efficiently?

What is the most semantic way of sending a large array of strings to an API

I’m building a simple app that is a client web application and an API behind it. I’m combining two third-party APIs, one needs to be called from my client application (for ease of authentication) and one needs to be called by the server.

The data I receive from my first API is a list of strings – it will be between 0 and 150 items long, each string is an unknown length. Each one of these items needs to be passed to my server – either as individual elements (preferred) or as a serialised string. My endpoint will always return a single object, regardless of how many items are passed to it.

What is the most semantic way of passing this data to my API?

It’s not really a GET request as the returned object is dynamic, and I’m concerned about the URL length limits (discussed here) given that I don’t know what these strings will look like.

POST also feels incorrect as I’m not going to storing what is sent to me, though this would allow me to send the data in the body of the request and not worry about the size.

Perhaps I shouldn’t worry so much about semantics and just do what works, but I’m interested in opinions on the best, or ‘proper’, way to design my API here.

Redirection Plugin: Change permalink structure to remove date, want to retain and pass query strings (?utm_)

I have changed the permalink structure from /%year%/%monthnum%/%postname%.html to /%postname%/

I have added the following regex rule to the Redirection plugin:

Source URL: ^/([0-9]{4})/([0-9]{2})/([^/]+).html(.*)$   Target URL: https://example.com/$  3/ 

This works and redirects links from social media appended with ?utm_medium=social&utm_source=pinterest etc., but strips that parameter from the target.

How may I edit my regex to have it pass the query string and append it to the new URL format?

Thank you.

GUI that searches files using user inputted search strings. Trying to speed up the REGEX search

Basically, I’ve got a GUI where the user can select text files, enter in strings (that may or may not contain wildcards) and search those files for those strings.

Currently, I take the user inputted string(s) and divide them into two groups: regularly searchable strings, and strings that have wildcards (either * for 1 wildcard character, or .* for any amount).

If they are regularly searchable strings, then I use the normal str.find function (as I tested this vs regex_search and this is faster), otherwise I use the regex_search function.

The main problem is performance. As a benchmark comparison, it takes my program roughly 47 minutes and it is searching through 5,028,712 lines. From trying to figure this problem out on google it seems this entire search should take me well under a minute…

searchAllFilesForAllStrings -> bool checkbox on the GUI where if it is set to true, the program will just search every file for every string, and false, will only search each “batch” of files (if the filepath includes a wildcard, such as “Read*.txt -> all files starting with Read and that are .txt files will be chosen)

Global variables:

vector<string> regex_patterns; vector<string> excelFiles; vector<string> nonWildCardSearchStrings; vector<string> searchStrings; vector<string> searchFiles; vector<bool> regexIndex; 

This function gets called from a GUI button using an editable text box that contains the path to the file. It essentially grabs strings and files from an excel file formatted with 2 columns, one column search strings, and one column search files. In search strings, XYY denotes 1 wildcard (because the user should be able to search for a * if they desire; XYZ denotes any # of wildcards):

ifstream excelFile;     string line;     string delimiter = ",";     int bIdxStrings = 0;     int bIdxFiles = 0;     for (int i = 0; i < excelPath.size(); i++)     {         excelFile.open(excelPath.at(i));         if (excelFile.is_open()) {}         else { return false; }         int index = 0;         while (getline(excelFile, line))          {             searchStrings.push_back(line.substr(0, line.find(delimiter)));             searchFiles.push_back(line.substr(line.find(delimiter) + 1));             index = index + 1;         }         searchStrings.erase(searchStrings.begin() + bIdxStrings);         searchFiles.erase(searchFiles.begin() + bIdxFiles);         bIdxStrings = searchStrings.size() + 1;         bIdxFiles = searchFiles.size() + 1;         for (int i = 0; i < searchStrings.size(); i++)          {             searchStrings.at(i) = addEscapes(searchStrings.at(i));         }     }     excelFile.close();     string key = "\";     size_t foundLast = 0;     string wcPath = "";     tuple<vector<string>, string> addWildCardFiles;     vector<string>test;     string holdTemp = "";     string regSearch = "";     string regtemp = "";     string fullPath = "";     vector<string>tempFiles;     vector<string>tempStrings;     // Search for wildcard paths. if any exist, find files based on their main directory. will not recursively search.     for (int i = 0; i < searchFiles.size();i++)     {         size_t found = searchFiles.at(i).find("*");         if (found != string::npos)         {             // temporarily hold the search string corresponding to this entry.             holdTemp = searchStrings.at(i);             foundLast = searchFiles.at(i).rfind(key);             wcPath = searchFiles.at(i).substr(0, foundLast);             regSearch = searchFiles.at(i).substr(foundLast + 1, string::npos);              regtemp = regSearch.substr(0, regSearch.find("*"));             regtemp.append(".*");             regtemp.append(regSearch.substr(regSearch.find("*") + 1, string::npos));             regSearch = regtemp;             smatch matchez;             // Should make regex search case insensitive.             regex e(regSearch, regex_constants::icase);             //searchFiles.erase(searchFiles.begin() + i);             //searchStrings.erase(searchStrings.begin() + i);             // All files in the directory:             addWildCardFiles = read_directory(wcPath, test);             for (int m = 0; m < get<0>(addWildCardFiles).size(); m++)             {                 size_t wcBool = regex_search(get<0>(addWildCardFiles)[m], matchez, e);                 if (wcBool == 1)                 {                     fullPath.append(wcPath); fullPath.append("\");                     fullPath.append(get<0>(addWildCardFiles)[m]);                     tempFiles.push_back(fullPath);                     tempStrings.push_back(holdTemp);                 }                 fullPath = "";             }         }     }      searchStrings.insert(searchStrings.end(), tempStrings.begin(), tempStrings.end());     searchFiles.insert(searchFiles.end(), tempFiles.begin(), tempFiles.end());     sort(searchStrings.begin(), searchStrings.end());     sort(searchFiles.begin(), searchFiles.end());     searchFiles.erase(unique(searchFiles.begin(), searchFiles.end()), searchFiles.end());     if (searchAllFilesForAllStrings == true)     {         searchStrings.erase(unique(searchStrings.begin(), searchStrings.end()), searchStrings.end());     }      int setNext = -1;     vector<int> filesRepeat;     vector<int> stringsRepeat;     size_t stringsCount = 0;     size_t filesCount = 0;      // Loops to get rid of duplicate search strings + duplicate files.     // Dont get rid of duplicates if only searching each file for each subsequent string because of how the code is structured;     for (int i = 0; i < searchStrings.size(); i++) { if (searchStrings.at(i).compare("Search Strings") == 0) { searchStrings.erase(searchStrings.begin() + i); } }     for (int i = 0; i < searchFiles.size(); i++) { if (searchFiles.at(i).compare("Search Files") == 0) { searchFiles.erase(searchFiles.begin() + i); } }      // Loops to get rid of wildstar patterns that are included (these can't be searched)     int idx = 0;     int startCount = searchFiles.size();     while (idx < startCount)     {         if (contains(searchFiles.at(idx), "*", 1) == 1)         {             searchFiles.erase(searchFiles.begin() + idx);             startCount = startCount - 1;             idx = 0;         }         idx = idx + 1;     }      // Loop to deal with each search string and format it for regex searching later.     // only pull strings that are non wildcard containing. everything else can be normally searched which will save time.     for (unsigned int jj = 0; jj < searchStrings.size(); jj++)     {          if (contains(searchStrings.at(jj), "XYY", 0) == 1 || contains(searchStrings.at(jj), "XYZ", 0) == 1)         {             regex_patterns.push_back(replaceWildCards(searchStrings.at(jj)));             regexIndex.push_back(true);         }          else         {             nonWildCardSearchStrings.push_back(searchStrings.at(jj));             regexIndex.push_back(false);         }        }     return true; 

found and nonwcfound are the variables used to check if a match was found and subsequently to save the line text and line number in vectors. file name and folder name are variables that will also be saved to be outputted to the file.

        size_t found;         bool nonwcfound = false;         smatch matches;         vector<regex> expressions;         for (int i = 0; i < regex_patterns.size(); i++) { expressions.emplace_back(regex_patterns.at(i)); }          if (searchAllFilesForAllStrings == true)         {             ofstream myOutPut;             myOutPut.open(outputFilePath, std::ofstream::out | std::ofstream::app);             myOutPut << "Line Text, Line Number, File Name, Folder Path," << "\n";             myOutPut.close();             for (size_t j = 0; j < searchFiles.size();j++)             {                 // Initialize variables for line number + text                 vector<int> lineNumber;                 vector<string> lineText;                 vector<string>lineStrings;                 string entireFile;                  // Get file and folder name for storage.                 string fileName;                 string folderName;                 fileName = searchFiles.at(j);                 int fileNameSlashIdx = fileName.rfind("\");                 folderName = fileName.substr(0, fileNameSlashIdx);                 fileName = fileName.substr(fileNameSlashIdx + 1, string::npos);                  // File ifstream definition/opening                 ifstream file;                 file.open(searchFiles.at(j), ios::in | ios::ate);                  // Fill and close file                 if (file)                 {                     ifstream::streampos filesize = file.tellg();                     entireFile.reserve(filesize);                     file.seekg(0);                     while (!file.eof())                     {                         entireFile += file.get();                     }                 }                 file.close();                 int linecount = 0;                  stringstream stream(entireFile);                 while (1)                 {                     string line;                     getline(stream, line);                     if (!stream.good())                         break;                     for (size_t r = 0; r < expressions.size(); r++)                     {                         found = regex_search(line, matches, expressions.at(r));                         if (found == 1)                         {                             lineNumber.push_back(linecount);                             lineText.push_back(line);                         }                     }                     for (size_t rr = 0; rr < nonWildCardSearchStrings.size(); rr++)                     {                         nonwcfound = contains(line, nonWildCardSearchStrings.at(rr), 0);                         if (nonwcfound == true)                         {                             lineNumber.push_back(linecount);                             lineText.push_back(line);                         }                     }                     linecount = linecount + 1;                 }                  entireFile.clear();                 ofstream myOutPut;                 myOutPut.open(outputFilePath, std::ofstream::out | std::ofstream::app);                 {                     tuple<vector<string>, vector<int>, string, string>result = make_tuple(lineText, lineNumber, fileName, folderName);                     writeResultsToFile(result, outputFilePath);                 }                 myOutPut.close();             }         }         if (searchAllFilesForAllStrings == false)         {                                    // Do the same thing as above, except that it will search each file/batch of files only with the                      // subsequent search string in the same row of the excel file that is read in using the above function.         }         MessageBox::Show("Finished execution. Your file is now available for viewing!", "Output Excel File Written"); 

Efficient way to add a colon before and after every word in strings inside a Series

I was working with twitter data, and extracted every emoji from the data. But, when I passed that data through CountVectorizer, colons where substracted from the strings. So, the string emoji :ok_hand: :thumbs_up: turned into ok_hand thumbs_up. I wanted to re-add those colons so then I could emojize them back. I managed to do that, but I’m quite sure my method is very inefficient. The emojis are the indexes of a coefficients DataFrame, like this:

    index              coef  ok_hand thumbs_up      0.4     airplane           0.2 

So what I did was this:

to_emojize=pd.Series(coef_mat_emoji.index) to_emojize=to_emojize.apply(lambda x: x.split()) to_emojize=to_emojize.apply(lambda x:[':'+i+':' for i in x]) to_emojize=to_emojize.apply(lambda x: emoji.emojize(x, use_aliases=True)) coef_mat_emoji.index=to_emojize 

Is there a better way to do this?