我有这样的日志行 -
/mnt/internal-app/logs/internal-app.log_2019-08-21.log.gz:2019-08-21 07:31:14,153 5458142 [XNIO-3 task-4] INFO c.c.p.i.m.ws.FileManger [FileName.java:1838] - UUIDs in this bucket 8501792126581991569,8073766106536916628,4830289023695906800,6135982080116553120,8306484440313978157,9040948912536460872,8471856544054164043,5431263453539111247,7661719762428556576
/mnt/internal-app/logs/internal-app.log_2019-08-21.log.gz:2019-08-21 07:31:14,153 5458144 [XNIO-3 task-4] INFO c.c.p.i.m.ws.FileManger [FileName.java:1838] - UUIDs in this bucket 6501792126581991569,8073766106536916628,4830289023695906800,6135982080116553120,8306484440313978157,9040948912536460872,8471856544054164043,5431263453539111247,7661719762428556576
我在 cat 的输出中使用 perl 来获取 UUID 并将它们每个都用双引号括起来 -
cat eligible_uuids_final_app1.txt | perl -ne 'chomp;if(s/^.*UUIDs in this bucket //){@uuids=split(/,/); print "," , join ",",map{qq/"$_"/} @uuids; }'
得到-
"9556896620363267700","7159125371810704365"
我还想将它们分别放在第一个括号中。我不是 Perl 专家,但能够添加起始括号 -
cat eligible_uuids_final_app1.txt | perl -ne 'chomp;if(s/^.*UUIDs in this bucket //){@uuids=split(/,/); print "," , join ",(",map{qq/"$_"/} @uuids; }'
得到-
("9556896620363267700",("7159125371810704365"
尝试添加尾括号 -
cat eligible_uuids_final_app1.txt | perl -ne 'chomp;if(s/^.*UUIDs in this bucket //){@uuids=split(/,/); print "," , join ",(",map{qq/"$_"/}, join ,")" @uuids; }'
出现语法错误 -
$ cat eligible_uuids_final_app1.txt | perl -ne 'chomp;if(s/^.*UUIDs in this bucket //){@uuids=split(/,/); print "," , join ",(",map{qq/"$_"/}, join ,")" @uuids; }'
Array found where operator expected at -e line 1, near "")" "
(Missing operator before ?)
syntax error at -e line 1, near "},"
Execution of -e aborted due to compilation errors.
答案1
如果你想每个要用括号和引号括起来的元素,然后您可以简单地将它们添加到现有map
块中,即替换
map{qq/"$_"/}
经过
map{qq/"($_)"/}
答案2
您不想通过(,
- 来连接数组元素,这会放在(,
的每个元素之间@uuids
。
您只需要(
在连接之前打印 a 并)
在连接之后打印 a 。
例如,使用printf
:
$ perl -ne '
chomp;
if(s/^.*UUIDs in this bucket //) {
@uuids=split(/,/);
printf ",(%s)", join ",",map{qq/"$_"/} @uuids;
}' eligible_uuids_final_app1.txt
,("8501792126581991569","8073766106536916628","4830289023695906800","6135982080116553120","8306484440313978157","9040948912536460872","8471856544054164043","5431263453539111247","7661719762428556576"),("6501792126581991569","8073766106536916628","4830289023695906800","6135982080116553120","8306484440313978157","9040948912536460872","8471856544054164043","5431263453539111247","7661719762428556576")
(在一行中添加换行以使其可读)
我不确定您是否想要在(...)
其内部或外部添加前导逗号。我假设是前者。根据需要调整 printf 格式字符串。
您可能想添加换行符(例如printf ",(%s)\n", ....;
)。