gpt4 book ai didi

linux - Bash > 从文本文件中检索整数值数组而不重复

转载 作者:太空宇宙 更新时间:2023-11-04 10:27:26 25 4
gpt4 key购买 nike

我有以下脚本。它从文本文件中正确返回整数值(文件 ID)的第一个实例

#!/bin/bash

#create file list
mtp-files > file_list.txt
echo "Hello World"

#retrieve File ID from file list
id=$(awk -F'[ \t]*:[ \t]*' '$1 == "File ID"{print $2; exit}' file_list.txt)

echo "$id"

但我需要一个包含所有文件 ID 整数值且不重复的数组。例如,如果以下是我的 file_list.txt,我的脚本应该返回一个包含 8,9,10 的数组

libmtp version: 1.1.10

mtp-files: Successfully connected
Android device detected, assigning default bug flags
Listing File Information on Device with name: LGE 360 CAM MTP Device
File ID: 8
Filename: 20161212_095355.jpg
File size 625762 (0x0000000000098C62) bytes
Parent ID: 2
Storage ID: 0x00010001
Filetype: JPEG file
File ID: 9
Filename: 20161212_095416.jpg
File size 602264 (0x0000000000093098) bytes
Parent ID: 2
Storage ID: 0x00010001
Filetype: JPEG file
File ID: 10
Filename: 20161212_095520.jpg
File size 642770 (0x000000000009CED2) bytes
Parent ID: 2
Storage ID: 0x00010001
Filetype: JPEG file
File ID: 8
Filename: 20161212_095355.jpg
File size 625762 (0x0000000000098C62) bytes
Parent ID: 2
Storage ID: 0x00010001
Filetype: JPEG file
File ID: 9
Filename: 20161212_095416.jpg
File size 602264 (0x0000000000093098) bytes
Parent ID: 2
Storage ID: 0x00010001
Filetype: JPEG file
File ID: 10
Filename: 20161212_095520.jpg
File size 642770 (0x000000000009CED2) bytes
Parent ID: 2
Storage ID: 0x00010001
Filetype: JPEG file
File ID: 8
Filename: 20161212_095355.jpg
File size 625762 (0x0000000000098C62) bytes
Parent ID: 2
Storage ID: 0x00010001
Filetype: JPEG file
File ID: 9
Filename: 20161212_095416.jpg
File size 602264 (0x0000000000093098) bytes
Parent ID: 2
Storage ID: 0x00010001
Filetype: JPEG file
File ID: 10
Filename: 20161212_095520.jpg
File size 642770 (0x000000000009CED2) bytes
Parent ID: 2
Storage ID: 0x00010001
Filetype: JPEG file
OK.

最佳答案

这个 awk 应该可以解决问题。您不需要第三方工具 sortuniq 来删除重复项,而 awk 可以自行完成。

awk -F'[: ]' '/^File ID/{seen[$2]++;}END{ for (i in seen) print i}' file_list.txt

关于linux - Bash > 从文本文件中检索整数值数组而不重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41104165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com