击:俊男通过字符串数组循环后,读出用空格字符串 [英] Bash: cant' read out strings with spaces after looping through array of strings

查看:112
本文介绍了击:俊男通过字符串数组循环后,读出用空格字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是循环读取一个数组,它包含了所有在名为音乐的目录层次结构中的目录和文件的内容(内容是从查找命令的previous输出字符串)。我们的想法是根据流派,艺术家和标题在directory_contents每个数组元素的完整目录路径分成子。由于我的音乐目录按流派先排序,然后按艺术家,然后按标题,我用awk,其中分隔符/显示出来抓住每个相关项目。例如,如果目录看起来像这样利用寻找./Electronic/Squarepusher/My红热Car.aif之后,我会分开电子,Squarepusher和我的红热车,然后将它们存储每个在流派,艺术家和标题单独的数组。后来,我将这些数据进行排序,然后通过管道将有序输出到其他工具来打印一个很好看的表中的所有目录内容(还没有这样做还)。现在,我只是想查看与echo语句字符串分离的结果,并在大多数情况下它似乎工作。不过,我似乎无法提取包含空格子,这是不好的:

   - > ./嘻哈/流浪者合唱团/ title1.aif<  - 
类型:
嘻哈
艺术家:
流浪者合唱团
标题:
TITLE1 - > ./嘻哈/流浪者合唱团/ title2.aif< -
类型:
嘻哈
艺术家:
流浪者合唱团
标题:
TITLE2 - > ./嘻哈/流浪者合唱团/ title3.aif< -
类型:
嘻哈
艺术家:
流浪者合唱团
标题:
TITLE3 - > ./爵士/约翰< -
类型:
爵士乐
艺术家:
约翰
标题:
- >雷恩/ title1.aif< -
类型:
title1.aif
艺术家:标题:

正如你可以看到,当循环字符串约翰柯川,在读取,它是治疗空间作为分隔符,而约翰治疗后,一切都以不同的文件名。我试图寻找下节阵列,以及这里的其他职位bash的手动解决方案,但未能找到工作了我的特定问题(对不起)的解决方案。如果任何人有意见,他们就会大大AP preciated。有问题的code下方出现,在for循环(我没有张贴整个脚本,因为它是pretty漫长的,但我如果需要的话):

  #more在此之前...#declare变量
声明-a genre_list
声明-a title_list
声明-a artist_list
声明-a directory_contents
与内容#populate目录
CD $目录
directory_contents = $(找到。-mindepth 1型的F)
CD ..
在$ {music_file directory_contents [*]};做
    如果[[$ DEBUG =真]];然后
        回声 - > $ music_file< -
    科幻    回声流派
    回声$ music_file | awk的-F/'{打印$ 2}'
    回声艺术家
    回声$ music_file | awk的-F/'{打印$ 3}
    回声标题:
    回声$ music_file | awk的-F/'{打印$ 4}'| awk的-F。 {打印$ 1}
    回声
DONE


解决方案

你为什么不干脆做在单行:

  CD $目录和放大器;&安培; \\
找 。 -mindepth 3 -maxdepth 3型F | \\
awk的-F'/''{拆分($ 4,A,AIF); printf的类型:%S \\ nArtist:%S \\ n标题:%S \\ n \\ n,$ 2,$ 3,A [1];}

更新: (除去在 .AIF 在标题部分)

I am using a loop to read the contents of an array, which contains all of the directories and files in the directory hierarchy called 'music' (contents are strings from the previous output of 'find' command). The idea is to separate the full directory path of each array element in "directory_contents" into substrings according to genre, artist, and title. Since my music directory is sorted first by genre, then by artist, then by title, I am grabbing each relevant item using awk where the delimiter "/" shows up. For example, if the directory looks like this after using find "./Electronic/Squarepusher/My Red Hot Car.aif", I will separate "Electronic", "Squarepusher", and "My Red Hot Car", then store them each in separate arrays for genre, artist, and title. Later I will sort these data, then pipe the sorted output into another utility to print all the directory contents in a nice looking table (haven't done this yet). For now, I am just trying to view the results of the string separation with echo statements, and for the most part it seems to work. However, I can't seem to extract substrings which contain spaces, which isn't good:

-->./Hip-Hop/OutKast/title1.aif<--
Genre:
Hip-Hop
Artist:
OutKast
Title:
title1

-->./Hip-Hop/OutKast/title2.aif<--
Genre:
Hip-Hop
Artist:
OutKast
Title:
title2

-->./Hip-Hop/OutKast/title3.aif<--
Genre:
Hip-Hop
Artist:
OutKast
Title:
title3

-->./Jazz/John<--
Genre:
Jazz
Artist:
John
Title:


-->Coltrane/title1.aif<--
Genre:
title1.aif
Artist:

Title:

As you can see, when the loop reads in the string "John Coltrane", it is treating the space as a delimiter, and treating everything after "John" as a different filename. I tried looking for a solution in the bash manual under the section "Arrays" as well as other posts here, but couldn't find a solution that worked for my specific problem (sorry). If anyone has ideas, they would be greatly appreciated. The problematic code appears below, in the for loop (I didn't post the whole script because it is pretty lengthy, but let me if it is needed):

#more before this...

#declare variables                                                                                                      
declare -a genre_list                                                                                                   
declare -a title_list                                                                                                   
declare -a artist_list                                                                                                  
declare -a directory_contents                                                                                           


#populate directory with contents                                                                                       
cd $directory                                                                                                           
directory_contents=$(find .  -mindepth 1 -type f)                                                                       
cd ..                                                                                                                   


for music_file in ${directory_contents[*]}; do                                                                          
    if [[ $DEBUG = "true" ]] ; then                                                                                     
        echo "-->$music_file<--"                                                                                        
    fi                                                                                                                  

    echo "Genre:"                                                                                                       
    echo $music_file | awk -F"/" '{print $2}'                                                              
    echo "Artist:"                                                                                                      
    echo $music_file | awk -F"/" '{print $3}'                                                               
    echo "Title:"                                                                                                       
    echo $music_file | awk -F"/" '{print $4}' | awk -F"." '{print $1}'                                     
    echo ""                                                                                                             
done   

解决方案

Why don't you simply do it in single line:

cd $directory && \
find .  -mindepth 3 -maxdepth 3 -type f | \ 
awk -F'/' '{split($4,A,".aif"); printf "Genre: %s\nArtist: %s\nTitle: %s\n\n",$2,$3,A[1];}'

Update: (removed the .aif from the Title part)

这篇关于击:俊男通过字符串数组循环后,读出用空格字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆