1

made a program that uses a file as input and sort the characters inside the file.

file4="temp.txt"
name4=$(cat $file4)
echo $name4 |  sed 's/\(.\)/\1\n/g' > file4.txt

echo "enter how many rows :"
read cols

IFS=$'\n' a=($(cat file4.txt))
for j in $(seq ${#a[*]}); do
[[ ${a[$j-1]} = $name ]] && echo "${a[$i]}"
done

for (( i=0; i<=$(( ${#a[@]} / cols )); ++i )); do
for (( j=0; j<cols; ++j )); do
    if (( i%2 )); then idx=$(( (i + 1) / 2 * 2 * cols - j - 1 ))
    else idx=$(( (i / 2) * 2 * cols + j )); fi
    printf "%-4s  " "${a[idx]}"
done
printf "\n"
done

i got this output.

Output:

1   2   3   4   5   
10  9   8   7   6   
11  12  13  14  15  
        18  17  16

and i wanted to save it in a single line string and into a file. the problem now is it should be sorted like this

1 10 11 12 9 2 3 8 13 18 17 14 7 4 5 6 15 16

i googled and what im getting is it save in right to left order

1 2 3 4 5 10 9 8 .....

any idea how?

2
  • It is not very clear what you mean. Could you elaborate a bit more and indicate what you've done so far? Commented Aug 26, 2014 at 9:27
  • 1
    @fedorqui edited my post. hopefully will be more clear now Commented Aug 26, 2014 at 9:40

1 Answer 1

1

I updated my answer again and again:

#!/bin/bash

declare -a data

OIFS=$IFS
while read line; do
  let n=0
  IFS=,
  for item in $line; do
    data[$n]+="|$item"
    (( ++n ))
  done
  IFS=$OIFS
done < <(sed -r -e 's@[\t ]@,@g' "$file") 

display() {
  local val=$1
  if [ ! -z "$val" ]; then
    echo -n "$val "
  fi
}

for (( i = 0 ; i < ${#data[@]} ; ++i )); do
  IFS='|'
  declare -a array=( ${data[$i]} )
  IFS=$OIFS
  if (( i % 2 )); then
    for (( j = ${#array[@]} ; j >= 0 ; --j )); do
      display "${array[$j]}"
    done
  else
    for (( j = 0 ; j < ${#array[@]} ; ++j )); do
      display "${array[$j]}"
    done    
  fi
  echo -n ','
done

echo ';'

The sed command is mandatory because the default IFS would split any space, tab or newline. You can try with IFS=' '.

For the input you give in your question, this will print:

$ file=file ./test.bash 
1 10 11 ,,9 12 ,2 ,13 ,8 ,3 14 ,,7 15 18 ,4 ,17 ,6 ,5 16 ,,,;

It is not the expected output, but I think you should add column separator in your output, to differentiate from empty column than space.

Sign up to request clarification or add additional context in comments.

6 Comments

as a side note, you store all the output in data[0] (your for loop at the end of your script displays : 0: 1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 18 17 16).
let say if the data inside a file, how should i declare it so that it will read each char as individual array and not a line as an array
I did not test it. I'll patch it :p
thanks a lot. i tried and it worked. yea. it gives out comma in the blank space. anyway what do u mean by adding column seperator?
Instead of printing space, print a separator you are sure to not have in your output (like ":", etc). Eg: initial output should look like: 1:2:3:4\n5:6:7:8.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.