TSV String, Filter Out by Text

TSV String, Filter Out by Text
TSV String, Filter Out by Text

Filters out TSV lines by any search text (Exclude Filter). Only lines that DO NOT contain the search text in the specified column are output. If OR search with multiple texts, specify them as a comma-separated list such as “USA,U.S.,United States”.

2019-12-26 (C) Questetra, Inc. (MIT License)
https://support.questetra.com/addons/tsv-string-filter-out-by-text/

Configs
  • A: Select STRING DATA for TSV String *
  • B-target: Set Column ID to be Searched (eg “0” ) * #{EL}
  • B-texts: Set Search Texts (eg “USA” “USA,U.S.” ) * #{EL}
  • C: Select STRING DATA for Filterd TSV (update) *
Script
// (c) 2019, Questetra, Inc. (the MIT License)
// Notes:
// Cannot specify search terms that contain a comma.
// Special characters in search text settings have no meaning (literal string).
// Notes(ja):
// カンマを含む検索語は指定できません
// 検索テキスト設定の特殊文字に意味はありません(リテラル文字列)


//////// START "main()" /////////////////////////////////////////////////////////////////
main();
function main(){ 

//// == Config Retrieving / 工程コンフィグの参照 ==
const dataIdA     = configs.get( "conf_DataIdA" ) + "";     // config required
const targetColId = configs.get( "conf_TargetColId" ) + ""; // config required
const searchTexts = configs.get( "conf_SearchTexts" ) + ""; // config required
const dataIdC     = configs.get( "conf_DataIdC" ) + "";     // config required

if( targetColId === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {Column ID} is empty \n" );
}
if( searchTexts === "" ){
  throw new Error( "\n AutomatedTask ConfigError:" +
                   " Config {Search Texts} is empty \n" );
}
const numTargetColId = parseInt( targetColId );


//// == Data Retrieving / ワークフローデータの参照 ==
const inputString = engine.findDataByNumber( dataIdA ) + "";
const arrInputString = inputString.split("\n");
engine.log( " AutomatedTask MultilineString:" + 
            " TSV {A}, number of lines: " + arrInputString.length );


//// == Calculating / 演算 ==
let   outputString = "";
const arrSearchTexts = searchTexts.split(",");
let   arrSearchRegExp = [];
for( let i = 0; i < arrSearchTexts.length; i++ ){
  engine.log( " AutomatedTask Search Text[" + i +"]: " + arrSearchTexts[i] );
  if( arrSearchTexts[i] === "" ){
    throw new Error( "\n AutomatedTask ConfigError:" +
                     " Config {Search Text[" + i +"]} is empty \n" );
  }
  let contain = new RegExp( arrSearchTexts[i]
                            .replace(/[.*+?^=!:${}()|[\]\/\\]/g, '\\$&')
                          ); // Escaping for a literal string search
  // For a wider range of values, comment out and use Patterns with special characters
  arrSearchRegExp.push( contain );
}

let   searchWord;
for( let i = 0; i < arrInputString.length; i++ ){
  searchWord = "notExist";
  let arrTsvLine = arrInputString[i].split("\t");
  for( let j = 0; j < arrSearchRegExp.length; j++ ){
    if( arrSearchRegExp[j].test( arrTsvLine[ numTargetColId ] ) ){
      searchWord = "exists";
      break;
    }
  }
  if( searchWord === "notExist" ){
    outputString += arrInputString[i];
    outputString += "\n";
  }
}
outputString = outputString.replace(/[\n]*$/, "");


//// == Data Updating / ワークフローデータへの代入 ==
if( outputString === "" ){
  engine.log( " AutomatedTask DataUpdating: Output Text empty" );
}
engine.setDataByNumber( dataIdC, outputString );

} //////// END "main()" /////////////////////////////////////////////////////////////////

Download

Capture

Filters out TSV lines by any search text (Exclude Filter). Only lines that DO NOT contain the search text in the specified column are output. If OR search with multiple texts, specify them as a comma-separated list such as "USA,U.S.,United States".

Notes

  1. Cannot specify search terms that contain a comma.
  2. Special characters in search text settings have no meaning (literal string).

See also

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: