How to format output of script to have equal number of spaces












7















Here's my script:



#!/bin/bash
declare -a a=(`ls`)
declare -a b=()
declare -a sorted_arr=()

var =0

while [ -n "${a[$var]}" ]
do

echo "${a[$var]:0:10} |"
var=`expr $var + 1`

done


This script produces inconsistent spacing



another_fi              |
f2.txt |
file1.txt |
file3.txt |


What I want is for vertical pipeline symbols to be aligned



another_fi             |
f2.txt |
file1.txt |
file3.txt |









share|improve this question




















  • 2





    Can you post the code instead of posting its picture?

    – choroba
    Jun 8 '17 at 13:36






  • 3





    Please drop the pictures showing text, replacing them with actual text, then clarify what you want to achieve. Actually at the moment there is no question mark in the entire post.

    – Andrea Lazzarotto
    Jun 8 '17 at 14:03






  • 1





    Fixed the formatting guys :)

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:24
















7















Here's my script:



#!/bin/bash
declare -a a=(`ls`)
declare -a b=()
declare -a sorted_arr=()

var =0

while [ -n "${a[$var]}" ]
do

echo "${a[$var]:0:10} |"
var=`expr $var + 1`

done


This script produces inconsistent spacing



another_fi              |
f2.txt |
file1.txt |
file3.txt |


What I want is for vertical pipeline symbols to be aligned



another_fi             |
f2.txt |
file1.txt |
file3.txt |









share|improve this question




















  • 2





    Can you post the code instead of posting its picture?

    – choroba
    Jun 8 '17 at 13:36






  • 3





    Please drop the pictures showing text, replacing them with actual text, then clarify what you want to achieve. Actually at the moment there is no question mark in the entire post.

    – Andrea Lazzarotto
    Jun 8 '17 at 14:03






  • 1





    Fixed the formatting guys :)

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:24














7












7








7








Here's my script:



#!/bin/bash
declare -a a=(`ls`)
declare -a b=()
declare -a sorted_arr=()

var =0

while [ -n "${a[$var]}" ]
do

echo "${a[$var]:0:10} |"
var=`expr $var + 1`

done


This script produces inconsistent spacing



another_fi              |
f2.txt |
file1.txt |
file3.txt |


What I want is for vertical pipeline symbols to be aligned



another_fi             |
f2.txt |
file1.txt |
file3.txt |









share|improve this question
















Here's my script:



#!/bin/bash
declare -a a=(`ls`)
declare -a b=()
declare -a sorted_arr=()

var =0

while [ -n "${a[$var]}" ]
do

echo "${a[$var]:0:10} |"
var=`expr $var + 1`

done


This script produces inconsistent spacing



another_fi              |
f2.txt |
file1.txt |
file3.txt |


What I want is for vertical pipeline symbols to be aligned



another_fi             |
f2.txt |
file1.txt |
file3.txt |






bash scripts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jun 8 '17 at 14:15









Sergiy Kolodyazhnyy

70.6k9147310




70.6k9147310










asked Jun 8 '17 at 13:30









SeHoonSeHoon

663




663








  • 2





    Can you post the code instead of posting its picture?

    – choroba
    Jun 8 '17 at 13:36






  • 3





    Please drop the pictures showing text, replacing them with actual text, then clarify what you want to achieve. Actually at the moment there is no question mark in the entire post.

    – Andrea Lazzarotto
    Jun 8 '17 at 14:03






  • 1





    Fixed the formatting guys :)

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:24














  • 2





    Can you post the code instead of posting its picture?

    – choroba
    Jun 8 '17 at 13:36






  • 3





    Please drop the pictures showing text, replacing them with actual text, then clarify what you want to achieve. Actually at the moment there is no question mark in the entire post.

    – Andrea Lazzarotto
    Jun 8 '17 at 14:03






  • 1





    Fixed the formatting guys :)

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:24








2




2





Can you post the code instead of posting its picture?

– choroba
Jun 8 '17 at 13:36





Can you post the code instead of posting its picture?

– choroba
Jun 8 '17 at 13:36




3




3





Please drop the pictures showing text, replacing them with actual text, then clarify what you want to achieve. Actually at the moment there is no question mark in the entire post.

– Andrea Lazzarotto
Jun 8 '17 at 14:03





Please drop the pictures showing text, replacing them with actual text, then clarify what you want to achieve. Actually at the moment there is no question mark in the entire post.

– Andrea Lazzarotto
Jun 8 '17 at 14:03




1




1





Fixed the formatting guys :)

– Sergiy Kolodyazhnyy
Jun 8 '17 at 14:24





Fixed the formatting guys :)

– Sergiy Kolodyazhnyy
Jun 8 '17 at 14:24










1 Answer
1






active

oldest

votes


















10














The main reason is because your echo command takes x number of characters from variable and pads 14 spaces. That means total number of chars in output string space won't be consistent.



Instead , you might want to use printf with width specifier %-10s for left padding like this:



bash-4.3$ for i in "${a[@]}"; do     printf "%-10s%-4s|n"  "${i:0:10}" " "; done
1.wav |
2.wav |
3.wav |
input.txt |


This way whatever variable you have will be made to fit within 10 characters,and to those 10 characters we pad 4. - sign makes each string left justified.



Number -10 in %-10s should remain the same to ensure that even if the file is shorter than 10 characters, we still get a 10 character string with spaces padded. But %-4s part can be varied. For instance in the example above, %-4s will have 4 spaces there, but if we want to have 14 spaces, then use %-14s.





Note that it's generally recommended against of parsing output of ls, which is exactly what you're doing. As alternative, we can use find command with while IFS= read -r -d '' structure like this:



bash-4.3$ find -maxdepth 1 -type f -print0 | while IFS= read -r -d '' file;
> do
> printf "%-10s%-4s|n" "${file:0:10}" " "
> done
./3.wav |
./1.wav |
./2.wav |
./.swp |
./input.tx |


Note that find is recursive, so it works on sub-directories as well. If you want to avoid that, use -maxdepth 1 option.



Note that find also has its own -printf option, which may be more efficient to have everything done via one process than two ( that's find plus the subshell in which while runs ):



$ find /bin -type f -printf "%-15f|n" 2>/dev/null | head -n 5
hostname |
nc.traditional |
fusermount |
loadkeys |
zless |


Ideally what I'd suggest is write everything to temporary file, figure out the longest line ( aka longest filename in the file ) and pad however many spaces you want to there accordingly.






share|improve this answer


























  • I ran it but... I want to print is ./3.wav | ./1.wav | ./2.wav | I don't want to include ./randomfi.

    – SeHoon
    Jun 8 '17 at 14:32













  • OK I'll fix that in a minute. Hold on

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:37











  • @최세훈 If the answer solved your question, can you please mark my answer as accepted by checking the gray checkmark on the side.

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:51











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f923396%2fhow-to-format-output-of-script-to-have-equal-number-of-spaces%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









10














The main reason is because your echo command takes x number of characters from variable and pads 14 spaces. That means total number of chars in output string space won't be consistent.



Instead , you might want to use printf with width specifier %-10s for left padding like this:



bash-4.3$ for i in "${a[@]}"; do     printf "%-10s%-4s|n"  "${i:0:10}" " "; done
1.wav |
2.wav |
3.wav |
input.txt |


This way whatever variable you have will be made to fit within 10 characters,and to those 10 characters we pad 4. - sign makes each string left justified.



Number -10 in %-10s should remain the same to ensure that even if the file is shorter than 10 characters, we still get a 10 character string with spaces padded. But %-4s part can be varied. For instance in the example above, %-4s will have 4 spaces there, but if we want to have 14 spaces, then use %-14s.





Note that it's generally recommended against of parsing output of ls, which is exactly what you're doing. As alternative, we can use find command with while IFS= read -r -d '' structure like this:



bash-4.3$ find -maxdepth 1 -type f -print0 | while IFS= read -r -d '' file;
> do
> printf "%-10s%-4s|n" "${file:0:10}" " "
> done
./3.wav |
./1.wav |
./2.wav |
./.swp |
./input.tx |


Note that find is recursive, so it works on sub-directories as well. If you want to avoid that, use -maxdepth 1 option.



Note that find also has its own -printf option, which may be more efficient to have everything done via one process than two ( that's find plus the subshell in which while runs ):



$ find /bin -type f -printf "%-15f|n" 2>/dev/null | head -n 5
hostname |
nc.traditional |
fusermount |
loadkeys |
zless |


Ideally what I'd suggest is write everything to temporary file, figure out the longest line ( aka longest filename in the file ) and pad however many spaces you want to there accordingly.






share|improve this answer


























  • I ran it but... I want to print is ./3.wav | ./1.wav | ./2.wav | I don't want to include ./randomfi.

    – SeHoon
    Jun 8 '17 at 14:32













  • OK I'll fix that in a minute. Hold on

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:37











  • @최세훈 If the answer solved your question, can you please mark my answer as accepted by checking the gray checkmark on the side.

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:51
















10














The main reason is because your echo command takes x number of characters from variable and pads 14 spaces. That means total number of chars in output string space won't be consistent.



Instead , you might want to use printf with width specifier %-10s for left padding like this:



bash-4.3$ for i in "${a[@]}"; do     printf "%-10s%-4s|n"  "${i:0:10}" " "; done
1.wav |
2.wav |
3.wav |
input.txt |


This way whatever variable you have will be made to fit within 10 characters,and to those 10 characters we pad 4. - sign makes each string left justified.



Number -10 in %-10s should remain the same to ensure that even if the file is shorter than 10 characters, we still get a 10 character string with spaces padded. But %-4s part can be varied. For instance in the example above, %-4s will have 4 spaces there, but if we want to have 14 spaces, then use %-14s.





Note that it's generally recommended against of parsing output of ls, which is exactly what you're doing. As alternative, we can use find command with while IFS= read -r -d '' structure like this:



bash-4.3$ find -maxdepth 1 -type f -print0 | while IFS= read -r -d '' file;
> do
> printf "%-10s%-4s|n" "${file:0:10}" " "
> done
./3.wav |
./1.wav |
./2.wav |
./.swp |
./input.tx |


Note that find is recursive, so it works on sub-directories as well. If you want to avoid that, use -maxdepth 1 option.



Note that find also has its own -printf option, which may be more efficient to have everything done via one process than two ( that's find plus the subshell in which while runs ):



$ find /bin -type f -printf "%-15f|n" 2>/dev/null | head -n 5
hostname |
nc.traditional |
fusermount |
loadkeys |
zless |


Ideally what I'd suggest is write everything to temporary file, figure out the longest line ( aka longest filename in the file ) and pad however many spaces you want to there accordingly.






share|improve this answer


























  • I ran it but... I want to print is ./3.wav | ./1.wav | ./2.wav | I don't want to include ./randomfi.

    – SeHoon
    Jun 8 '17 at 14:32













  • OK I'll fix that in a minute. Hold on

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:37











  • @최세훈 If the answer solved your question, can you please mark my answer as accepted by checking the gray checkmark on the side.

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:51














10












10








10







The main reason is because your echo command takes x number of characters from variable and pads 14 spaces. That means total number of chars in output string space won't be consistent.



Instead , you might want to use printf with width specifier %-10s for left padding like this:



bash-4.3$ for i in "${a[@]}"; do     printf "%-10s%-4s|n"  "${i:0:10}" " "; done
1.wav |
2.wav |
3.wav |
input.txt |


This way whatever variable you have will be made to fit within 10 characters,and to those 10 characters we pad 4. - sign makes each string left justified.



Number -10 in %-10s should remain the same to ensure that even if the file is shorter than 10 characters, we still get a 10 character string with spaces padded. But %-4s part can be varied. For instance in the example above, %-4s will have 4 spaces there, but if we want to have 14 spaces, then use %-14s.





Note that it's generally recommended against of parsing output of ls, which is exactly what you're doing. As alternative, we can use find command with while IFS= read -r -d '' structure like this:



bash-4.3$ find -maxdepth 1 -type f -print0 | while IFS= read -r -d '' file;
> do
> printf "%-10s%-4s|n" "${file:0:10}" " "
> done
./3.wav |
./1.wav |
./2.wav |
./.swp |
./input.tx |


Note that find is recursive, so it works on sub-directories as well. If you want to avoid that, use -maxdepth 1 option.



Note that find also has its own -printf option, which may be more efficient to have everything done via one process than two ( that's find plus the subshell in which while runs ):



$ find /bin -type f -printf "%-15f|n" 2>/dev/null | head -n 5
hostname |
nc.traditional |
fusermount |
loadkeys |
zless |


Ideally what I'd suggest is write everything to temporary file, figure out the longest line ( aka longest filename in the file ) and pad however many spaces you want to there accordingly.






share|improve this answer















The main reason is because your echo command takes x number of characters from variable and pads 14 spaces. That means total number of chars in output string space won't be consistent.



Instead , you might want to use printf with width specifier %-10s for left padding like this:



bash-4.3$ for i in "${a[@]}"; do     printf "%-10s%-4s|n"  "${i:0:10}" " "; done
1.wav |
2.wav |
3.wav |
input.txt |


This way whatever variable you have will be made to fit within 10 characters,and to those 10 characters we pad 4. - sign makes each string left justified.



Number -10 in %-10s should remain the same to ensure that even if the file is shorter than 10 characters, we still get a 10 character string with spaces padded. But %-4s part can be varied. For instance in the example above, %-4s will have 4 spaces there, but if we want to have 14 spaces, then use %-14s.





Note that it's generally recommended against of parsing output of ls, which is exactly what you're doing. As alternative, we can use find command with while IFS= read -r -d '' structure like this:



bash-4.3$ find -maxdepth 1 -type f -print0 | while IFS= read -r -d '' file;
> do
> printf "%-10s%-4s|n" "${file:0:10}" " "
> done
./3.wav |
./1.wav |
./2.wav |
./.swp |
./input.tx |


Note that find is recursive, so it works on sub-directories as well. If you want to avoid that, use -maxdepth 1 option.



Note that find also has its own -printf option, which may be more efficient to have everything done via one process than two ( that's find plus the subshell in which while runs ):



$ find /bin -type f -printf "%-15f|n" 2>/dev/null | head -n 5
hostname |
nc.traditional |
fusermount |
loadkeys |
zless |


Ideally what I'd suggest is write everything to temporary file, figure out the longest line ( aka longest filename in the file ) and pad however many spaces you want to there accordingly.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 31 '18 at 8:06

























answered Jun 8 '17 at 14:00









Sergiy KolodyazhnyySergiy Kolodyazhnyy

70.6k9147310




70.6k9147310













  • I ran it but... I want to print is ./3.wav | ./1.wav | ./2.wav | I don't want to include ./randomfi.

    – SeHoon
    Jun 8 '17 at 14:32













  • OK I'll fix that in a minute. Hold on

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:37











  • @최세훈 If the answer solved your question, can you please mark my answer as accepted by checking the gray checkmark on the side.

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:51



















  • I ran it but... I want to print is ./3.wav | ./1.wav | ./2.wav | I don't want to include ./randomfi.

    – SeHoon
    Jun 8 '17 at 14:32













  • OK I'll fix that in a minute. Hold on

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:37











  • @최세훈 If the answer solved your question, can you please mark my answer as accepted by checking the gray checkmark on the side.

    – Sergiy Kolodyazhnyy
    Jun 8 '17 at 14:51

















I ran it but... I want to print is ./3.wav | ./1.wav | ./2.wav | I don't want to include ./randomfi.

– SeHoon
Jun 8 '17 at 14:32







I ran it but... I want to print is ./3.wav | ./1.wav | ./2.wav | I don't want to include ./randomfi.

– SeHoon
Jun 8 '17 at 14:32















OK I'll fix that in a minute. Hold on

– Sergiy Kolodyazhnyy
Jun 8 '17 at 14:37





OK I'll fix that in a minute. Hold on

– Sergiy Kolodyazhnyy
Jun 8 '17 at 14:37













@최세훈 If the answer solved your question, can you please mark my answer as accepted by checking the gray checkmark on the side.

– Sergiy Kolodyazhnyy
Jun 8 '17 at 14:51





@최세훈 If the answer solved your question, can you please mark my answer as accepted by checking the gray checkmark on the side.

– Sergiy Kolodyazhnyy
Jun 8 '17 at 14:51


















draft saved

draft discarded




















































Thanks for contributing an answer to Ask Ubuntu!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f923396%2fhow-to-format-output-of-script-to-have-equal-number-of-spaces%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

 ⁒  ․,‪⁊‑⁙ ⁖, ⁇‒※‌, †,⁖‗‌⁝    ‾‸⁘,‖⁔⁣,⁂‾
”‑,‥–,‬ ,⁀‹⁋‴⁑ ‒ ,‴⁋”‼ ⁨,‷⁔„ ‰′,‐‚ ‥‡‎“‷⁃⁨⁅⁣,⁔
⁇‘⁔⁡⁏⁌⁡‿‶‏⁨ ⁣⁕⁖⁨⁩⁥‽⁀  ‴‬⁜‟ ⁃‣‧⁕‮ …‍⁨‴ ⁩,⁚⁖‫ ,‵ ⁀,‮⁝‣‣ ⁑  ⁂– ․, ‾‽ ‏⁁“⁗‸ ‾… ‹‡⁌⁎‸‘ ‡⁏⁌‪ ‵⁛ ‎⁨ ―⁦⁤⁄⁕