Assumptions/understandings:
- fields are separated by periods
- fields wrapped in parens are to be left alone
- all other fields have leading/trailing double quotes while all other double quotes, as well as pipes, are to be removed
Sample data:
$ cat pipes.dat
("a"|"b"|"c")."A"|"B"|"C".("e"|"f")."E"|"F"
"j"|"K"|"L"."m"|"n"|"o"|"p".("x"|"y"|"z")
One awk
idea:
awk '
BEGIN { FS=OFS="." } # define input/output field separator as a period
{ printf "############
before: %s
",$0 # print a record separator and the current input line;
# solely for display purposes; this line can
# be removed/commented-out once logic is verified
for (i=1; i<=NF; i++) # loop through fields
if ( $i !~ "^[(].*[)]$" ) # if field does not start/end with parens then ...
$i=""" gensub(/"||/,"","g",$i) """ # replace field with a new double quote (+) modified string
# whereby all double quotes and pipes are removed (+)
# a new ending double quote
printf "after : %s
",$0 # print the newly modified line;
# can be replaced with "print" once logic is verified
}
' pipes.dat # read data from file; to read from a variable remove this line and ...
#' <<< "${variable_name}" # uncomment this line
The above generates:
############
before: ("a"|"b"|"c")."A"|"B"|"C".("e"|"f")."E"|"F"
after : ("a"|"b"|"c")."ABC".("e"|"f")."EF"
############
before: "j"|"K"|"L"."m"|"n"|"o"|"p".("x"|"y"|"z")
after : "jKL"."mnop".("x"|"y"|"z")
After removing comments and making the printf
changes:
awk '
BEGIN { FS=OFS="." }
{ for (i=1; i<=NF; i++)
if ( $i !~ "^[(].*[)]$" )
$i=""" gensub(/"||/,"","g",$i) """
print
}
' pipes.dat
Which generates:
("a"|"b"|"c")."ABC".("e"|"f")."EF"
"jKL"."mnop".("x"|"y"|"z")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…