我想在AWS Batch上编写作业终止脚本,并且在将UUID分配给bash变量时遇到问题。
我有一个名为' LastAndFinal_job_id_list.txt的文本文件,其中包含以下两列:
cbac858b-3884-41c7-8bb7-97eeca8339ae LastAndFinal12
12c2085c-c62b-4c89-8afc-e9448c792a85 LastAndFinal9
a4698208-dd5c-4e1f-bc14-5f8ebc6aadb4 LastAndFinal4
aa601bed-ee86-49ff-b508-eb488d560e42 LastAndFinal33
2cf8739a-5600-454b-a056-8e6c65762b4b LastAndFinal1
b93375cc-3ec7-48e4-833c-146c81cf8aca LastAndFinal10
bf7bfbe7-bb73-4f88-8098-a1e48844f384 LastAndFinal31
86bec3d7-7608-46a5-8d9d-77a57b91807f LastAndFinal28
31f077d9-c658-4d61-8d25-c75d56d26adc LastAndFinal27
dbb29c3b-f63d-4f1f-9f10-e3b1267cda04 LastAndFinal17
5a9fb315-82b2-4750-97c1-cbc47a57dd3b LastAndFinal15
a5a36434-c051-42ff-a08d-cbc397985341 LastAndFinal2
9e2ad835-296e-4bc1-9ea0-72272ac27217 LastAndFinal23
0e128231-de69-4101-b726-f125f424f651 LastAndFinal7
9c984064-2a9e-4f3d-908f-07f50c6ef9a9 LastAndFinal25
3265913e-f846-47a4-8e0d-c467bf65cce3 LastAndFinal20
97846185-ea06-4f4a-8377-1e56640f9099 LastAndFinal22
a28857ac-386f-4efe-ab5d-d3d0e7b862c9 LastAndFinal24
4330edd3-84f9-434a-9929-0805629ab8b9 LastAndFinal8
565f1ac3-3b56-4b14-8a67-398afca98d7c LastAndFinal5
a5179933-4805-4a61-9e1e-73ed1c9f9689 LastAndFinal21
b9d76d04-3e78-4db9-8fb0-e79f5477e672 LastAndFinal29
539a11d1-3c9b-4076-bce9-aa760d99887b LastAndFinal13
19c06f4b-be90-4bfa-8263-6d4861259deb LastAndFinal11
4889ba41-852a-419d-9f09-ee918c5a33e9 LastAndFinal19
8063adc4-7ea8-42f6-b5e2-7ed391c6278b LastAndFinal16
c03e0feb-8be7-4bdc-83a4-0ca0bbc7a34b LastAndFinal14
43ce685a-2764-4bd4-ab43-57f3647c7734 LastAndFinal3
f4b77e2f-0774-42aa-afd2-7c835c720e6c LastAndFinal0
7a6cd4d3-b29a-4760-8d92-93d8d72cee8e LastAndFinal6
a676a3d4-ade4-413e-859d-3bba0c5bb082 LastAndFinal30
0efc9ccb-f508-42e2-b3ef-daebc4bff227 LastAndFinal34
b0fffb70-d92f-45b8-acca-ce050d990fd8 LastAndFinal32
addcc063-6f3e-41a4-90c2-274cf117ec97 LastAndFinal18
6bd35938-b5f9-4dc9-8cd9-bcfa53b4f04e LastAndFinal26
" LastAndFinal *"在第二列中是我提交给AWS Batch的作业名称列表,第一列中的UUID是AWS Batch返回给我的相应作业ID。
我通过此命令提取具有作业名称' LastAndFinal22的作业的UUID:
job_id=$(cat LastAndFinal_job_id_list.txt | grep -w LastAndFinal22 | cut -d ' ' -f 1)
现在问题是当我回显job_id
时,我只得到空白输出。是否有任何想法将UUID分配给变量' job_id'?
答案 0 :(得分:2)
使用awk
建议的替代方案:
job_id=$(awk '$2 == "LastAndFinal22" { print $1 }' LastAndFinal_job_id_list.txt)
答案 1 :(得分:1)
使用sed
,您可以使用反向引用获取UUID
:
job_id=$(sed 's/\(^[^ ]* *\)LastAndFinal22.*/\1/' LastAndFinal_job_id_list.txt)
或仅使用grep
:
job_id=$(grep -oP '[^ ]*(?= *LastAndFinal22)' LastAndFinal_job_id_list.txt)
此处,使用-o
(仅匹配)标记,在lookahead assertion后跟*LastAndFinal22
后输出非空格字符。