0

I have text files with some information I want to copy from one place and paste somewhere else on the same line. For instance, I have something like:

Random text here { Name = "Tropical Smoothie", UniqueId = "1110100", More random text};
Random text here { Name = "Tropical Smoothie", UniqueId = "1110110", More random text};
Random text here { Name = "Tropical Mango Smoothie", UniqueId = "1110120", More random text};
.
.
.
Random text here { Name = "Tropical Smoothie", UniqueId = "2000110", More random text};

and I want the numbers after UniqueId to be copied and placed at the end of Smoothie on the same line like so

Random text here { Name = "Tropical Smoothie 1110100", UniqueId = "1110100", More random text};
Random text here { Name = "Tropical Smoothie 1110110", UniqueId = "1110110", More random text};
Random text here { Name = "Tropical Mango Smoothie 1110120", UniqueId = "1110120", More random text};
.
.
.
Random text here { Name = "Tropical Smoothie 2000110", UniqueId = "2000110", More random text};

As long as there is a space after the words that are in quotes after Name and then the UniqueId numbers. The name can be anything, the UniqueId is unique for each line.

So using the first line as example, in a text editor I would highlight 1110100 and copy it and put a space after Smoothie then paste. Then I would do the same thing for the next line and so on. Can this task be automated somehow for each line? I'll try any script or Windows program. Even something that does "for each line copy 7 digits after 3rd double-quotes and paste before 2nd double-quotes" or some such would work.

12
  • that text looks suspiciouly... how to say it... a programmer kinda thing. Does fgets() sound familiar to you?, if not, well, I'm sorry, I don't know any Windows program that solves this specific problem. Can you compile code by any chance? Dec 18 '18 at 5:13
  • No I can't compile. This example is text from a .cs file but I'm not looking for a programming solution as I have other text files that I want to be able to do the same thing with.
    – joetex72
    Dec 18 '18 at 6:00
  • What tools do you have avaliable? cmd, python, bash?
    – dmb
    Dec 18 '18 at 11:23
  • Yes I'll try any Windows program, script or app like cmd, Powershell, VBS or download any tool. Not familiar with fgets @JennyT-Type.
    – joetex72
    Dec 18 '18 at 12:04
  • fgets() is a C function that reads a line from a file in put it in a string, I only mention it cause I thought that you might know a little bit of programming, (C is a programming language). I asked you if you can compile cause I was willing to write a program for you to compile in your machine, but setting a cross-compiler just for you, well, not so much. though I'm giving it a though. Dec 18 '18 at 20:13
0

You asked the same question on StackOverflow where I just wrote you an answer. I am supplying the same answer here as well.

You can accomplish what you are asking by using any text editor that supports Regex. In this example, I will be using Notepad++. First I will describe what to do, then after I will explain what the Regex does.

Example

  1. Open the file with Notepad++.
  2. Press Ctrl+F to bring up the Search and Replace window.
  3. Make sure to check the box named Wrap around
  4. Select Regular expression under Search Mode
  5. Under Find What: insert Name = "(.*)", UniqueId = "(\d+)"
  6. Under Replace with: insert Name = "$1 $2", UniqueId = "$2"
  7. Press Replace all once.

Npp-with-regex

Understanding the regex

  • () these characters represent a group that you want to capture.
  • $1 represent the first group that you marked with ()
  • $2 same thing but it takes the second group.
  • \d matches any digit. + after a digit means, match one or more digits.
  • . matches any character. * after the dot means match zero or more characters.

In the search example Name = "(.*)", UniqueId = "(\d+)" we have two groups. Matching the content in between the quotes of Name and UniqueId.

In the replace example Name = "$1 $2", UniqueId = "$2", we are using these groups to replace what we match with something new. The new is, in this case, the contents of group $1 and group $2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.