这是我的档案
$ cat head_datafile_pipe_deleimiter.csv
"Rec_Open_Date"|"MSISDN"|"IMEI"|"Data_Volume_Bytes"|"Device_Manufacturer"|"Device_Model"|"Product_Description"|"Data_Volume_MB"
"2016-07-17"|"686"|"630"|"618320"|"Apple Inc"|"Apple iPhone S A1530"|"PREPAY PLUS - $0 -"|"0.589676"
"2016-07-17"|"560"|"570"|"42841779"|"Motorola Mobility LLC, a Lenovo Company"|"Moto X 2nd Generation, X112360445"|"$39.95 Plan"|"40.8571"
"2016-07-17"|"811"|"340"|"2465082"|"Samsung Korea"|"Samsung SM-G900I"|"$69.95 Plan"|"2.35089"
"2016-07-17"|"785"|"610"|"41498628"|"Apple Inc"|"Apple iPhone 6S Plus A1687"|"$29.95 Carryover Plan 1GB"|"39.5762"
"2016-07-17"|"908"|"310"|"6497563"|"Samsung Korea"|"Samsung GT-I9195"|"PREPAY PLUS - $0 -"|"6.19656"
"2016-07-17"|"919"|"610"|"0"|"Samsung Korea"|"Samsung SM-G925I"|"$19 CO COMBO - NOT RECURRENT"|"0"
"2016-07-17"|"356"|"290"|"33189681"|"Apple Inc"|"Apple iPhone 6S A1688"|"$39.95 Plan"|"31.6521"
"2016-07-17"|"009"|"160"|"30340"|"Samsung Korea"|"Samsung SM-J500Y"|"PREPAY PLUS - $1 - #33"|"0.0289345"
"2016-07-17"|"574"|"400"|"549067"|"HUAWEI Technologies Co Ltd"|"HUAWEI Y6"|"PREPAY PLUS - $0 -"|"0.523631"
我想将此输出存储在数组
中$ awk -F'|' 'NR>1{print $7}' head_datafile_pipe_deleimiter.csv | sort | uniq
"$19 CO COMBO - NOT RECURRENT"
"$29.95 Carryover Plan 1GB"
"$39.95 Plan"
"$69.95 Plan"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $1 - #33"
我这样做的方法是将其写入日志文件
$ awk -F'|' 'NR>1{print $7}' head_datafile_pipe_deleimiter.csv | sort | uniq > logfile
$ cat logfile
"$19 CO COMBO - NOT RECURRENT"
"$29.95 Carryover Plan 1GB"
"$39.95 Plan"
"$69.95 Plan"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $1 - #33"
然后将其存储在数组
中$ u_vals=(`cat "logfile"`)
prning数组中的所有元素
$ echo "${u_vals[@]}"
"$19 CO COMBO - NOT RECURRENT" "$29.95 Carryover Plan 1GB" "$39.95 Plan" "$69.95 Plan" "PREPAY PLUS - $0 -" "PREPAY PLUS - $1 - #33"
打印第一个元素
$ echo "${u_vals[0]}"
"$19
获取数组的长度(零是第一个数组)
$ echo "${#u_vals[@]}"
25
打印最后一个元素
$ echo "${u_vals[24]}"
#33"
我真的有2个问题 首先
我想要的是在一个命令中创建我的数组,如果可能的话,不必写入文件
$ u_vals=(`awk -F'|' 'NR>1{print $7}' head_datafile_pipe_deleimiter.csv | sort | uniq`)
,其次,更重要的是,我希望数组有6个元素,如下所示,但空格似乎是问题
$ cat -n logfile
1 "$19 CO COMBO - NOT RECURRENT"
2 "$29.95 Carryover Plan 1GB"
3 "$39.95 Plan"
4 "$69.95 Plan"
5 "PREPAY PLUS - $0 -"
6 "PREPAY PLUS - $1 - #33"
## this will loop through the array but sperrates the elements by spaces
for elem in "${u_vals[@]}"; do echo "$elem"; done
这回答了我的第一个问题,但不回答第二个问题
$ u_vals=($(awk -F'|' 'NR>1{print $7}' head_datafile_pipe_deleimiter.csv | sort | uniq))
$ echo "${u_vals[@]}"
"$19 CO COMBO - NOT RECURRENT" "$29.95 Carryover Plan 1GB" "$39.95 Plan" "$69.95 Plan" "PREPAY PLUS - $0 -" "PREPAY PLUS - $1 - #33"
$ echo "${u_vals[0]}"
"$19
根据下面的回答,这是我选择的方式,请注意我使用awk
而不是下面发布的while
。不确定哪个是最好的,但我只是喜欢并更好地理解awk。
$ mapfile -t u_vals <<<"$(awk -F'|' 'NR>1{print $7}' head_datafile_pipe_deleimiter.csv | sort | uniq)"
$ declare -p u_vals
declare -a u_vals='([0]="\"\$19 CO COMBO - NOT RECURRENT\"" [1]="\"\$29.95 Carryover Plan 1GB\"" [2]="\"\$39.95 Plan\"" [3]="\"\$69.95 Plan\"" [4]="\"PREPAY PLUS - \$0 -\"" [5]="\"PREPAY PLUS - \$1 - #33\"")'
$ for elem in "${u_vals[@]}"; do echo "$elem"; done
"$19 CO COMBO - NOT RECURRENT"
"$29.95 Carryover Plan 1GB"
"$39.95 Plan"
"$69.95 Plan"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $1 - #33"
$ printf "%s\n" "${u_vals[@]}"
"$19 CO COMBO - NOT RECURRENT"
"$29.95 Carryover Plan 1GB"
"$39.95 Plan"
"$69.95 Plan"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $1 - #33"
答案 0 :(得分:1)
使用BASH,您可以将mapfile
与process substitution
:
mapfile -t u_vals < <(
p=1; while IFS='|' read -ra arr; do (( p )) && p=0 || echo "${arr[6]}"; done < file.csv|
sort -u)
测试输出:
printf "%s\n" "${u_vals[@]}"
"$19 CO COMBO - NOT RECURRENT"
"$29.95 Carryover Plan 1GB"
"$39.95 Plan"
"$69.95 Plan"
"PREPAY PLUS - $0 -"
"PREPAY PLUS - $1 - #33"
进程替换中的 while loop
正在执行此操作: