[BASH] Lining ascii boxes up next to each other


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [BASH] Lining ascii boxes up next to each other
# 1  
Old 11-13-2012
[BASH] Lining ascii boxes up next to each other

Hello all, I'm writing a simple little game to get myself back into BASH, but am getting unstuck with some formatting. I have the following code:

Code:
let len=${#binary}
    
    for ((j=0; j < len; j++))
    do
        echo  "+-----+"
        echo  "|  ${binary:j:1}  |"    
        echo  "+-----+"
    done

This code prints out each of my boxes on a new line like so (excuse formatting):

Code:
+-------+      
|     1 |        
+-------+  
+-------+      
|     0 |        
+-------+ 
+-------+      
|     1 |        
+-------+

I'd love it to be this way:


Code:
+-------+        +-------+      +-------+   
|     1 |        |     0 |      |     1 |
+-------+        +-------+      +-------+

Any tips would be greatly appreciated!

*** very difficult to format here, hopefully you get the general jist of what I'm trying to do.

Last edited by vgersh99; 11-13-2012 at 04:35 PM..
# 2  
Old 11-13-2012
Just thinking...

What if you wrote each box to its own file, then used the 'paste' command?
# 3  
Old 11-13-2012
Quote:
Originally Posted by joeyg
What if you wrote each box to its own file, then used the 'paste' command?
That is certainly one solution I had thought about, but it doesn't seem very elegant. Potentially there could be an arbitrary number of boxes (I should have said in the original post, sorry), so I don't particularly want them to each have their own file.

I do appreciate the response, though. Thank you.
# 4  
Old 11-13-2012
Just writing down some other thoughts...

(not really CODE, but looks better with CodeTags)
Code:
Write all text to one file, then
In a loop
   head -3 from mainfile > tempfile
   paste newfile tempfile
   tail +3 from mainfile to holdfile
   copy holdfile to mainfile
end loop

taking first 3 lines of file
paste command to add 2nd box to 1st
skip first three lines of file
copy file to mainfile again
*repeat
# 5  
Old 11-14-2012
Hi.

Using an array:
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate arrays and printing on one line.

# Utility functions: print-as-echo, print-line-with-visual-space, debug.
# export PATH="/usr/local/bin:/usr/bin:/bin"
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { : ; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
C=$HOME/bin/context && [ -f $C ] && $C

# Create boxes.

binary=1011
let len=${#binary}
declare -a g
i=0

# Stuff all items into an array.
for ((j=0; j < len; j++))
do
  g[$i]=$(echo  "+-----+")
  (( i++ ))
  g[$i]=$(echo  "|  ${binary:j:1}  |")
  (( i++ ))
  g[$i]=$(echo  "+-----+")
  (( i++ ))
done

# Provide debugging print.
db "${g[*]}"
pl " Debug, ${#g[*]} elements of g:"
for (( i=0; i<${#g[*]}; i++ ))
do
  pe "$i: ${g[$i]}"
done

# Print box objects on a line.
pl " Results, size of g is ${#g[*]}:"
for (( j=0; j<3; j++ ))
do
  for (( i=$j; i<${#g[*]}; i+=3 ))
  do
    printf "%s" "${g[$i]}  "
  done
  printf "\n"
done

exit 0

producing:
Code:
% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
 db, +-----+ |  1  | +-----+ +-----+ |  0  | +-----+ +-----+ |  1  | +-----+ +-----+ |  1  | +-----+

-----
 Debug, 12 elements of g:
0: +-----+
1: |  1  |
2: +-----+
3: +-----+
4: |  0  |
5: +-----+
6: +-----+
7: |  1  |
8: +-----+
9: +-----+
10: |  1  |
11: +-----+

-----
 Results, size of g is 12:
+-----+  +-----+  +-----+  +-----+  
|  1  |  |  0  |  |  1  |  |  1  |  
+-----+  +-----+  +-----+  +-----+

See man bash for details.

Best wishes ... cheers, drl
This User Gave Thanks to drl For This Post:
# 6  
Old 11-14-2012
Smilie

drl, you are amazing. Thank you so much.
# 7  
Old 11-14-2012
Here's an alternate version:

Code:
#! /bin/bash

binary=101000
len=${#binary}

first_third_row () {
    for (( i=0; i<len; i++ ))
    do
        echo -e "+-----+ \c"
    done
    echo
}

second_row () {
    for (( i=0; i<len; i++ ))
    do
        echo -e "|   ${binary:i:1} | \c"
    done
    echo
}

first_third_row
second_row
first_third_row

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

Convert Hex to Ascii in a Ascii file

Hi All, I have an ascii file in which few columns are having hex values which i need to convert into ascii. Kindly suggest me what command can be used in unix shell scripting? Thanks in Advance (2 Replies)
Discussion started by: HemaV
2 Replies

3. Shell Programming and Scripting

Bash - binary data to ascii code

Hello, With bash-script (ubunto server) I'm trying to read a binary file and, for each character, give back its ascii code (including extended ascii). For example: HEX => ASCII => PRINT f5 => 245 => ő 50 => 80 => P To load the binary file into a variable I tried in this way: ... (2 Replies)
Discussion started by: math4
2 Replies

4. Shell Programming and Scripting

bash ascii

hi guys In my bash script I need to use ascii characters such as SYN(22) and US(31). How do I echo them? (3 Replies)
Discussion started by: vlm
3 Replies

5. Shell Programming and Scripting

Preserve extented ascii character when run echo comand inside bash script

Hi everyone, I'm echo some text with extended ascii characters as below: echo -e "Pr\xE9sentation du spectacle" > output or echo -e "Présentation du spectacle" > outputIf I open the file created I see this text Présentation du spectacleThe text is shown correctly in this created file when... (7 Replies)
Discussion started by: Ophiuchus
7 Replies

6. Shell Programming and Scripting

convert ascii values into ascii characters

Hi gurus, I have a file in unix with ascii values. I need to convert all the ascii values in the file to ascii characters. File contains nearly 20000 records with ascii values. (10 Replies)
Discussion started by: sandeeppvk
10 Replies

7. UNIX for Advanced & Expert Users

Processing extended ascii character file names in UNIX (BASH scipts)

Hi, I have a accentuated letter (ö) in a script for an Installer. It's a file name. This is not working and I'm told to try using the octal value for the extended ascii character. Does anyone no how to do this? If I had the word "filförval", can I just put in the value between the letters, like... (9 Replies)
Discussion started by: peli
9 Replies

8. Shell Programming and Scripting

lining up columns of data

Hi, I have two files containing clumns of data and now I'd like to merge them into one. File one, 1.dat, contains 7 data columns and file two, 2.dat, contains 6 data columns I need a third file, 3.dat, containing on the first 7 columns the data of file 1.dat and on the 8th, 9th etc the 6... (2 Replies)
Discussion started by: pau
2 Replies

9. HP-UX

HP-UX boxes - am I mad?

Greetings, and thanks for having such an interesting forum! I currently have 3 Intel boxes running 2000, and two HP boxes running HP-UX 10.20. I am very inexperienced with the Unix/Linux world. I have several problems, mainly in getting them all networked together. I am constrained to keep a... (3 Replies)
Discussion started by: Menace
3 Replies

10. Programming

text boxes, radio buttons , check boxes in c++ on unix

Hi ! Please tell me how to get radio buttons, text boxes , check boxes , option buttons , pull down menus in C++ on Unix. I think it would be done using curses.h ..but that's all i know. TIA, Devyani. (3 Replies)
Discussion started by: devy8
3 Replies
Login or Register to Ask a Question