Bash - string1 replaces part of string2 on concat
Hello there,
This has confused the hell out of me for a while now, so here I am. I have three arrays. Two with numbers and one with text. I'm trying to neatly print these three arrays out next to eachother, but here's where I get my problem. The arrays look like this: Code:
tsizes fsizes files Here was my first attempt: Code:
for (( i=0; i< ${#tsizes[@]}; i++ )) Code:
| 735940608 | File 1 However, having the first array printed on its own in the loop does work. And when I try adding some random characters to print alone with the first array things get really weird: Code:
for (( i=0; i< ${#tsizes[@]}; i++ )) Code:
test417408 Does anyone have an idea on why it would do that and how I can fix it? Cheers! |
I would hazard a guess that the file where the 'arrays' are being read from was created in Windows and hence the line endings are incorrect.
Try running dos2nix over it or if not sure before you start try: Code:
cat -A file |
Damn, you are so right!
One of the arrays is part of a windows .txt file from a windows machine, read by Code:
cat "$1" | cut -s -f3 Thanks mate! |
You could use
Code:
awk 'BEGIN { ORS="\n"; RS="[\t\v\f ]*(\r\n|\n\r|\r|\n)[\t\v\f ]*"; FS="[\t\v\f ]+" } Code:
awk 'BEGIN { ORS="\n"; RS="[\t\v\f ]*(\r\n|\n\r|\r|\n)[\t\v\f ]*"; FS="[\t\v\f ]+" } |
Really need to learn some of that awk magic some day.
Thanks for the alternative mate, appreciate it. |
To add a couple of suggestions for your loop:
1) Assuming all the arrays have an equal number of entries (or you know which one will have the largest number), you can use "${!array[@]}" to output a list of all existing index numbers. This is particularly handy when you have sparse arrays (with missing indexes). 2) The '[]' index boxes in regular arrays (but not associative arrays) operate in an arithmetic environment, so you generally don't need to use '$' on variables. They'll expand automatically. Code:
for i in "${!tsizes[@]}" ; do http://mywiki.wooledge.org/BashFAQ/005 |
All times are GMT -5. The time now is 08:56 PM. |