屏蔽分隔列的shellscript
Masking delimited columns shellscript
我正在尝试屏蔽此分隔文件中的第 6 列。目前我的尝试掩盖了整个文件。我想知道我可能做错了什么。
当前:
awk 'BEGIN{FS=OFS="^^"} {gsub(/./, "X", )} 6'
输入:
00000000001^^00023^^111112233^^C^^ ^^Iwanttomaskthis ^^ ^^ ^^U^^W^^ ^^ ^^222^^6^^77
00000000001^^00024^^111112233^^B^^ ^^Iwanttomaskthis ^^ ^^ ^^X^^W^^ ^^ ^^333^^9^^88
预计:
00000000001^^00023^^111112233^^C^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^ ^^U^^W^^ ^^ ^^222^^6^^77
00000000001^^00024^^111112233^^B^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^ ^^U^^W^^ ^^ ^^222^^6^^77
你可以使用这个awk
:
awk 'BEGIN{FS="\^\^"; OFS="^^"} {gsub(/./, "X", )} 1' file
00000000001^^00023^^111112233^^C^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^ ^^U^^W^^ ^^ ^^222^^6^^77
00000000001^^00024^^111112233^^B^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^ ^^X^^W^^ ^^ ^^333^^9^^88
这里我们需要转义^
因为^
是一个特殊的正则表达式元字符
能否再尝试一种方法,如果对您有帮助,请告诉我。
awk '{match([=10=],/\^\^ +\^\^ /);while((RLENGTH)>1){val=val?val "X":"X";RLENGTH--};sub(/\^\^ +\^\^ /,val,[=10=]);print;val=""}' Input_file
解释:
awk '
{
match([=11=],/\^\^ +\^\^ /); ##Using match functionality of awk to match regex which will look from ^^ space and ^^ space.
while((RLENGTH)>1){ ##Now starting a while loop which will run till the value of RLENGTH is NOT NULL, not here RSTART and RLENGTH are variables which will be set once a match is found by provided regex in match function of awk.
val=val?val "X":"X"; ##creating a variable here and concatenating its value with stating X only each time it comes in loop.
RLENGTH-- ##Decrement the value of RLENGTH each time it comes in while loop.
};
sub(/\^\^ +\^\^ /,"^^"val"^^ ",[=11=])##Substitute regex ^^ space(all spaces till) ^^ space with value of val
print; ##printing the current line. It could edited/non-edited depending upon regex is found by match or not.
val="" ##Nullifying the value of variable val here.
}
' file98997 ##Mentioning the Input_file here.
甚至更改最后几个字段。
awk '{sub(/Iwanttomaskthis /,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")sub(/X..W/,"U^^W")sub(/..333..9..88/,"^^222^^6^^77")}1' file
00000000001^^00023^^111112233^^C^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^ ^^U^^W^^ ^^ ^^222^^6^^77
00000000001^^00024^^111112233^^B^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^ ^^U^^W^^ ^^ ^^222^^6^^77
我正在尝试屏蔽此分隔文件中的第 6 列。目前我的尝试掩盖了整个文件。我想知道我可能做错了什么。
当前:
awk 'BEGIN{FS=OFS="^^"} {gsub(/./, "X", )} 6'
输入:
00000000001^^00023^^111112233^^C^^ ^^Iwanttomaskthis ^^ ^^ ^^U^^W^^ ^^ ^^222^^6^^77
00000000001^^00024^^111112233^^B^^ ^^Iwanttomaskthis ^^ ^^ ^^X^^W^^ ^^ ^^333^^9^^88
预计:
00000000001^^00023^^111112233^^C^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^ ^^U^^W^^ ^^ ^^222^^6^^77
00000000001^^00024^^111112233^^B^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^ ^^U^^W^^ ^^ ^^222^^6^^77
你可以使用这个awk
:
awk 'BEGIN{FS="\^\^"; OFS="^^"} {gsub(/./, "X", )} 1' file
00000000001^^00023^^111112233^^C^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^ ^^U^^W^^ ^^ ^^222^^6^^77
00000000001^^00024^^111112233^^B^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^ ^^X^^W^^ ^^ ^^333^^9^^88
这里我们需要转义^
因为^
是一个特殊的正则表达式元字符
能否再尝试一种方法,如果对您有帮助,请告诉我。
awk '{match([=10=],/\^\^ +\^\^ /);while((RLENGTH)>1){val=val?val "X":"X";RLENGTH--};sub(/\^\^ +\^\^ /,val,[=10=]);print;val=""}' Input_file
解释:
awk '
{
match([=11=],/\^\^ +\^\^ /); ##Using match functionality of awk to match regex which will look from ^^ space and ^^ space.
while((RLENGTH)>1){ ##Now starting a while loop which will run till the value of RLENGTH is NOT NULL, not here RSTART and RLENGTH are variables which will be set once a match is found by provided regex in match function of awk.
val=val?val "X":"X"; ##creating a variable here and concatenating its value with stating X only each time it comes in loop.
RLENGTH-- ##Decrement the value of RLENGTH each time it comes in while loop.
};
sub(/\^\^ +\^\^ /,"^^"val"^^ ",[=11=])##Substitute regex ^^ space(all spaces till) ^^ space with value of val
print; ##printing the current line. It could edited/non-edited depending upon regex is found by match or not.
val="" ##Nullifying the value of variable val here.
}
' file98997 ##Mentioning the Input_file here.
甚至更改最后几个字段。
awk '{sub(/Iwanttomaskthis /,"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")sub(/X..W/,"U^^W")sub(/..333..9..88/,"^^222^^6^^77")}1' file
00000000001^^00023^^111112233^^C^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^ ^^U^^W^^ ^^ ^^222^^6^^77
00000000001^^00024^^111112233^^B^^ ^^XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX^^ ^^ ^^U^^W^^ ^^ ^^222^^6^^77