awk bash 重复模式
awk bash repeat pattern
我目前有这个原始字符串:
mklink .\Oracle\MOLT_HYB_010_Header\pkg_cdc_util.pks $.\Oracle\MOLT_HYB_010_Header\pkg_cdc_util.pks
#.......................................................^
.....需要替换如下:
mklink .\Oracle\MOLT_HYB_010_Header\pkg_cdc_util.pks ..\..\..\..\.\Oracle\MOLT_HYB_010_Header\pkg_cdc_util.pks
#.......^......^...........^..........^.................^^^^^^^^^^^^
即根据原始字符串 (".\Oracle\MOLT_HYB_010_Header\pkg_cdc_util.pks")
第 2 列中斜杠的数量,将 $ 替换为“..\” 4 次]
例如,我可以单独执行以下操作:
awk -F '\' '{print NF-1}'
--> 打印反斜杠出现的次数
sed -e "s,\\,$(printf '..\\%.0s' {1..4}),g"
--> 替换并重复字符串模式
......但不确定如何在 1 个命令行中串起来。
我会使用perl,虽然这有点"clever"
perl -ane '
$n = ($F[1] =~ tr/\/\/);
$F[2] =~ s{$}{"..\" x $n}e;
print join(" ", @F);
' file
哪里
-a
- 将行自动拆分为数组 @F
-n
- 为 "file" 的每一行执行正文
$F[1] =~ tr/\/\/
- 计算第二个字段中反斜杠的数量
$F[2] =~ s{$}{"..\" x $n}e
- 将美元替换为正确的数字“..\”
awk 一行,假设只有一行要处理
awk -F\ -v RS='$' -v ORS='' '{print} NR==1{for(i=1; i< NF; i++) printf("..\")}'
说明
awk -F\ -v RS='$' ' -v ORS='' ' # Specify separators
{print} # print the line
NR==1{ # for first record
for (i=1; i<NF; i++) # print multiple ..\
printf("..\") # need to escape \
}
'
一个awk
命令,可以处理任意数量的输入行(具有相同的字段结构):
awk -v prefixUnit="..\" '{
count = gsub("\\", "\", ) # count the number of "\"s
# Build the prefix composed of count prefixUnit instances
prefix = ""; for (i=1; i<=count; ++i) prefix = prefix prefixUnit
= prefix substr(, 2) # prepend the prefix to the 3rd field, replacing the "$"
print # print result
}' file
作为浓缩单行:
awk -v pu="..\" '{n=gsub("\\","\",);p="";for(i=1;i<=n;++i)p=p pu;=p substr(,2);print}' file
我目前有这个原始字符串:
mklink .\Oracle\MOLT_HYB_010_Header\pkg_cdc_util.pks $.\Oracle\MOLT_HYB_010_Header\pkg_cdc_util.pks
#.......................................................^
.....需要替换如下:
mklink .\Oracle\MOLT_HYB_010_Header\pkg_cdc_util.pks ..\..\..\..\.\Oracle\MOLT_HYB_010_Header\pkg_cdc_util.pks
#.......^......^...........^..........^.................^^^^^^^^^^^^
即根据原始字符串 (".\Oracle\MOLT_HYB_010_Header\pkg_cdc_util.pks")
第 2 列中斜杠的数量,将 $ 替换为“..\” 4 次]例如,我可以单独执行以下操作:
awk -F '\' '{print NF-1}'
--> 打印反斜杠出现的次数sed -e "s,\\,$(printf '..\\%.0s' {1..4}),g"
--> 替换并重复字符串模式
......但不确定如何在 1 个命令行中串起来。
我会使用perl,虽然这有点"clever"
perl -ane '
$n = ($F[1] =~ tr/\/\/);
$F[2] =~ s{$}{"..\" x $n}e;
print join(" ", @F);
' file
哪里
-a
- 将行自动拆分为数组@F
-n
- 为 "file" 的每一行执行正文
$F[1] =~ tr/\/\/
- 计算第二个字段中反斜杠的数量$F[2] =~ s{$}{"..\" x $n}e
- 将美元替换为正确的数字“..\”
awk 一行,假设只有一行要处理
awk -F\ -v RS='$' -v ORS='' '{print} NR==1{for(i=1; i< NF; i++) printf("..\")}'
说明
awk -F\ -v RS='$' ' -v ORS='' ' # Specify separators
{print} # print the line
NR==1{ # for first record
for (i=1; i<NF; i++) # print multiple ..\
printf("..\") # need to escape \
}
'
一个awk
命令,可以处理任意数量的输入行(具有相同的字段结构):
awk -v prefixUnit="..\" '{
count = gsub("\\", "\", ) # count the number of "\"s
# Build the prefix composed of count prefixUnit instances
prefix = ""; for (i=1; i<=count; ++i) prefix = prefix prefixUnit
= prefix substr(, 2) # prepend the prefix to the 3rd field, replacing the "$"
print # print result
}' file
作为浓缩单行:
awk -v pu="..\" '{n=gsub("\\","\",);p="";for(i=1;i<=n;++i)p=p pu;=p substr(,2);print}' file