我有以下脚本。它正确地从文本文件
返回整数值(文件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.
答案 0 :(得分:1)
这个awk
应该可以解决问题。您不需要第三方工具sort
和uniq
来删除重复项,而awk
可以自行执行此操作。
awk -F'[: ]' '/^File ID/{seen[$2]++;}END{ for (i in seen) print i}' file_list.txt
答案 1 :(得分:0)
这应该可以解决问题
id=$(awk -F'[ \t]*:[ \t]*' '$1 == "File ID"{print $2; exit}' file_list.txt | sort | uniq)
答案 2 :(得分:0)
如果您只想获取ID,可以像这样减少awk
命令:
awk '/File ID/ { print $3 }' file_list.txt | sort | uniq
您仍然需要使用sort
和uniq
命令才能删除任何重复的结果。
整个命令是:
array_of_ids=($(awk '/File ID/ { print $3 }' file_list.txt | sort | uniq))
echo ${array_of_ids[@]}
10 8 9
echo ${array_of_ids[0]}
10
echo ${array_of_ids[1]}
8
echo ${array_of_ids[2]}
9
**命令周围的“额外”(“和”)“确保您创建一个bash数组。