MuppetGate Posted March 4, 2013 Share Posted March 4, 2013 Hi there. Does anyone have an example Bash script that works with items selected with the option-up? Having trouble separating the items. Thanks Link to comment
jdfwarrior Posted March 5, 2013 Share Posted March 5, 2013 in="{query}" blah=' ' read -a array <<< "$in" echo "${array[0]}" > output.txt echo "${array[1]}" >> output.txt Multiple files are tab separated. Code above reads the {query} input, splits on a tab and stores it into an array called "array". Items can then be individually accessed or you can run them through a loop. Looping through would look like this: for fn in "${array[@]}" do echo "$fn" >> output.txt done Link to comment
MuppetGate Posted March 5, 2013 Author Share Posted March 5, 2013 Thanks. I'll give that a try. It look like I was accessing the array incorrectly. Link to comment
MuppetGate Posted March 5, 2013 Author Share Posted March 5, 2013 Right, I think that blah='<tab>' needs to be IFS='<tab>' All working now. Thanks :-) Link to comment
archislav Posted June 3, 2013 Share Posted June 3, 2013 Hi, thanks Alfred team for the great tool. How to handle multiple files, when some file names includes spaces? this code blah=' ' read -a array <<< "$in" splits file name with spaces into several array elements. Regards Link to comment
vitor Posted June 3, 2013 Share Posted June 3, 2013 (edited) in="{query}" blah=' ' read -a array <<< "$in" echo "${array[0]}" > output.txt echo "${array[1]}" >> output.txt Multiple files are tab separated. Code above reads the {query} input, splits on a tab and stores it into an array called "array". Items can then be individually accessed or you can run them through a loop. Looping through would look like this: for fn in "${array[@]}" do echo "$fn" >> output.txt done Hi, thanks Alfred team for the great tool. How to handle multiple files, when some file names includes spaces? this code blah=' ' read -a array <<< "$in" splits file name with spaces into several array elements. Regards The code for separating the files’ names can be greatly simplified (no need for the array), by doing simply echo "{query}" | perl -pe 's/\t/\n/g' > file_list.txt That’ll take the tabs and convert them to newlines, achieving the same with less code, making it more easily reusable and maintainable. Edited June 3, 2013 by Vítor Link to comment
_mk_ Posted June 4, 2013 Share Posted June 4, 2013 Hi, thanks Alfred team for the great tool. How to handle multiple files, when some file names includes spaces? this code blah=' ' read -a array <<< "$in" splits file name with spaces into several array elements. Regards I am doing it this way: FILES="{query}" OLDIFS="$IFS" IFS='<tab>' for f in ${FILES[*]}; do # do whatever you want with $f done IFS="$OLDIFS" Using Vítor's solution requires you to create a temporary file. If the data is not needed anymore after processing it, I would omit the file system operation and use the array. Link to comment
archislav Posted June 4, 2013 Share Posted June 4, 2013 Thanks for answers, but in my case it turned out, that simple {query} using was quite enough. Is there any difference between: VAR={query} and VAR="{query}" ? Link to comment
_mk_ Posted June 4, 2013 Share Posted June 4, 2013 Thanks for answers, but in my case it turned out, that simple {query} using was quite enough. Is there any difference between: VAR={query} and VAR="{query}" ? You should use VAR="{query}" to avoid problems with strings containing spaces. Link to comment
vitor Posted June 4, 2013 Share Posted June 4, 2013 (edited) Using Vítor's solution requires you to create a temporary file. If the data is not needed anymore after processing it, I would omit the file system operation and use the array. Ah, yes, that is quite correct. I was taking David’s example of saving the filenames to a .txt file and simplifying it, but I agree, if they’re only needed for a one-off operation, you solution is better if the action should be performed for each file individually. If you need it to be applied to a bunch of files as whole, then using perl and xargs is still an acceptable solution. Something like echo "{query}" | perl -pe 's/[\t\n]/\0/g' | xargs -0 <your_script> Edited June 4, 2013 by Vítor Link to comment
_mk_ Posted June 4, 2013 Share Posted June 4, 2013 Ah, yes, that is quite correct. I was taking David’s example of saving the filenames to a .txt file and simplifying it, but I agree, if they’re only needed for a one-off operation, you solution is better if the action should be performed for each file individually. If you need it to be applied to a bunch of files as whole, then using perl and xargs is still an acceptable solution. Something like echo "{query}" | perl -pe 's/[\t\n]/\0/g' | xargs -0 <your_script> xargs is doing the same as the for loop. It reads the filenames from stdin, splits them at the null character and executes <your_script> for every single filename. Link to comment
vitor Posted June 10, 2013 Share Posted June 10, 2013 xargs is doing the same as the for loop. It reads the filenames from stdin, splits them at the null character and executes <your_script> for every single filename. It is not, I tested it before posting that. If you used something like xargs -I {} <script> {}, then it would do it one by one, and by doing the similar xargs -J {} <script> {}, it would act on them in one go. For a short list of files, its behaviour (at least on OSX) seems to be similar to the -J option, when none is specified. I tested it by using the CLI trash command on several files at once — the Finder acts differently when sending multiple files at once to the trash, or doing it one by one (in this case, you hear the trash sound multiple times, and you need to to ⌘+Z once for each file, to get them back). Link to comment
_mk_ Posted June 11, 2013 Share Posted June 11, 2013 It is not, I tested it before posting that. If you used something like xargs -I {} <script> {}, then it would do it one by one, and by doing the similar xargs -J {} <script> {}, it would act on them in one go. For a short list of files, its behaviour (at least on OSX) seems to be similar to the -J option, when none is specified. I tested it by using the CLI trash command on several files at once — the Finder acts differently when sending multiple files at once to the trash, or doing it one by one (in this case, you hear the trash sound multiple times, and you need to to ⌘+Z once for each file, to get them back). The man page is quite strange on these two switches... Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now