多个条件+ awk命令中的正则表达式
multiple conditions + regex in awk command
我正在使用以下 awk 命令替换 swift 源文件中的字符串:
awk '
BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; len=length(old) }
s=index([=10=],old) { [=10=] = substr([=10=],1,s-1) new substr([=10=],s+len) }
{ print }
' "$old" "$new" "$file" > ./temp
尽量不要编辑注释掉的值。至少,需要忽略以“//”开头的行,但似乎可以忽略行内注释(例如,当该行仅被部分注释时,如 "MATCH // <- Ok"
或 "foo // MATCH <- Not Ok"
)。
类似...
s=index([=11=],old) && !([=11=] =~ "^//") { ... }
示例输入:
old="\"Some \(value) with %@ special \n characters\""
new="\"some_key\".localized"
file {contents}...
/// - Returns: "Some \(value) with %@ special \n characters"
static let someValue = "Some \(value) with %@ special \n characters" // <-- This should change
static let otherValue = "This line does NOT change" // "Some \(value) with %@ special \n characters"
预期输出:
/// - Returns: "Some \(value) with %@ special \n characters"
static let someValue = "some_key".localized // <-- This should change
static let otherValue = "This line does NOT change" // "Some \(value) with %@ special \n characters"
编辑
虽然@RavinderSingh13 的回答与预期的输出不匹配,但它很接近,我用它来修改我的命令,如下所示:
BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; len=length(old) }
s=index([=14=],old) { if (!(match([=14=],/.*\/\//))) [=14=] = substr([=14=],1,s-1) new substr([=14=],s+len) }
{ print }' "$old" "$new" "$file"
这符合最初的要求,但忽略了任何有两个斜杠的行。这是有问题的,因为它不支持行注释(例如,上面的命令不会编辑任何示例输入;除非删除“// <-- This should change”注释。如果没有人回复,我我会使用这个作为答案,但我会等一天左右,以防有人发布满足所有要求的命令版本。将接受该答案。
会是这样的...
s=index([=15=],old) { if (!(match([=15=],/.*\/\//)) || (match([=15=],/"$old".*\/\//))) [=15=] = substr([=15=],1,s-1) new substr([=15=],s+len) }
考虑到您想要跳过所有从 //
开始的行,并且您还想打印 //
之前的内容以用于内联注释之间。由于未提供样品,因此未进行公平警告。
awk '
BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; len=length(old) }
/^\/\//{ next }
match([=10=],/.*\/\//){ [=10=] = substr([=10=],RSTART,RLENGTH-2) }
s=index([=10=],old) { [=10=] = substr([=10=],1,s-1) new substr([=10=],s+len) }
{ print }
' "$old" "$new" "$file" > ./temp
上面将忽略以 //
开头的行,如果你想打印它们然后执行以下操作。
awk '
BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; len=length(old) }
/^\/\//{ print; next }
match([=11=],/.*\/\//){ [=11=] = substr([=11=],RSTART,RLENGTH-2) }
s=index([=11=],old) { [=11=] = substr([=11=],1,s-1) new substr([=11=],s+len) }
{ print }
' "$old" "$new" "$file" > ./temp
只在每行任何评论开始之前的部分寻找"old",例如:
awk '
BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; len=length(old) }
{ preCmt = [=10=]; sub("//.*","", preCmt) }
s=index(preCmt,old) { [=10=] = substr([=10=],1,s-1) new substr([=10=],s+len) }
{ print }
' "$old" "$new" file
/// - Returns: "Some \(value) with %@ special \n characters"
static let someValue = "some_key".localized // <-- This should change
static let otherValue = "This line does NOT change" // "Some \(value) with %@ special \n characters
我正在使用以下 awk 命令替换 swift 源文件中的字符串:
awk '
BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; len=length(old) }
s=index([=10=],old) { [=10=] = substr([=10=],1,s-1) new substr([=10=],s+len) }
{ print }
' "$old" "$new" "$file" > ./temp
尽量不要编辑注释掉的值。至少,需要忽略以“//”开头的行,但似乎可以忽略行内注释(例如,当该行仅被部分注释时,如 "MATCH // <- Ok"
或 "foo // MATCH <- Not Ok"
)。
类似...
s=index([=11=],old) && !([=11=] =~ "^//") { ... }
示例输入:
old="\"Some \(value) with %@ special \n characters\""
new="\"some_key\".localized"
file {contents}...
/// - Returns: "Some \(value) with %@ special \n characters"
static let someValue = "Some \(value) with %@ special \n characters" // <-- This should change
static let otherValue = "This line does NOT change" // "Some \(value) with %@ special \n characters"
预期输出:
/// - Returns: "Some \(value) with %@ special \n characters"
static let someValue = "some_key".localized // <-- This should change
static let otherValue = "This line does NOT change" // "Some \(value) with %@ special \n characters"
编辑
虽然@RavinderSingh13 的回答与预期的输出不匹配,但它很接近,我用它来修改我的命令,如下所示:
BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; len=length(old) }
s=index([=14=],old) { if (!(match([=14=],/.*\/\//))) [=14=] = substr([=14=],1,s-1) new substr([=14=],s+len) }
{ print }' "$old" "$new" "$file"
这符合最初的要求,但忽略了任何有两个斜杠的行。这是有问题的,因为它不支持行注释(例如,上面的命令不会编辑任何示例输入;除非删除“// <-- This should change”注释。如果没有人回复,我我会使用这个作为答案,但我会等一天左右,以防有人发布满足所有要求的命令版本。将接受该答案。
会是这样的...
s=index([=15=],old) { if (!(match([=15=],/.*\/\//)) || (match([=15=],/"$old".*\/\//))) [=15=] = substr([=15=],1,s-1) new substr([=15=],s+len) }
考虑到您想要跳过所有从 //
开始的行,并且您还想打印 //
之前的内容以用于内联注释之间。由于未提供样品,因此未进行公平警告。
awk '
BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; len=length(old) }
/^\/\//{ next }
match([=10=],/.*\/\//){ [=10=] = substr([=10=],RSTART,RLENGTH-2) }
s=index([=10=],old) { [=10=] = substr([=10=],1,s-1) new substr([=10=],s+len) }
{ print }
' "$old" "$new" "$file" > ./temp
上面将忽略以 //
开头的行,如果你想打印它们然后执行以下操作。
awk '
BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; len=length(old) }
/^\/\//{ print; next }
match([=11=],/.*\/\//){ [=11=] = substr([=11=],RSTART,RLENGTH-2) }
s=index([=11=],old) { [=11=] = substr([=11=],1,s-1) new substr([=11=],s+len) }
{ print }
' "$old" "$new" "$file" > ./temp
只在每行任何评论开始之前的部分寻找"old",例如:
awk '
BEGIN { old=ARGV[1]; new=ARGV[2]; ARGV[1]=ARGV[2]=""; len=length(old) }
{ preCmt = [=10=]; sub("//.*","", preCmt) }
s=index(preCmt,old) { [=10=] = substr([=10=],1,s-1) new substr([=10=],s+len) }
{ print }
' "$old" "$new" file
/// - Returns: "Some \(value) with %@ special \n characters"
static let someValue = "some_key".localized // <-- This should change
static let otherValue = "This line does NOT change" // "Some \(value) with %@ special \n characters