テキストファイルから商品、番号、数量を抽出する

テキストファイルから商品、番号、数量を抽出する

私のテキストファイルが正しくありません。これには製品名、ウェブサイトの場所、数量などが含まれます。今、製品名、番号(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

おすすめ記事