我的文本文件不正确,其中包含产品名称、网站位置和数量。现在我只想准备产品名称、编号(从 URL 中提取)和数量。
输入文件:
rawfile.txt
Component name Link Quantity
Ba Test Con - Red https://kr.element14.com/multicomp/a-1-126-n-r/banana-plug-16a-4mm-cable-red/dp/1698969 25
Ban Te Con - Black https://kr.element14.com/multicomp/a-1-126-n-b/plug-16a-4mm-cable-black/dp/1698970 25
Ban Te Con - Black https://kr.element14.com/hirschmann-testmeasurement/930103700/socket-4mm-black-5pk-mls/dp/1854599 15
预期输出:
Ba Test Con - Red 1698969 25
Ban Te Con - Black 1698970 25
Ban Te Con - Black 1854599 15
我的代码:
For product name:
# extract product name
grep '.*?(?=https://)' rawfile.txt
# extract product number
grep -Po '\b[0-9]{6,7}\t\b' rawfile.txt
# extract quanity
grep -Po '\t[0-9]{1,3}' rawfile.txt
# Now combining the last two functions into one ; this works
# grep -Po '(number argument)(quantity argument)' rawfile.txt
grep -Po '(\b[0-9]{6,7}\t\b)(\t[0-9]{1,3})' rawfile.txt
1698969 25
1698970 25
1854599 15
# Now combining the three functions into one and producing an output text file; this works
# grep -Po '(product name argument)(number argument)(quantity argument)' rawfile.txt
grep -Po '(.*?(?=https://))(\b[0-9]{6,7}\t\b)(\t[0-9]{1,3})' rawfile.txt
目前的输出:
>> grep -Po '(.*?(?=https://))(\b[0-9]{6,7}\t\b)(\t[0-9]{1,3})' rawfile.txt
>> # no output
像这样简单的事情会做吗?(可以改进,但你明白了)