-2

I want to compress multiple lines of string having some common elements into single or fewer lines by replacing the changing values with wildcard character.

Example

Input:

Lorem ipsum dolor sit amet_0
Lorem ipsum dolor sit amet_1
Lorem ipsum dolor sit amet_2
Cursus risus at ultrices mi tempus imperdiet
Cursus risus at scelerisque mi tempus imperdiet

Output:

Lorem ipsum dolor sit amet_*
Cursus risus at * mi tempus imperdiet

Is there any tool that I can use for this purpose ? If not, what are the ways to achieve this? Thanks

I tried finding any tool which does this but didn't find any.

InSync
  • 4,851
  • 4
  • 8
  • 30
Saurabh
  • 1
  • 1
  • You gave us the details of the problem, but you said nothing about your own efforts. What have you tried and what went wrong? – InSync Aug 16 '23 at 15:11
  • I just want to know if there is any tool out there which can do this. I tried searching on google phrasing this question in multiple ways to see if anything exist for this or not, but I didn't find anything (which I've already mentioned in the question). I don't want to spend days doing something for which the solution already exists. That's all I want to know. – Saurabh Aug 16 '23 at 15:20
  • Please check demo1-https://regex101.com/r/aPo9ZZ/1 and demo2- https://regex101.com/r/ZbSASh/1. Would these server your purpose ? – novice Aug 16 '23 at 16:44
  • @novice I want reverse of this. Meaning, I don't want to write regex, I want it as output based on input data. – Saurabh Aug 16 '23 at 19:36

1 Answers1

0

I am not sure if you have already found a tool to do this for you. But if I would do this, I think I would use VSCode to find and replace the regex and then remove the duplicates.

Generic step/logic:

  • Find all common lines, using regex replace strings with wildcard characters
  • Using regex, remove duplicate line (If you want, you may also want to check if there are other IDE or editors that can remove the duplicates for you)

To do this using VSCode, you may do the following:

  1. Control+F
  2. Toggle "Replace mode"
  3. Toggle "Use Regular Expression" (the icon with the .* symbol)
  4. In the search field, type Lorem ipsum dolor sit amet_(.*)
  5. In the "replace with" field, type Lorem ipsum dolor sit amet_*
  6. Click the Replace All button
  7. In the search field, type ^(.*)(\n\1)+$
  8. In the "replace with" field, type $1
  9. Click the Replace All button

Repeat the same steps for the other lines:

  • Search: Cursus risus at (.*) mi tempus imperdiet
  • Replace With: Cursus risus at * mi tempus imperdiet
lorvindc
  • 69
  • 1
  • 7