[英]sed command to replace dots
我有許多腳本,我想在其中掃描特定類型的字符串,並單獨替換這些字符串中的點。 我們正在將我們的定位器策略從 map 替換為其他內容,因此必須將變量從點改為下划線
在下面,我想改變
driver.click(objectMap.getIdentifier(new.dropdown), "DropDown clicking");
至
driver.click(new_dropdown, "DropDown clicking");
所以有兩件事:
objectMap.getIdentifier
如果我嘗試使用sed 's/./_/g' >>
這將替換所有點,我只想替換包含在objectMap.getIdentifier
中的點。 嘗試了這些 sed 命令,但用處不大: 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");
}
}
很丑,它是sed ,它可以工作(只要帶有objectMap.getIdentifier()
的行不包含#
)
#!/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");
}
}
根據 OP 輸入,兩個請求的更改
get_identifier
objectMap.get_identifier("foo.bar.zoo")
,則應刪除引號,並將鍵轉換為帶有 '_' 的符號名稱: foo_bar_zoo
objectMap.getIdentifier(new.dropdown)
,應該轉換為 _ 分隔的 key: new_dropdown
使用 sed 執行此操作很棘手,因為 sed 是面向行的。 很難限制“。”的 scope 替換為單行。 相信 Perl 在這里會做得更好。 不完全在 OP 的約束范圍內(請求的 sed 解決方案)。
第一個“-e”定義了一個 function 來翻譯單個標記(“.”->“_”,刪除引號)第二個“-pe”將 function 應用於輸入。
perl -e 'sub fix { $_ = shift ; s/"//g ; s/\./_/g ; return $_ }' -pe 's/objectMap\.getIdentifier\(("?[a-z.]+"?)\)/fix($1)/e' < a.java
可能更容易創建一個 perl 腳本,做同樣的事情
#! /usr/bin/perl
use strict ;
sub fix { $_ = shift ; s/"//g ; s/\./_/g ; return $_ }
while ( <> ) {
s/objectMap\.getIdentifier\(("?[a-z.]+"?)\)/fix($1)/e ;
print ;
}
Output(用於樣品輸入)。 注意輸入數據中存在的不平衡括號。
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");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.