Shell – SED challenge,aggregating String containing braces

replacesedshell-script

I've been trying to use sed for aggregating a specific string pattern within a text for hours, but can't find the solution. I hope you guys know how to do it!?

  • The text subject to my Goal consists of string characters only (No invisible \t present).
  • I want to find string parts which contain (TAB), but at least two next to each other and maximum 8 adjacent to each other and replace them by a single (TAB) entry.

  • The search shall be performed within a Shell file, containing a bash script

Example:

#!/bin/bash

text="Column One(TAB)(TAB)(TAB)Column Two(TAB)(TAB)Column three(TAB)Column4"

modText=`echo $text | sed 's/([(]\{1\}TAB[)]\{1\})\{2,8\}/(TAB)/g'`
  • I have tried several Versions of the sed-command, the one above is just one of them.
    My original idea was
    modText=`echo $text | sed 's/\(TAB\)\{1\})\{2,8\}/(TAB)/g'`

Would be really great if you could help me out. Got the Feeling that the solution is not so far away but just don't have any more ideas and my research didn't do it :-S


Text Example

"Column One(TAB)(TAB)(TAB)Column Two(TAB)(TAB)Column three(TAB)Column4"

My search criteria would be matched by the first two (TAB) Groups, occurring
between "Column One" and "Column three".

The result shall look like this:

"Column One(TAB)Column Two(TAB)Column three(TAB)Column4"

Best Answer

I'm not sure I understand how the "maximum 8" clause is supposed to apply, but the naive approach would be something like this:

sed 's/\((TAB)\)\{2,8\}/(TAB)/g'
Related Question