sed命令替换点 [英] sed command to replace dots

查看:278
本文介绍了sed命令替换点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多脚本,我想在其中扫描特定类型的字符串,并单独替换这些字符串中的点.我们正在将定位器策略从地图替换为其他内容,因此必须将变量从点更改为下划线

I have a number of scripts, in which i want to scan a particular type of string, and replace dots in those strings alone. We are replacing our locator strategy from map to something else, so the variables have to be changed from having dots to having underscore

在下面,我想改变

driver.click(objectMap.getIdentifier(new.dropdown), "DropDown click");

driver.click(new_dropdown, "DropDown click");

所以有两件事:

  1. 删除文本objectMap.getIdentifier
  2. 将包含的字符串从点替换为下划线

如果我尝试使用 sed 's/./_/g' >> 这将替换所有的点,我只想替换包含在 objectMap.getIdentifier.尝试了这些 sed 命令,但没有多大用处:sed -e 's/objectMap.getIdentifier("\(.*\).\(.*\)")/(\1_\2)/pg'

If I try to use sed 's/./_/g' >> This will replace all the dots across, I want to replace only the dots which are enclosed within objectMap.getIdentifier. Tried these sed commands, but not of much use : sed -e 's/objectMap.getIdentifier("\(.*\).\(.*\)")/(\1_\2)/pg'

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

    public class TestingScript {
    public void createNewItems(String itemName){
    driver.click(objectMap.getIdentifier(new.dropdown), "DropDown clicking");
    driver.click((objectMap.getIdentifier("new.dropdown.notes.button"), "Note from template");
    driver.click((getElement(objectMap.getIdentifier("modal.default.template").replace("$Template",  "Default")), "Default", true);
    driver.click((getElementByText(templateName), templateName);
    driver.click(objectMap.getIdentifier(new.dropdown), "DropDown clicking");
    driver.click((objectMap.getIdentifier("modal.create.button"), "Create");
  } 
}

推荐答案

太丑了,是,它有效(只要带有 objectMap.getIdentifier() 的行不包含 #)

It's ugly, it's sed,it works (as long as the lines with objectMap.getIdentifier() don't contain #)

#!/bin/bash

sed '
/objectMap\.getIdentifier(/{
    # copy pattern-space to hold-space
      h;
    # replace pattern-space with only the contents inside objectMap.getIdentifier()
      s/^.*objectMap\.getIdentifier(\([^)]*\)).*$/\1/;
    # replace all dots with underscores
      s/\./_/g;
    # swap pattern-space with hold-space
      x;
    # Replace objectMap.getIdentifier(.*) with a "#"
      s/objectMap\.getIdentifier([^)]*)/#/;
    # Append hold-space to pattern-space (with new-line between)
      G;
    # parse out the contents we want, use "#" as key for replacement
      s/^\([^#]*\)#\(.*\)\n\(.*\)$/\1\3\2/
}' ./infile

概念证明

$ sed '/objectMap\.getIdentifier(/{h;s/^.*objectMap\.getIdentifier(\([^)]*\)).*$/\1/;s/\./_/g;x;s/objectMap\.getIdentifier([^)]*)/#/;G;s/^\([^#]*\)#\(.*\)\n\(.*\)$/\1\3\2/}' ./infile
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

    public class TestingScript {
    public void createNewItems(String itemName){
    driver.click(new_dropdown, "DropDown clicking");
    driver.click(("new_dropdown_notes_button", "Note from template....");
    driver.click((getElement("modal_default_template".replace("$Template",  "Default")), "Default", true);
    driver.click((getElementByText(templateName), templateName);
    driver.click(new_dropdown, "DropDown clicking");
    driver.click(("modal_create_button", "Create");
  }
}

这篇关于sed命令替换点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆