從文字檔案中提取產品、編號和數量

從文字檔案中提取產品、編號和數量

我的文字檔案不正確,其中包含產品名稱、網站位置和數量。現在我只想準備產品名稱、編號(從 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

答案1

像這樣簡單的事情會做嗎? (可以改進,但你明白了)

$ cat test.txt 
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

$ sed 's#https://.*/##' test.txt 
Ba Test Con - Red   1698969 25
Ban Te Con - Black  1698970  25
Ban Te Con - Black  1854599   15

相關內容