How to compress multiple lines into single/fewer lines using regular expression or wildcard character?

61 views Asked by At

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.

1

There are 1 answers

0
lorvindc On

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